Package org.codehaus.groovy.runtime
Class DefaultGroovyMethodsSupport
java.lang.Object
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
- Direct Known Subclasses:
ArrayGroovyMethods,DefaultGroovyMethods,IOGroovyMethods,NioExtensions,PluginDefaultGroovyMethods,PluginDefaultGroovyMethods,ProcessGroovyMethods,ResourceGroovyMethods,SocketGroovyMethods,StringGroovyMethods
Support methods for DefaultGroovyMethods and PluginDefaultMethods.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static <T> Collection<T>cloneSimilarCollection(Collection<T> orig, int newCapacity) Copiesorig.protected static <K,V> Map<K, V> cloneSimilarMap(Map<K, V> orig) Copiesorig.static voidCloses the Closeable, ignoring any problems that might occur.static voidcloseWithWarning(Closeable closeable) Close the Closeable.protected static <T> T[]createSimilarArray(T[] orig, int newCapacity) Allocates an array with the component type oforigand lengthnewCapacity.protected static <T> Collection<T>createSimilarCollection(Iterable<T> iterable) Builds an empty collection whose concrete type matchesiterablewhen it is aCollection.protected static <T> Collection<T>createSimilarCollection(Collection<T> collection) Builds an empty collection of the same kind ascollection, usingcollection.size()as the capacity where the kind accepts one.protected static <T> Collection<T>createSimilarCollection(Collection<T> orig, int newCapacity) Builds an empty collection of the same kind asorig.protected static <T> List<T>createSimilarList(List<T> orig, int newCapacity) Builds an empty list of the same kind asorig.protected static <K,V> Map<K, V> createSimilarMap(Map<K, V> orig) Builds an empty map of the same kind asorig.protected static Collectionprotected static <T> Queue<T>createSimilarQueue(Queue<T> orig) Builds an empty queue of the same kind asorig.protected static <T> Set<T>createSimilarSet(Set<T> orig) Builds an empty set of the same kind asorig.protected static intnormaliseIndex(int i, int size) Converts a possibly negative index to a real index into the array.protected static booleanReports whether the elements ofitersfit one type.protected static booleansameType(Collection[] cols) Determines if all items of given array are of the same type.protected static RangeInfosubListBorders(int size, EmptyRange range) Resolves an empty range to list borders for getAt and putAt.protected static RangeInfosubListBorders(int size, Range range) Resolvesrangeto list borders for getAt and putAt.protected static IntRangesubListRange(RangeInfo info, IntRange range) Rebuilds anIntRangefrom borders that were prepared for a sub-list.protected static voidwriteUTF16BomIfRequired(OutputStream stream, String charset) Writes a UTF-16 byte-order mark tostreamwhencharsetnamesUTF-16BEorUTF-16LE.protected static voidwriteUTF16BomIfRequired(OutputStream stream, Charset charset) Writes a UTF-16 byte-order mark tostreamwhen the canonical name ofcharsetisUTF-16BEorUTF-16LE.protected static voidwriteUTF16BomIfRequired(Writer writer, String charset) Writes a UTF-16 byte-order mark towriterwhencharsetnamesUTF-16BEorUTF-16LE.protected static voidwriteUTF16BomIfRequired(Writer writer, Charset charset) Writes a UTF-16 byte-order mark towriterwhen the canonical name ofcharsetisUTF-16BEorUTF-16LE.
-
Constructor Details
-
DefaultGroovyMethodsSupport
public DefaultGroovyMethodsSupport()
-
-
Method Details
-
subListBorders
Resolvesrangeto list borders for getAt and putAt. AnIntRangeorNumberRangesupplies its own borders. Any other range has its endpoints normalised againstsize, and the ends are swapped when the normalised start sits past the normalised end, which is how a range written with a negative end is applied. The end border is one past the last included index.- Parameters:
size- length of the list being indexedrange- range to resolve- Returns:
- the start index, the exclusive end index, and whether the slice runs backwards
-
subListBorders
Resolves an empty range to list borders for getAt and putAt. Both borders are the normalised start, so the slice contains no elements.- Parameters:
size- length of the list being indexedrange- empty range whose start is normalised- Returns:
- a forward slice whose start and exclusive end are equal
-
subListRange
Rebuilds anIntRangefrom borders that were prepared for a sub-list. The exclusive end ininfobecomes an inclusive end, an exclusive bound is stepped back to the original endpoint, and the inclusive ends are swapped wheninforuns backwards.- Parameters:
info- borders previously computed forrangerange- original range, whose inclusive ends are restored- Returns:
- an inclusive-aware range covering the same indexes
-
normaliseIndex
protected static int normaliseIndex(int i, int size) Converts a possibly negative index to a real index into the array. ThrowsArrayIndexOutOfBoundsExceptionwheniis negative and its magnitude exceedssize.- Parameters:
i- the unnormalized indexsize- the array size- Returns:
- the normalised index
-
closeWithWarning
Close the Closeable. Logging a warning if any problems occur.- Parameters:
closeable- the thing to close
-
closeQuietly
Closes the Closeable, ignoring any problems that might occur.- Parameters:
c- the thing to close
-
cloneSimilarCollection
Copiesorig. WhenorigimplementsCloneable, the copy is the result of callingcloneandnewCapacityis ignored. Otherwise an empty collection of a similar concrete type is created with room fornewCapacityelements, then filled fromorig.- Type Parameters:
T- element type- Parameters:
orig- collection to copynewCapacity- capacity of the replacement whenorigcannot be cloned- Returns:
- a copy of
orig
-
cloneSimilarMap
Copiesorig. WhenorigimplementsCloneable, the copy is the result of callingclone. Otherwise an empty map of a similar concrete type is created and filled fromorig.- Type Parameters:
K- key typeV- value type- Parameters:
orig- map to copy- Returns:
- a copy of
orig
-
createSimilarOrDefaultCollection
Builds an empty collection whose concrete type matchesobjectwhenobjectis aCollection. Any other value, including null, yields a newArrayList.- Parameters:
object- collection to imitate, or any other value- Returns:
- an empty collection of a similar type, or an
ArrayList
-
createSimilarCollection
Builds an empty collection whose concrete type matchesiterablewhen it is aCollection. Any other iterable yields a newArrayList. Elements ofiterableare not copied.- Type Parameters:
T- element type- Parameters:
iterable- source of the concrete collection type- Returns:
- an empty collection of a similar type
-
createSimilarCollection
Builds an empty collection of the same kind ascollection, usingcollection.size()as the capacity where the kind accepts one.- Type Parameters:
T- element type- Parameters:
collection- collection to imitate- Returns:
- an empty collection of a similar type
-
createSimilarCollection
Builds an empty collection of the same kind asorig. A set, list, or queue keeps that kind. Anything else becomes anArrayListwith capacitynewCapacity. The capacity is not applied to a set or a queue.- Type Parameters:
T- element type- Parameters:
orig- collection to imitatenewCapacity- initial capacity of a list that is not one of the preserved list classes- Returns:
- an empty collection of a similar type
-
createSimilarList
Builds an empty list of the same kind asorig.LinkedList,Stack,Vector, andCopyOnWriteArrayListare preserved. Any other list becomes anArrayListwith capacitynewCapacity.- Type Parameters:
T- element type- Parameters:
orig- list to imitatenewCapacity- initial capacity when the result is anArrayList- Returns:
- an empty list of a similar type
-
createSimilarArray
protected static <T> T[] createSimilarArray(T[] orig, int newCapacity) Allocates an array with the component type oforigand lengthnewCapacity. Elements oforigare not copied. AnObject[]source produces anObject[]result rather than a reflective array ofObject.- Type Parameters:
T- component type- Parameters:
orig- array whose component type is reusednewCapacity- length of the allocated array- Returns:
- a new array of length
newCapacity
-
createSimilarSet
Builds an empty set of the same kind asorig. A sorted set keeps its comparator: aConcurrentSkipListSetstays that class, and any otherSortedSetbecomes aTreeSet. ACopyOnWriteArraySetis preserved. Any other set becomes aLinkedHashSet, so insertion order is kept.- Type Parameters:
T- element type- Parameters:
orig- set to imitate- Returns:
- an empty set of a similar type
-
createSimilarQueue
Builds an empty queue of the same kind asorig. AnArrayBlockingQueueis recreated with its original capacity, the sum of its size and remaining capacity. APriorityQueuekeeps its comparator.ArrayDeque,ConcurrentLinkedQueue,DelayQueue,LinkedBlockingDeque,LinkedBlockingQueue,PriorityBlockingQueue, andSynchronousQueueare each recreated as the same class. Any other queue becomes aLinkedList.- Type Parameters:
T- element type- Parameters:
orig- queue to imitate- Returns:
- an empty queue of a similar type
-
createSimilarMap
Builds an empty map of the same kind asorig. A sorted map keeps its comparator: aConcurrentSkipListMapstays that class, and any otherSortedMapbecomes aTreeMap.ConcurrentHashMap,Hashtable,Properties,IdentityHashMap, andWeakHashMapare preserved. Any other map becomes aLinkedHashMap, so insertion order is kept.- Type Parameters:
K- key typeV- value type- Parameters:
orig- map to imitate- Returns:
- an empty map of a similar type
-
sameType
Determines if all items of given array are of the same type.- Parameters:
cols- an array of collections- Returns:
- true if the collections are all of the same type
-
sameType
Reports whether the elements ofitersfit one type. The type is taken from the first element. When that element is aNumber, later numbers of any numeric class are accepted. No elements counts as one type.- Parameters:
iters- iterables whose elements are compared- Returns:
truewhen every element fits the type of the first element
-
writeUTF16BomIfRequired
Writes a UTF-16 byte-order mark towriterwhencharsetnamesUTF-16BEorUTF-16LE. Any other charset name writes nothing. The name is resolved withCharset.forName(String). An illegal name or an unsupported charset fails in that call.- Parameters:
writer- destination for the markcharset- charset name to test- Throws:
IOException- when writing the mark fails
-
writeUTF16BomIfRequired
Writes a UTF-16 byte-order mark towriterwhen the canonical name ofcharsetisUTF-16BEorUTF-16LE. Any other charset writes nothing.- Parameters:
writer- destination for the markcharset- charset whose canonical name is tested- Throws:
IOException- when writing the mark fails
-
writeUTF16BomIfRequired
protected static void writeUTF16BomIfRequired(OutputStream stream, String charset) throws IOException Writes a UTF-16 byte-order mark tostreamwhencharsetnamesUTF-16BEorUTF-16LE. Any other charset name writes nothing. The name is resolved withCharset.forName(String). An illegal name or an unsupported charset fails in that call.- Parameters:
stream- destination for the markcharset- charset name to test- Throws:
IOException- when writing the mark fails
-
writeUTF16BomIfRequired
protected static void writeUTF16BomIfRequired(OutputStream stream, Charset charset) throws IOException Writes a UTF-16 byte-order mark tostreamwhen the canonical name ofcharsetisUTF-16BEorUTF-16LE. Big-endian writesFE FF; little-endian writesFF FE. Any other charset writes nothing.- Parameters:
stream- destination for the markcharset- charset whose canonical name is tested- Throws:
IOException- when writing the mark fails
-