Package squidpony

Class Maker

java.lang.Object
squidpony.Maker

public class Maker
extends Object
Utility methods for more easily constructing data structures, particularly those in Java's standard library. All static methods and inner classes; meant to be imported with import static squidpony.Maker.*. Created by Tommy Ettinger on 5/19/2016.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static StringBuilder issueLog
    Stores any information relating to non-fatal issues, such as caught and handled Exceptions that still change the behavior of methods.
  • Constructor Summary

    Constructors 
    Constructor Description
    Maker()  
  • Method Summary

    Modifier and Type Method Description
    static <T> Arrangement<T> makeArrange​(T... elements)
    Makes a Arrangement (Arrange) of T given an array or vararg of T elements.
    static <K extends Enum<K>,​ V>
    EnumOrderedMap<K,​V>
    makeEOM()
    Makes an empty EnumOrderedMap (EOM); needs key and value types to be specified in order to work.
    static <K extends Enum<?>,​ V>
    EnumOrderedMap<K,​V>
    makeEOM​(K k0, V v0, Object... rest)
    Makes an EnumOrderedMap (EOM) with key and value types inferred from the types of k0 and v0, and considers all remaining parameters key-value pairs, casting the Objects at positions 0, 2, 4...
    static <T extends Enum<T>>
    EnumOrderedSet<T>
    makeEOS()
    Makes an empty EnumOrderedSet (EOS); needs item type to be specified in order to work.
    static <T extends Enum<?>>
    EnumOrderedSet<T>
    makeEOS​(T initial, T... elements)
    Makes a EnumOrderedSet (OS) of the enum type T given at least one T element followed by an array or vararg of any number of additional T elements.
    static <K,​ V> HashMap<K,​V> makeHM()
    Makes an empty HashMap (HM); needs key and value types to be specified in order to work.
    static <K,​ V> HashMap<K,​V> makeHM​(K k0, V v0, Object... rest)
    Makes a HashMap (HM) with key and value types inferred from the types of k0 and v0, and considers all parameters key-value pairs, casting the Objects at positions 0, 2, 4...
    static <T> HashSet<T> makeHS​(T element)
    Makes a HashSet (HS) of T given a single T element.
    static <T> HashSet<T> makeHS​(T... elements)
    Makes a HashSet (HS) of T given an array or vararg of T elements.
    static <A,​ B> K2<A,​B> makeK2()
    Makes an empty K2 (two-key set/bimap); needs A and B key types to be specified in order to work.
    static <A,​ B> K2<A,​B> makeK2​(float factor, A a0, B b0, Object... rest)
    Makes a K2 (two-key set/bimap) with the given load factor (which should be between 0.1 and 0.9), A and B key types inferred from the types of a0 and b0, and considers all parameters A-B pairs, casting the Objects at positions 0, 2, 4...
    static <A,​ B> K2<A,​B> makeK2​(A a0, B b0, Object... rest)
    Makes a K2 (two-key set/bimap) with A and B key types inferred from the types of a0 and b0, and considers all parameters A-B pairs, casting the Objects at positions 0, 2, 4...
    static <K,​ V> LinkedHashMap<K,​V> makeLHM()
    Makes an empty LinkedHashMap (LHM); needs key and value types to be specified in order to work.
    static <K,​ V> LinkedHashMap<K,​V> makeLHM​(K k0, V v0, Object... rest)
    Makes a LinkedHashMap (LHM) with key and value types inferred from the types of k0 and v0, and considers all parameters key-value pairs, casting the Objects at positions 0, 2, 4...
    static <T> LinkedHashSet<T> makeLHS​(T element)
    Makes a LinkedHashSet (LHS) of T given a single T element.
    static <T> LinkedHashSet<T> makeLHS​(T... elements)
    Makes a LinkedHashSet (LHS) of T given an array or vararg of T elements.
    static <T> ArrayList<T> makeList​(T element)
    Makes an ArrayList of T given a single T element; avoids creating an array for varargs as makeList(Object[]) would do, but only allows one item.
    static <T> ArrayList<T> makeList​(T... elements)
    Makes an ArrayList of T given an array or vararg of T elements.
    static <K,​ V> OrderedMap<K,​V> makeOM()
    Makes an empty OrderedMap (OM); needs key and value types to be specified in order to work.
    static <K,​ V> OrderedMap<K,​V> makeOM​(float factor, K k0, V v0, Object... rest)
    Makes an OrderedMap (OM) with the given load factor (which should be between 0.1 and 0.9), key and value types inferred from the types of k0 and v0, and considers all remaining parameters key-value pairs, casting the Objects at positions 0, 2, 4...
    static <K,​ V> OrderedMap<K,​V> makeOM​(K k0, V v0, Object... rest)
    Makes an OrderedMap (OM) with key and value types inferred from the types of k0 and v0, and considers all parameters key-value pairs, casting the Objects at positions 0, 2, 4...
    static <T> OrderedSet<T> makeOS​(T element)
    Makes an OrderedSet of T given a single T element; avoids creating an array for varargs as makeOS(Object[]) would do, but only allows one item.
    static <T> OrderedSet<T> makeOS​(T... elements)
    Makes an OrderedSet (OS) of T given an array or vararg of T elements.
    static <T> UnorderedSet<T> makeUOS​(T element)
    Makes an UnorderedSet of T given a single T element; avoids creating an array for varargs as makeOS(Object[]) would do, but only allows one item.
    static <T> UnorderedSet<T> makeUOS​(T... elements)
    Makes an UnorderedSet (UOS) of T given an array or vararg of T elements.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • issueLog

      public static final StringBuilder issueLog
      Stores any information relating to non-fatal issues, such as caught and handled Exceptions that still change the behavior of methods. Typically shouldn't be cleared while debugging, since it could be useful later on, and hopefully won't need to be written to in a release build.
  • Constructor Details

  • Method Details

    • makeLHM

      public static <K,​ V> LinkedHashMap<K,​V> makeLHM​(K k0, V v0, Object... rest)
      Makes a LinkedHashMap (LHM) with key and value types inferred from the types of k0 and v0, and considers all parameters key-value pairs, casting the Objects at positions 0, 2, 4... etc. to K and the objects at positions 1, 3, 5... etc. to V. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of K or V, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field.
      Type Parameters:
      K - the type of keys in the returned LinkedHashMap; if not specified, will be inferred from k0
      V - the type of values in the returned LinkedHashMap; if not specified, will be inferred from v0
      Parameters:
      k0 - the first key; used to infer the types of other keys if generic parameters aren't specified.
      v0 - the first value; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements
      Returns:
      a freshly-made LinkedHashMap with K keys and V values, using k0, v0, and the contents of rest to fill it
    • makeLHM

      public static <K,​ V> LinkedHashMap<K,​V> makeLHM()
      Makes an empty LinkedHashMap (LHM); needs key and value types to be specified in order to work. For an empty LinkedHashMap with String keys and Coord values, you could use Maker.<String, Coord>makeLHM();. Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeLHM() with 2 or more parameters.
      Type Parameters:
      K - the type of keys in the returned LinkedHashMap; cannot be inferred and must be specified
      V - the type of values in the returned LinkedHashMap; cannot be inferred and must be specified
      Returns:
      an empty LinkedHashMap with the given key and value types.
    • makeHM

      public static <K,​ V> HashMap<K,​V> makeHM​(K k0, V v0, Object... rest)
      Makes a HashMap (HM) with key and value types inferred from the types of k0 and v0, and considers all parameters key-value pairs, casting the Objects at positions 0, 2, 4... etc. to K and the objects at positions 1, 3, 5... etc. to V. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of K or V, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field.
      Type Parameters:
      K - the type of keys in the returned HashMap; if not specified, will be inferred from k0
      V - the type of values in the returned HashMap; if not specified, will be inferred from v0
      Parameters:
      k0 - the first key; used to infer the types of other keys if generic parameters aren't specified.
      v0 - the first value; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements
      Returns:
      a freshly-made HashMap with K keys and V values, using k0, v0, and the contents of rest to fill it
    • makeHM

      public static <K,​ V> HashMap<K,​V> makeHM()
      Makes an empty HashMap (HM); needs key and value types to be specified in order to work. For an empty HashMap with String keys and Coord values, you could use Maker.<String, Coord>makeHM();. Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeHM() with 2 or more parameters.
      Type Parameters:
      K - the type of keys in the returned HashMap; cannot be inferred and must be specified
      V - the type of values in the returned HashMap; cannot be inferred and must be specified
      Returns:
      an empty HashMap with the given key and value types.
    • makeList

      public static <T> ArrayList<T> makeList​(T... elements)
      Makes an ArrayList of T given an array or vararg of T elements.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      elements - an array or vararg of T
      Returns:
      a newly-allocated ArrayList containing all of elements, in order
    • makeList

      public static <T> ArrayList<T> makeList​(T element)
      Makes an ArrayList of T given a single T element; avoids creating an array for varargs as makeList(Object[]) would do, but only allows one item.
      Type Parameters:
      T - just about any non-primitive, non-array type (arrays would cause confusion with the vararg method)
      Parameters:
      element - an array or vararg of T
      Returns:
      a newly-allocated ArrayList containing only element
    • makeLHS

      public static <T> LinkedHashSet<T> makeLHS​(T... elements)
      Makes a LinkedHashSet (LHS) of T given an array or vararg of T elements. Duplicate items in elements will have all but one item discarded, using the later item in elements.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      elements - an array or vararg of T
      Returns:
      a newly-allocated LinkedHashSet containing all of the non-duplicate items in elements, in order
    • makeLHS

      public static <T> LinkedHashSet<T> makeLHS​(T element)
      Makes a LinkedHashSet (LHS) of T given a single T element.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      element - a single T
      Returns:
      a newly-allocated LinkedHashSet containing only element
    • makeHS

      public static <T> HashSet<T> makeHS​(T... elements)
      Makes a HashSet (HS) of T given an array or vararg of T elements. Duplicate items in elements will have all but one item discarded, using the later item in elements.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      elements - an array or vararg of T
      Returns:
      a newly-allocated HashSet containing all of the non-duplicate items in elements, in order
    • makeHS

      public static <T> HashSet<T> makeHS​(T element)
      Makes a HashSet (HS) of T given a single T element.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      element - a single T
      Returns:
      a newly-allocated HashSet containing only element
    • makeOM

      public static <K,​ V> OrderedMap<K,​V> makeOM​(K k0, V v0, Object... rest)
      Makes an OrderedMap (OM) with key and value types inferred from the types of k0 and v0, and considers all parameters key-value pairs, casting the Objects at positions 0, 2, 4... etc. to K and the objects at positions 1, 3, 5... etc. to V. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of K or V, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field.
      Type Parameters:
      K - the type of keys in the returned OrderedMap; if not specified, will be inferred from k0
      V - the type of values in the returned OrderedMap; if not specified, will be inferred from v0
      Parameters:
      k0 - the first key; used to infer the types of other keys if generic parameters aren't specified.
      v0 - the first value; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements
      Returns:
      a freshly-made OrderedMap with K keys and V values, using k0, v0, and the contents of rest to fill it
    • makeOM

      public static <K,​ V> OrderedMap<K,​V> makeOM​(float factor, K k0, V v0, Object... rest)
      Makes an OrderedMap (OM) with the given load factor (which should be between 0.1 and 0.9), key and value types inferred from the types of k0 and v0, and considers all remaining parameters key-value pairs, casting the Objects at positions 0, 2, 4... etc. to K and the objects at positions 1, 3, 5... etc. to V. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of K or V, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field.
      Type Parameters:
      K - the type of keys in the returned OrderedMap; if not specified, will be inferred from k0
      V - the type of values in the returned OrderedMap; if not specified, will be inferred from v0
      Parameters:
      factor - the load factor; should be between 0.1 and 0.9, and 0.75f is a safe choice
      k0 - the first key; used to infer the types of other keys if generic parameters aren't specified.
      v0 - the first value; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements
      Returns:
      a freshly-made OrderedMap with K keys and V values, using k0, v0, and the contents of rest to fill it
    • makeOM

      public static <K,​ V> OrderedMap<K,​V> makeOM()
      Makes an empty OrderedMap (OM); needs key and value types to be specified in order to work. For an empty OrderedMap with String keys and Coord values, you could use Maker.<String, Coord>makeOM(). Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeOM() with 2 or more parameters.
      Type Parameters:
      K - the type of keys in the returned OrderedMap; cannot be inferred and must be specified
      V - the type of values in the returned OrderedMap; cannot be inferred and must be specified
      Returns:
      an empty OrderedMap with the given key and value types.
    • makeOS

      public static <T> OrderedSet<T> makeOS​(T... elements)
      Makes an OrderedSet (OS) of T given an array or vararg of T elements. Duplicate items in elements will have all but one item discarded, using the later item in elements.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      elements - an array or vararg of T
      Returns:
      a newly-allocated OrderedSet containing all of the non-duplicate items in elements, in order
    • makeOS

      public static <T> OrderedSet<T> makeOS​(T element)
      Makes an OrderedSet of T given a single T element; avoids creating an array for varargs as makeOS(Object[]) would do, but only allows one item.
      Type Parameters:
      T - just about any non-primitive, non-array type (arrays would cause confusion with the vararg method)
      Parameters:
      element - an array or vararg of T
      Returns:
      a newly-allocated OrderedSet containing only element
    • makeUOS

      public static <T> UnorderedSet<T> makeUOS​(T... elements)
      Makes an UnorderedSet (UOS) of T given an array or vararg of T elements. Duplicate items in elements will have all but one item discarded, using the later item in elements; order will not be kept.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      elements - an array or vararg of T
      Returns:
      a newly-allocated UnorderedSet containing all of the non-duplicate items in elements, in order
    • makeUOS

      public static <T> UnorderedSet<T> makeUOS​(T element)
      Makes an UnorderedSet of T given a single T element; avoids creating an array for varargs as makeOS(Object[]) would do, but only allows one item.
      Type Parameters:
      T - just about any non-primitive, non-array type (arrays would cause confusion with the vararg method)
      Parameters:
      element - an array or vararg of T
      Returns:
      a newly-allocated UnorderedSet containing only element
    • makeEOS

      public static <T extends Enum<?>> EnumOrderedSet<T> makeEOS​(T initial, T... elements)
      Makes a EnumOrderedSet (OS) of the enum type T given at least one T element followed by an array or vararg of any number of additional T elements. Duplicate items in elements will have all but one item discarded, using the later item in elements. The order given here will be kept in the result, like in OrderedSet, and you can use OrderedSet.getAt(int) to get a T value at a given index, like you would with a List.
      Type Parameters:
      T - an enum type
      Parameters:
      initial - the first item to insert into the EnumOrderedSet; if initial is null, the method returns null
      elements - an array or vararg of T; allowed to be empty
      Returns:
      a newly-allocated OrderedSet containing all of the non-duplicate items in elements, in order
    • makeEOS

      public static <T extends Enum<T>> EnumOrderedSet<T> makeEOS()
      Makes an empty EnumOrderedSet (EOS); needs item type to be specified in order to work. For an empty EnumOrderedSet with Radius items, you could use Maker.<Radius>makeEOS(). Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeEOS() with 1 or more parameters.
      Type Parameters:
      T - the type of Enum keys in the returned EnumOrderedSet; cannot be inferred and must be specified
      Returns:
      an empty EnumOrderedSet with the given item type
    • makeArrange

      public static <T> Arrangement<T> makeArrange​(T... elements)
      Makes a Arrangement (Arrange) of T given an array or vararg of T elements. Duplicate items in elements will have all but one item discarded, using the later item in elements. As is always the case with Arrangement, each item will be mapped bi-directionally to its index in the iteration order, and an item can be retrieved with Arrangement.keyAt(int) if you only know its index, or the int index for an item can be retrieved with Arrangement.getInt(Object) if you only have an item.
      Type Parameters:
      T - just about any non-primitive type
      Parameters:
      elements - an array or vararg of T
      Returns:
      a newly-allocated Arrangement containing all of the non-duplicate items in elements, in order
    • makeK2

      public static <A,​ B> K2<A,​B> makeK2​(A a0, B b0, Object... rest)
      Makes a K2 (two-key set/bimap) with A and B key types inferred from the types of a0 and b0, and considers all parameters A-B pairs, casting the Objects at positions 0, 2, 4... etc. to A and the objects at positions 1, 3, 5... etc. to B. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of A or B, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field.
      Type Parameters:
      A - a type of keys in the returned K2; if not specified, will be inferred from a0
      B - a type of keys in the returned K2; if not specified, will be inferred from b0
      Parameters:
      a0 - the first A key; used to infer the types of other keys if generic parameters aren't specified.
      b0 - the first B key; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating A, B, A, B... elements
      Returns:
      a freshly-made K2 with A and B keys, using a0, b0, and the contents of rest to fill it
    • makeK2

      public static <A,​ B> K2<A,​B> makeK2​(float factor, A a0, B b0, Object... rest)
      Makes a K2 (two-key set/bimap) with the given load factor (which should be between 0.1 and 0.9), A and B key types inferred from the types of a0 and b0, and considers all parameters A-B pairs, casting the Objects at positions 0, 2, 4... etc. to A and the objects at positions 1, 3, 5... etc. to B. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of A or B, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field.
      Type Parameters:
      A - a type of keys in the returned K2; if not specified, will be inferred from a0
      B - a type of keys in the returned K2; if not specified, will be inferred from b0
      Parameters:
      factor - the load factor; should be between 0.1 and 0.9, and 0.75f is a safe choice
      a0 - the first A key; used to infer the types of other keys if generic parameters aren't specified.
      b0 - the first B key; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating A, B, A, B... elements
      Returns:
      a freshly-made K2 with A and B keys, using a0, b0, and the contents of rest to fill it
    • makeK2

      public static <A,​ B> K2<A,​B> makeK2()
      Makes an empty K2 (two-key set/bimap); needs A and B key types to be specified in order to work. For an empty K2 with String A keys Coord B keys, you could use Maker.<String, Coord>makeK2();. Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeK2() with 2 or more parameters.
      Type Parameters:
      A - the type of "A" keys in the returned K2; cannot be inferred and must be specified
      B - the type of "B" keys in the returned K2; cannot be inferred and must be specified
      Returns:
      an empty K2 with the given key and value types.
    • makeEOM

      public static <K extends Enum<?>,​ V> EnumOrderedMap<K,​V> makeEOM​(K k0, V v0, Object... rest)
      Makes an EnumOrderedMap (EOM) with key and value types inferred from the types of k0 and v0, and considers all remaining parameters key-value pairs, casting the Objects at positions 0, 2, 4... etc. to K and the objects at positions 1, 3, 5... etc. to V. If rest has an odd-number length, then it discards the last item. If any pair of items in rest cannot be cast to the correct type of K or V, then this inserts nothing for that pair and logs information on the problematic pair to the static Maker.issueLog field. The order given here will be kept in the result, unlike in the JDK's EnumMap class, and you can use OrderedMap.keyAt(int) or OrderedMap.getAt(int) to get a key or value at a given index, like you would with a List.
      Type Parameters:
      K - the type of Enum keys in the returned EnumOrderedMap; if not specified, will be inferred from k0
      V - the type of values in the returned EnumOrderedMap; if not specified, will be inferred from v0
      Parameters:
      k0 - the first key, which must be an Enum; used to infer the types of other keys if generic parameters aren't specified.
      v0 - the first value; used to infer the types of other values if generic parameters aren't specified.
      rest - an array or vararg of keys and values in pairs; should contain alternating K, V, K, V... elements
      Returns:
      a freshly-made EnumOrderedMap with K keys and V values, using k0, v0, and the contents of rest to fill it
    • makeEOM

      public static <K extends Enum<K>,​ V> EnumOrderedMap<K,​V> makeEOM()
      Makes an empty EnumOrderedMap (EOM); needs key and value types to be specified in order to work. For an empty EnumOrderedMap with Radius keys and Coord values, you could use Maker.<Radius, Coord>makeEOM(). Using the new keyword is probably just as easy in this case; this method is provided for completeness relative to makeEOM() with 2 or more parameters.
      Type Parameters:
      K - the type of Enum keys in the returned EnumOrderedMap; cannot be inferred and must be specified
      V - the type of values in the returned EnumOrderedMap; cannot be inferred and must be specified
      Returns:
      an empty EnumOrderedMap with the given key and value types.