Class CaseInsensitiveSet

java.lang.Object
com.github.tommyettinger.ds.ObjectSet<CharSequence>
com.github.tommyettinger.ds.CaseInsensitiveSet
All Implemented Interfaces:
EnhancedCollection<CharSequence>, Iterable<CharSequence>, Collection<CharSequence>, Set<CharSequence>

public class CaseInsensitiveSet extends ObjectSet<CharSequence>
A custom variant on ObjectSet that always uses CharSequence keys and compares them as case-insensitive. This uses a fairly complex, quite-optimized hashing function because it needs to hash CharSequences rather often, and to do so ignoring case means String.hashCode() won't work, plus not all CharSequences implement hashCode() themselves (such as StringBuilder). User code similar to this can often get away with a simple polynomial hash (the typical Java kind, used by String and Arrays), or if more speed is needed, one with some of these optimizations by Richard Startin. If you don't want to write or benchmark a hash function (which is quite understandable), Utilities.hashCodeIgnoreCase(CharSequence) can get a case-insensitive hash of any CharSequence, as a long. It does this without allocating new Strings all over, where many case-insensitive algorithms do allocate quite a lot, but it does this by handling case incorrectly for the Georgian alphabet. If I see Georgian text in-the-wild, I may reconsider, but I don't think that particular alphabet is in widespread use. There's also Utilities.equalsIgnoreCase(CharSequence, CharSequence) for equality comparisons that are similarly case-insensitive, except for Georgian.
This is very similar to FilteredStringSet when its editor is Character.toUpperCase(char) or Casing.caseUp(char). FilteredStringSet works with Strings rather than CharSequences, which may be more convenient, and allows filtering some characters out of hashing and equality comparisons. If you want a case-insensitive set that ignores any non-letter characters in a String, then CaseInsensitiveSet won't do, but new FilteredStringSet<>(CharPredicates.IS_LETTER, Casing::caseUp will work. Note that GWT only handles Character.isLetter(char) for ASCII letters; CharPredicates in this library provides cross-platform predicates that use CharBitSet to store their data, and the library RegExodus offers replacements in Category for other Unicode categories, such as upper-case letters, currency symbols, decimal digits, and so on.
  • Constructor Details

    • CaseInsensitiveSet

      public CaseInsensitiveSet()
      Creates a new set with an initial capacity of Utilities.getDefaultTableCapacity() and a load factor of Utilities.getDefaultLoadFactor().
    • CaseInsensitiveSet

      public CaseInsensitiveSet(int initialCapacity)
      Creates a new set with the specified initial capacity a load factor of Utilities.getDefaultLoadFactor(). This set will hold initialCapacity items before growing the backing table.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
    • CaseInsensitiveSet

      public CaseInsensitiveSet(int initialCapacity, float loadFactor)
      Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table.
      Parameters:
      initialCapacity - If not a power of two, it is increased to the next nearest power of two.
      loadFactor - what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
    • CaseInsensitiveSet

      public CaseInsensitiveSet(Iterator<? extends CharSequence> coll)
      Creates a new instance containing the items in the specified iterator.
      Parameters:
      coll - an iterator that will have its remaining contents added to this
    • CaseInsensitiveSet

      public CaseInsensitiveSet(ObjectSet<? extends CharSequence> set)
      Creates a new set identical to the specified set.
      Parameters:
      set - an ObjectSet or one of its subclasses
    • CaseInsensitiveSet

      public CaseInsensitiveSet(Collection<? extends CharSequence> coll)
      Creates a new set that contains all distinct elements in coll.
      Parameters:
      coll - a Collection implementation, such as an ObjectList
    • CaseInsensitiveSet

      public CaseInsensitiveSet(CharSequence[] array, int offset, int length)
      Creates a new set using length items from the given array, starting at offset (inclusive).
      Parameters:
      array - an array to draw items from
      offset - the first index in array to draw an item from
      length - how many items to take from array; bounds-checking is the responsibility of the using code
    • CaseInsensitiveSet

      public CaseInsensitiveSet(CharSequence[] array)
      Creates a new set containing all the items in the given array.
      Parameters:
      array - an array that will be used in full, except for duplicate items
    • CaseInsensitiveSet

      public CaseInsensitiveSet(CaseInsensitiveSet set)
      Creates a new set that contains all distinct elements in set.
      Parameters:
      set - another CaseInsensitiveSet
  • Method Details

    • place

      protected int place(Object item)
      Description copied from class: ObjectSet
      Returns an index >= 0 and <= ObjectSet.mask for the specified item, mixed.
      Overrides:
      place in class ObjectSet<CharSequence>
      Parameters:
      item - a non-null Object; its hashCode() method should be used by most implementations
      Returns:
      an index between 0 and ObjectSet.mask (both inclusive)
    • equate

      protected boolean equate(Object left, Object right)
      Description copied from class: ObjectSet
      Compares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returns left.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually, ObjectSet.place(Object) is also overridden if this method is.
      Overrides:
      equate in class ObjectSet<CharSequence>
      Parameters:
      left - must be non-null; typically a key being compared, but not necessarily
      right - may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
      Returns:
      true if left and right are considered equal for the purposes of this class
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<CharSequence>
      Specified by:
      hashCode in interface Set<CharSequence>
      Overrides:
      hashCode in class ObjectSet<CharSequence>
    • resize

      protected void resize(int newSize)
      Overrides:
      resize in class ObjectSet<CharSequence>
    • with

      public static CaseInsensitiveSet with()
      Constructs an empty set. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Returns:
      a new set containing nothing
    • with

      public static CaseInsensitiveSet with(CharSequence item)
      Creates a new CaseInsensitiveSet that holds only the given item, but can be resized.
      Parameters:
      item - one CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given item
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      item2 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      item2 - a CharSequence item
      item3 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      item2 - a CharSequence item
      item3 - a CharSequence item
      item4 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      item2 - a CharSequence item
      item3 - a CharSequence item
      item4 - a CharSequence item
      item5 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      item2 - a CharSequence item
      item3 - a CharSequence item
      item4 - a CharSequence item
      item5 - a CharSequence item
      item6 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6, CharSequence item7)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.
      Parameters:
      item0 - a CharSequence item
      item1 - a CharSequence item
      item2 - a CharSequence item
      item3 - a CharSequence item
      item4 - a CharSequence item
      item5 - a CharSequence item
      item6 - a CharSequence item
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • with

      public static CaseInsensitiveSet with(CharSequence... varargs)
      Creates a new CaseInsensitiveSet that holds only the given items, but can be resized. This overload will only be used when varargs are used and there are 9 or more arguments.
      Parameters:
      varargs - a CharSequence varargs or CharSequence array; remember that varargs allocate
      Returns:
      a new CaseInsensitiveSet that holds the given items
    • parse

      public static CaseInsensitiveSet parse(String str, String delimiter)
      Parameters:
      str - a String that will be parsed in full
      delimiter - the delimiter between items in str
      Returns:
      a new collection parsed from str
    • parse

      public static CaseInsensitiveSet parse(String str, String delimiter, boolean brackets)
      Creates a new collection and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) on either all of str (if brackets is false) or str without its first and last chars (if brackets is true). Each item is expected to be separated by delimiter.
      Parameters:
      str - a String that will be parsed in full (depending on brackets)
      delimiter - the delimiter between items in str
      brackets - if true, the first and last chars in str will be ignored
      Returns:
      a new collection parsed from str
    • parse

      public static CaseInsensitiveSet parse(String str, String delimiter, int offset, int length)
      Creates a new collection and fills it by calling EnhancedCollection.addLegible(String, String, PartialParser, int, int) with the given five parameters as-is.
      Parameters:
      str - a String that will have the given section parsed
      delimiter - the delimiter between items in str
      offset - the first position to parse in str, inclusive
      length - how many chars to parse, starting from offset
      Returns:
      a new collection parsed from str