private String[] makeCommandLine(List<String> commandLineList) { log.verbose("Compilation arguments:\n" + DefaultGroovyMethods.join((Iterable)commandLineList, "\n")); return commandLineList.toArray(EMPTY_STRING_ARRAY); }
private static <K, V> void addEntry(Map<K, V> result, Object newEntry) { if (newEntry instanceof Map) { leftShift(result, (Map)newEntry); } else if (newEntry instanceof List) { List list = (List) newEntry; // def (key, value) == list Object key = list.isEmpty() ? null : list.get(0); Object value = list.size() <= 1 ? null : list.get(1); leftShift(result, new MapEntry(key, value)); } else if (newEntry.getClass().isArray()) { Object[] array = (Object[]) newEntry; // def (key, value) == array.toList() Object key = array.length == 0 ? null : array[0]; Object value = array.length <= 1 ? null : array[1]; leftShift(result, new MapEntry(key, value)); } else { // TODO: enforce stricter behavior? // given Map.Entry is an interface, we get a proxy which gives us lots // of flexibility but sometimes the error messages might be unexpected leftShift(result, asType(newEntry, Map.Entry.class)); } }
/** * Extend class globally with category methods. * All methods for given class and all super classes will be added to the class. * * @param self any Class * @param categoryClasses a category classes to use * @since 1.6.0 */ public static void mixin(Class self, List<Class> categoryClasses) { mixin(getMetaClass(self), categoryClasses); }
/** * Adds max() method to Iterator objects. The iterator will become * exhausted of elements after determining the maximum value. * * @param self an Iterator * @return the maximum value * @see groovy.util.GroovyCollections#max(java.util.Collection) * @since 1.5.5 */ public static <T> T max(Iterator<T> self) { return max((Iterable<T>)toList(self)); }
/** * Selects the minimum value found from the Object array using the given comparator. * * @param self an array * @param comparator a Comparator * @return the minimum value * @see #min(java.util.Collection, java.util.Comparator) * @since 1.5.5 */ public static <T> T min(T[] self, Comparator<T> comparator) { return min((Iterable<T>)toList(self), comparator); }
int params = closure.getMaximumNumberOfParameters(); if (params != 1) { return max(self, new ClosureComparator<T>(closure)); Object answerValue = null; for (T item : self) { Object value = closure.call(item); if (first) { first = false; answer = item; answerValue = value; } else if (ScriptBytecodeAdapter.compareLessThan(answerValue, value)) { answer = item; answerValue = value;
private void proxyPerInstanceMetaClasses(List<Reference> refs) { for (Reference ref : refs) { Object obj = ref.get(); if (obj != null) { Class theClass = obj.getClass(); if (obj instanceof GroovyObject) { GroovyObject gobj = (GroovyObject) obj; MetaClass metaClass = gobj.getMetaClass(); MetaClass proxyMetaClass = proxyMetaClass(theClass, metaClass); DefaultGroovyMethods.setMetaClass(gobj, proxyMetaClass); } else { MetaClass metaClass = DefaultGroovyMethods.getMetaClass(obj); MetaClass proxyMetaClass = proxyMetaClass(theClass, metaClass); MetaClassHelper.doSetMetaClass(obj, proxyMetaClass); } } } }
private static <T> Collection<T> flatten(Iterable elements, Collection<T> addTo, Closure<? extends T> flattenUsing) { for (Object element : elements) { if (element instanceof Collection) { flatten((Collection) element, addTo, flattenUsing); } else if (element != null && element.getClass().isArray()) { flatten(DefaultTypeTransformation.arrayAsCollection(element), addTo, flattenUsing); } else { T flattened = flattenUsing.call(new Object[]{element}); boolean returnedSelf = flattened == element; if (!returnedSelf && flattened instanceof Collection) { List<?> list = toList((Iterable<?>) flattened); if (list.size() == 1 && list.get(0) == element) { returnedSelf = true; } } if (flattened instanceof Collection && !returnedSelf) { flatten((Collection) flattened, addTo, flattenUsing); } else { addTo.add(flattened); } } } return addTo; }
public void testListArithmetic() { List<List> numLists = new ArrayList<List>(); numLists.add(Arrays.asList(1, 2, 3)); numLists.add(Arrays.asList(10, 20, 30)); assertEquals(Arrays.asList(6, 60), collect(numLists, new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a + b + c; } })); Closure<Integer> arithmeticClosure = new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a * b + c; } }; Closure<Integer> tensAndUnits = arithmeticClosure.curry(10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(0, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(1, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); Closure<Integer> timesPlus5 = arithmeticClosure.rcurry(5); assertEquals(35, (int) timesPlus5.call(15, 2)); timesPlus5 = arithmeticClosure.ncurry(2, 5); assertEquals(35, (int) timesPlus5.call(15, 2)); }
if (sort != null) files = DefaultGroovyMethods.sort((Iterable<File>)files, sort); for (File file : files) { if (file.isDirectory()) { if (type != FileType.FILES) { if (closure != null && notFiltered(file, filter, nameFilter, excludeFilter, excludeNameFilter)) { Object closureResult = closure.call(file); if (closureResult == FileVisitResult.SKIP_SIBLINGS) break; if (closureResult == FileVisitResult.TERMINATE) return FileVisitResult.TERMINATE; Object preResult = null; if (pre != null) { preResult = pre.call(file); postResult = post.call(file);
/** * Iterates through this aggregate Object transforming each item into a new value using Closure.IDENTITY * as a transformer, basically returning a list of items copied from the original object. * <pre class="groovyTestCase">assert [1,2,3] == [1,2,3].iterator().collect()</pre> * * @param self an aggregate Object with an Iterator returning its items * @return a Collection of the transformed values * @see Closure#IDENTITY * @since 1.8.5 */ public static Collection collect(Object self) { return collect(self, Closure.IDENTITY); }
Number maxDepthNumber = DefaultGroovyMethods.asType(options.remove("maxDepth"), Number.class); int maxDepth = maxDepthNumber == null ? -1 : maxDepthNumber.intValue(); Boolean visitRoot = DefaultGroovyMethods.asType(get(options, "visitRoot", false), Boolean.class); Boolean preRoot = DefaultGroovyMethods.asType(get(options, "preRoot", false), Boolean.class); Boolean postRoot = DefaultGroovyMethods.asType(get(options, "postRoot", false), Boolean.class); final Closure pre = (Closure) options.get("preDir"); final Closure post = (Closure) options.get("postDir"); Object preResult = null; if (preRoot && pre != null) { preResult = pre.call(self); Object closureResult = closure.call(self); if (closureResult == FileVisitResult.TERMINATE) return; if (postRoot && post != null && terminated != FileVisitResult.TERMINATE) post.call(self);
/** * Overloads the left shift operator to provide an easy way to append * objects to a List. * <pre class="groovyTestCase">def list = [1,2] * list << 3 * assert list == [1,2,3]</pre> * * @param self a List * @param value an Object to be added to the List. * @return same List, after the value was added to it. * @since 2.4.0 */ public static <T> List<T> leftShift(List<T> self, T value) { return (List<T>) leftShift((Collection<T>) self, value); }
/** * Concatenates the <code>toString()</code> representation of each * item from the iterator, with the given String as a separator between * each item. The iterator will become exhausted of elements after * determining the resulting conjoined value. * * @param self an Iterator of items * @param separator a String separator * @return the joined String * @since 1.5.5 */ public static String join(Iterator<Object> self, String separator) { return join((Iterable)toList(self), separator); }
/** * {@inheritDoc} * * @since 0.2.0 */ @SuppressWarnings({"PMD.UnusedMethodParameter", "PMD.AvoidInstantiatingObjectsInLoops"}) public void visit(final ASTNode[] nodes, final SourceUnit sourceUnit) { final List<ClassNode> classNodeList = (List<ClassNode>) collect(sourceUnit.getAST().getClasses()); for (final ClassNode clazzNode : classNodeList) { for (final Class<? extends Transformer> clazz : getTransformers()) { newInstance(clazz, new Object[] { sourceUnit }) .visitClass(clazzNode); } } }
public void testGetProperty() throws Exception { MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); ObjectName name = new ObjectName("groovy.test:role=TestMBean,type=Dummy"); // use Class.forName instead of new Dummy() to allow separate compilation mbeanServer.registerMBean(Class.forName("groovy.util.Dummy").newInstance(), name); assertEquals("JMX value of Name", "James", mbeanServer.getAttribute(name, "Name")); GroovyMBean object = new GroovyMBean(mbeanServer, name); Object value = object.getProperty("Name"); assertEquals("Name property", "James", value); object.setProperty("Name", "Bob"); assertEquals("Name property", "Bob", object.getProperty("Name")); // now let's look up the name via JMX to check assertEquals("JMX value of Name", "Bob", mbeanServer.getAttribute(name, "Name")); assertEquals("Location : London|Name : Bob|Size : 12", join(sort(object.listAttributeValues()), "|")); assertEquals("start|stop", join(sort(object.listOperationNames()), "|")); assertEquals("void start()", join(sort(object.describeOperation("start")), "|")); assertEquals("(rw) java.lang.String Location", object.describeAttribute("Location")); } }
List<T> list = mutate ? asList(self) : toList(self); int params = closure.getMaximumNumberOfParameters(); if (params == 1) { Collections.sort(list, new OrderBy<T>(closure));
/** * Sorts the given iterator items into a sorted iterator. The items are * assumed to be comparable. The original iterator will become * exhausted of elements after completing this method call. * A new iterator is produced that traverses the items in sorted order. * * @param self the Iterator to be sorted * @return the sorted items as an Iterator * @since 1.5.5 */ public static <T> Iterator<T> sort(Iterator<T> self) { return sort((Iterable<T>) toList(self)).listIterator(); }
/** * Flatten an array. This array and any nested arrays or * collections have their contents (recursively) added to the new collection. * * @param self a char Array to flatten * @return a flattened Collection * @since 1.6.0 */ public static Collection flatten(char[] self) { return flatten(toList(self), new ArrayList()); }