Class DefaultGroovyMethodsSupport

java.lang.Object
org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport
Direct Known Subclasses:
ArrayGroovyMethods, DefaultGroovyMethods, IOGroovyMethods, NioExtensions, PluginDefaultGroovyMethods, PluginDefaultGroovyMethods, ProcessGroovyMethods, ResourceGroovyMethods, SocketGroovyMethods, StringGroovyMethods

public class DefaultGroovyMethodsSupport extends Object
Support methods for DefaultGroovyMethods and PluginDefaultMethods.
  • Constructor Details

    • DefaultGroovyMethodsSupport

      public DefaultGroovyMethodsSupport()
  • Method Details

    • subListBorders

      protected static RangeInfo subListBorders(int size, Range range)
      Resolves range to list borders for getAt and putAt. An IntRange or NumberRange supplies its own borders. Any other range has its endpoints normalised against size, 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 indexed
      range - range to resolve
      Returns:
      the start index, the exclusive end index, and whether the slice runs backwards
    • subListBorders

      protected static RangeInfo subListBorders(int size, EmptyRange range)
      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 indexed
      range - empty range whose start is normalised
      Returns:
      a forward slice whose start and exclusive end are equal
    • subListRange

      protected static IntRange subListRange(RangeInfo info, IntRange range)
      Rebuilds an IntRange from borders that were prepared for a sub-list. The exclusive end in info becomes an inclusive end, an exclusive bound is stepped back to the original endpoint, and the inclusive ends are swapped when info runs backwards.
      Parameters:
      info - borders previously computed for range
      range - 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. Throws ArrayIndexOutOfBoundsException when i is negative and its magnitude exceeds size.
      Parameters:
      i - the unnormalized index
      size - the array size
      Returns:
      the normalised index
    • closeWithWarning

      public static void closeWithWarning(Closeable closeable)
      Close the Closeable. Logging a warning if any problems occur.
      Parameters:
      closeable - the thing to close
    • closeQuietly

      public static void closeQuietly(Closeable c)
      Closes the Closeable, ignoring any problems that might occur.
      Parameters:
      c - the thing to close
    • cloneSimilarCollection

      protected static <T> Collection<T> cloneSimilarCollection(Collection<T> orig, int newCapacity)
      Copies orig. When orig implements Cloneable, the copy is the result of calling clone and newCapacity is ignored. Otherwise an empty collection of a similar concrete type is created with room for newCapacity elements, then filled from orig.
      Type Parameters:
      T - element type
      Parameters:
      orig - collection to copy
      newCapacity - capacity of the replacement when orig cannot be cloned
      Returns:
      a copy of orig
    • cloneSimilarMap

      protected static <K, V> Map<K,V> cloneSimilarMap(Map<K,V> orig)
      Copies orig. When orig implements Cloneable, the copy is the result of calling clone. Otherwise an empty map of a similar concrete type is created and filled from orig.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      orig - map to copy
      Returns:
      a copy of orig
    • createSimilarOrDefaultCollection

      protected static Collection createSimilarOrDefaultCollection(Object object)
      Builds an empty collection whose concrete type matches object when object is a Collection. Any other value, including null, yields a new ArrayList.
      Parameters:
      object - collection to imitate, or any other value
      Returns:
      an empty collection of a similar type, or an ArrayList
    • createSimilarCollection

      protected static <T> Collection<T> createSimilarCollection(Iterable<T> iterable)
      Builds an empty collection whose concrete type matches iterable when it is a Collection. Any other iterable yields a new ArrayList. Elements of iterable are not copied.
      Type Parameters:
      T - element type
      Parameters:
      iterable - source of the concrete collection type
      Returns:
      an empty collection of a similar type
    • createSimilarCollection

      protected static <T> Collection<T> createSimilarCollection(Collection<T> collection)
      Builds an empty collection of the same kind as collection, using collection.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

      protected static <T> Collection<T> createSimilarCollection(Collection<T> orig, int newCapacity)
      Builds an empty collection of the same kind as orig. A set, list, or queue keeps that kind. Anything else becomes an ArrayList with capacity newCapacity. The capacity is not applied to a set or a queue.
      Type Parameters:
      T - element type
      Parameters:
      orig - collection to imitate
      newCapacity - initial capacity of a list that is not one of the preserved list classes
      Returns:
      an empty collection of a similar type
    • createSimilarList

      protected static <T> List<T> createSimilarList(List<T> orig, int newCapacity)
      Builds an empty list of the same kind as orig. LinkedList, Stack, Vector, and CopyOnWriteArrayList are preserved. Any other list becomes an ArrayList with capacity newCapacity.
      Type Parameters:
      T - element type
      Parameters:
      orig - list to imitate
      newCapacity - initial capacity when the result is an ArrayList
      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 of orig and length newCapacity. Elements of orig are not copied. An Object[] source produces an Object[] result rather than a reflective array of Object.
      Type Parameters:
      T - component type
      Parameters:
      orig - array whose component type is reused
      newCapacity - length of the allocated array
      Returns:
      a new array of length newCapacity
    • createSimilarSet

      protected static <T> Set<T> createSimilarSet(Set<T> orig)
      Builds an empty set of the same kind as orig. A sorted set keeps its comparator: a ConcurrentSkipListSet stays that class, and any other SortedSet becomes a TreeSet. A CopyOnWriteArraySet is preserved. Any other set becomes a LinkedHashSet, so insertion order is kept.
      Type Parameters:
      T - element type
      Parameters:
      orig - set to imitate
      Returns:
      an empty set of a similar type
    • createSimilarQueue

      protected static <T> Queue<T> createSimilarQueue(Queue<T> orig)
      Builds an empty queue of the same kind as orig. An ArrayBlockingQueue is recreated with its original capacity, the sum of its size and remaining capacity. A PriorityQueue keeps its comparator. ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, PriorityBlockingQueue, and SynchronousQueue are each recreated as the same class. Any other queue becomes a LinkedList.
      Type Parameters:
      T - element type
      Parameters:
      orig - queue to imitate
      Returns:
      an empty queue of a similar type
    • createSimilarMap

      protected static <K, V> Map<K,V> createSimilarMap(Map<K,V> orig)
      Builds an empty map of the same kind as orig. A sorted map keeps its comparator: a ConcurrentSkipListMap stays that class, and any other SortedMap becomes a TreeMap. ConcurrentHashMap, Hashtable, Properties, IdentityHashMap, and WeakHashMap are preserved. Any other map becomes a LinkedHashMap, so insertion order is kept.
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      orig - map to imitate
      Returns:
      an empty map of a similar type
    • sameType

      protected static boolean sameType(Collection[] cols)
      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

      protected static boolean sameType(Iterable[] iters)
      Reports whether the elements of iters fit one type. The type is taken from the first element. When that element is a Number, later numbers of any numeric class are accepted. No elements counts as one type.
      Parameters:
      iters - iterables whose elements are compared
      Returns:
      true when every element fits the type of the first element
    • writeUTF16BomIfRequired

      protected static void writeUTF16BomIfRequired(Writer writer, String charset) throws IOException
      Writes a UTF-16 byte-order mark to writer when charset names UTF-16BE or UTF-16LE. Any other charset name writes nothing. The name is resolved with Charset.forName(String). An illegal name or an unsupported charset fails in that call.
      Parameters:
      writer - destination for the mark
      charset - charset name to test
      Throws:
      IOException - when writing the mark fails
    • writeUTF16BomIfRequired

      protected static void writeUTF16BomIfRequired(Writer writer, Charset charset) throws IOException
      Writes a UTF-16 byte-order mark to writer when the canonical name of charset is UTF-16BE or UTF-16LE. Any other charset writes nothing.
      Parameters:
      writer - destination for the mark
      charset - 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 to stream when charset names UTF-16BE or UTF-16LE. Any other charset name writes nothing. The name is resolved with Charset.forName(String). An illegal name or an unsupported charset fails in that call.
      Parameters:
      stream - destination for the mark
      charset - 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 to stream when the canonical name of charset is UTF-16BE or UTF-16LE. Big-endian writes FE FF; little-endian writes FF FE. Any other charset writes nothing.
      Parameters:
      stream - destination for the mark
      charset - charset whose canonical name is tested
      Throws:
      IOException - when writing the mark fails