Class RelTraitSet

  • All Implemented Interfaces:
    java.lang.Iterable<RelTrait>, java.util.Collection<RelTrait>, java.util.List<RelTrait>

    public final class RelTraitSet
    extends java.util.AbstractList<RelTrait>
    RelTraitSet represents an ordered set of RelTraits.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  RelTraitSet.Cache
      Cache of trait sets.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private RelTraitSet​(RelTraitSet.Cache cache, RelTrait[] traits)
      Constructs a RelTraitSet with the given set of RelTraits.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean allSimple()
      Returns whether there are any composite traits in this set.
      <T extends RelTrait>
      T
      canonize​(T trait)
      Converts a trait to canonical form.
      boolean comprises​(RelTrait... relTraits)
      Returns whether this trait set comprises precisely the list of given traits.
      protected java.lang.String computeString()
      Outputs the traits of this set as a String.
      boolean contains​(RelTrait trait)
      Returns whether this trait set contains a given trait.
      boolean containsIfApplicable​(RelTrait trait)
      Returns whether this trait set contains the given trait, or whether the trait is not present because its RelTraitDef is not enabled.
      private static <T> boolean containsShallow​(T[] ts, RelTrait seek)
      Returns whether an element occurs within an array.
      static RelTraitSet createEmpty()
      Creates an empty trait set.
      com.google.common.collect.ImmutableList<RelTrait> difference​(RelTraitSet traitSet)
      Returns a list of traits that are in traitSet but not in this RelTraitSet.
      boolean equals​(java.lang.Object obj)
      Compares two RelTraitSet objects for equality.
      private int findIndex​(RelTraitDef traitDef)
      Finds the index of a trait of a given type in this set.
      RelTrait get​(int index)  
      RelTrait getTrait​(int index)
      Retrieves a RelTrait from the set.
      <T extends RelTrait>
      T
      getTrait​(RelTraitDef<T> traitDef)
      Retrieves a RelTrait of the given type from the set.
      <E extends RelMultipleTrait>
      java.util.List<E>
      getTraits​(int index)
      Retrieves a list of traits from the set.
      <T extends RelMultipleTrait>
      java.util.List<T>
      getTraits​(RelTraitDef<T> traitDef)
      Retrieves a list of traits of the given type from the set.
      int hashCode()  
      <T extends RelTrait>
      boolean
      isEnabled​(RelTraitDef<T> traitDef)
      Returns whether a given kind of trait is enabled.
      boolean matches​(RelTraitSet that)
      Compares two RelTraitSet objects to see if they match for the purposes of firing a rule.
      RelTraitSet merge​(RelTraitSet additionalTraits)  
      RelTraitSet plus​(RelTrait trait)
      Returns this trait set with a given trait added or overridden.
      RelTraitSet plusAll​(RelTrait[] traits)  
      RelTraitSet replace​(int index, RelTrait trait)
      Replaces an existing RelTrait in the set.
      <T extends RelMultipleTrait>
      RelTraitSet
      replace​(java.util.List<T> traits)
      Replaces the trait(s) of a given type with a list of traits of the same type.
      RelTraitSet replace​(RelTrait trait)
      Returns a trait set consisting of the current set plus a new trait.
      <T extends RelMultipleTrait>
      RelTraitSet
      replace​(RelTraitDef<T> def, java.util.List<T> traits)
      Replaces the trait(s) of a given type with a list of traits of the same type.
      <T extends RelTrait>
      RelTraitSet
      replaceIf​(RelTraitDef<T> def, java.util.function.Supplier<T> traitSupplier)
      If a given trait is enabled, replaces it by calling the given function.
      <T extends RelMultipleTrait>
      RelTraitSet
      replaceIfs​(RelTraitDef<T> def, java.util.function.Supplier<java.util.List<T>> traitSupplier)
      If a given multiple trait is enabled, replaces it by calling the given function.
      boolean satisfies​(RelTraitSet that)
      Returns whether this trait set satisfies another trait set.
      RelTraitSet simplify()
      Returns a trait set similar to this one but with all composite traits flattened.
      int size()
      Returns the size of the RelTraitSet.
      java.lang.String toString()  
      • Methods inherited from class java.util.AbstractList

        add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
      • Methods inherited from class java.util.AbstractCollection

        addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
    • Field Detail

      • EMPTY_TRAITS

        private static final RelTrait[] EMPTY_TRAITS
      • traits

        private final RelTrait[] traits
      • string

        private final java.lang.String string
    • Constructor Detail

      • RelTraitSet

        private RelTraitSet​(RelTraitSet.Cache cache,
                            RelTrait[] traits)
        Constructs a RelTraitSet with the given set of RelTraits.
        Parameters:
        cache - Trait set cache (and indirectly cluster) that this set belongs to
        traits - Traits
    • Method Detail

      • createEmpty

        public static RelTraitSet createEmpty()
        Creates an empty trait set.

        It has a new cache, which will be shared by any trait set created from it. Thus each empty trait set is the start of a new ancestral line.

      • getTrait

        public RelTrait getTrait​(int index)
        Retrieves a RelTrait from the set.
        Parameters:
        index - 0-based index into ordered RelTraitSet
        Returns:
        the RelTrait
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if index greater than or equal to size() or less than 0.
      • getTraits

        public <E extends RelMultipleTrait> java.util.List<E> getTraits​(int index)
        Retrieves a list of traits from the set.
        Parameters:
        index - 0-based index into ordered RelTraitSet
        Returns:
        the RelTrait
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if index greater than or equal to size() or less than 0.
      • get

        public RelTrait get​(int index)
        Specified by:
        get in interface java.util.List<RelTrait>
        Specified by:
        get in class java.util.AbstractList<RelTrait>
      • isEnabled

        public <T extends RelTrait> boolean isEnabled​(RelTraitDef<T> traitDef)
        Returns whether a given kind of trait is enabled.
      • getTrait

        public <T extends RelTrait> T getTrait​(RelTraitDef<T> traitDef)
        Retrieves a RelTrait of the given type from the set.
        Parameters:
        traitDef - the type of RelTrait to retrieve
        Returns:
        the RelTrait, or null if not found
      • getTraits

        public <T extends RelMultipleTrait> java.util.List<T> getTraits​(RelTraitDef<T> traitDef)
        Retrieves a list of traits of the given type from the set.

        Only valid for traits that support multiple entries. (E.g. collation.)

        Parameters:
        traitDef - the type of RelTrait to retrieve
        Returns:
        the RelTrait, or null if not found
      • replace

        public RelTraitSet replace​(int index,
                                   RelTrait trait)
        Replaces an existing RelTrait in the set. Returns a different trait set; does not modify this trait set.
        Parameters:
        index - 0-based index into ordered RelTraitSet
        trait - the new RelTrait
        Returns:
        the old RelTrait at the index
      • replace

        public RelTraitSet replace​(RelTrait trait)
        Returns a trait set consisting of the current set plus a new trait.

        If the set does not contain a trait of the same RelTraitDef, the trait is ignored, and this trait set is returned.

        Parameters:
        trait - the new trait
        Returns:
        New set
        See Also:
        plus(RelTrait)
      • containsShallow

        private static <T> boolean containsShallow​(T[] ts,
                                                   RelTrait seek)
        Returns whether an element occurs within an array.

        Uses ==, not equals(java.lang.Object). Nulls are allowed.

      • replace

        public <T extends RelMultipleTraitRelTraitSet replace​(java.util.List<T> traits)
        Replaces the trait(s) of a given type with a list of traits of the same type.

        The list must not be empty, and all traits must be of the same type.

      • replace

        public <T extends RelMultipleTraitRelTraitSet replace​(RelTraitDef<T> def,
                                                                java.util.List<T> traits)
        Replaces the trait(s) of a given type with a list of traits of the same type.

        The list must not be empty, and all traits must be of the same type.

      • replaceIfs

        public <T extends RelMultipleTraitRelTraitSet replaceIfs​(RelTraitDef<T> def,
                                                                   java.util.function.Supplier<java.util.List<T>> traitSupplier)
        If a given multiple trait is enabled, replaces it by calling the given function.
      • replaceIf

        public <T extends RelTraitRelTraitSet replaceIf​(RelTraitDef<T> def,
                                                          java.util.function.Supplier<T> traitSupplier)
        If a given trait is enabled, replaces it by calling the given function.
      • size

        public int size()
        Returns the size of the RelTraitSet.
        Specified by:
        size in interface java.util.Collection<RelTrait>
        Specified by:
        size in interface java.util.List<RelTrait>
        Specified by:
        size in class java.util.AbstractCollection<RelTrait>
        Returns:
        the size of the RelTraitSet.
      • canonize

        public <T extends RelTrait> T canonize​(T trait)
        Converts a trait to canonical form.

        After canonization, t1.equals(t2) if and only if t1 == t2.

        Parameters:
        trait - Trait
        Returns:
        Trait in canonical form
      • equals

        public boolean equals​(java.lang.Object obj)
        Compares two RelTraitSet objects for equality.
        Specified by:
        equals in interface java.util.Collection<RelTrait>
        Specified by:
        equals in interface java.util.List<RelTrait>
        Overrides:
        equals in class java.util.AbstractList<RelTrait>
        Parameters:
        obj - another RelTraitSet
        Returns:
        true if traits are equal and in the same order, false otherwise
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<RelTrait>
        Specified by:
        hashCode in interface java.util.List<RelTrait>
        Overrides:
        hashCode in class java.util.AbstractList<RelTrait>
      • satisfies

        public boolean satisfies​(RelTraitSet that)
        Returns whether this trait set satisfies another trait set.

        For that to happen, each trait satisfies the corresponding trait in the other set. In particular, each trait set satisfies itself, because each trait subsumes itself.

        Intuitively, if a relational expression is needed that has trait set S (A, B), and trait set S1 (A1, B1) subsumes S, then any relational expression R in S1 meets that need.

        For example, if we need a relational expression that has trait set S = {enumerable convention, sorted on [C1 asc]}, and R has {enumerable convention, sorted on [C3], [C1, C2]}. R has two sort keys, but one them [C1, C2] satisfies S [C1], and that is enough.

        Parameters:
        that - another RelTraitSet
        Returns:
        whether this trait set satisfies other trait set
        See Also:
        RelTrait.satisfies(RelTrait)
      • matches

        public boolean matches​(RelTraitSet that)
        Compares two RelTraitSet objects to see if they match for the purposes of firing a rule. A null RelTrait within a RelTraitSet indicates a wildcard: any RelTrait in the other RelTraitSet will match. If one RelTraitSet is smaller than the other, comparison stops when the last RelTrait from the smaller set has been examined and the remaining RelTraits in the larger set are assumed to match.
        Parameters:
        that - another RelTraitSet
        Returns:
        true if the RelTraitSets match, false otherwise
      • contains

        public boolean contains​(RelTrait trait)
        Returns whether this trait set contains a given trait.
        Parameters:
        trait - Sought trait
        Returns:
        Whether set contains given trait
      • containsIfApplicable

        public boolean containsIfApplicable​(RelTrait trait)
        Returns whether this trait set contains the given trait, or whether the trait is not present because its RelTraitDef is not enabled. Returns false if another trait of the same RelTraitDef is present.
        Parameters:
        trait - Trait
        Returns:
        Whether trait is present, or is absent because disabled
      • comprises

        public boolean comprises​(RelTrait... relTraits)
        Returns whether this trait set comprises precisely the list of given traits.
        Parameters:
        relTraits - Traits
        Returns:
        Whether this trait set's traits are the same as the argument
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractCollection<RelTrait>
      • computeString

        protected java.lang.String computeString()
        Outputs the traits of this set as a String. Traits are output in order, separated by periods.
      • findIndex

        private int findIndex​(RelTraitDef traitDef)
        Finds the index of a trait of a given type in this set.
        Parameters:
        traitDef - Sought trait definition
        Returns:
        index of trait, or -1 if not found
      • plus

        public RelTraitSet plus​(RelTrait trait)
        Returns this trait set with a given trait added or overridden. Does not modify this trait set.
        Parameters:
        trait - Trait
        Returns:
        Trait set with given trait
      • difference

        public com.google.common.collect.ImmutableList<RelTrait> difference​(RelTraitSet traitSet)
        Returns a list of traits that are in traitSet but not in this RelTraitSet.
      • allSimple

        public boolean allSimple()
        Returns whether there are any composite traits in this set.
      • simplify

        public RelTraitSet simplify()
        Returns a trait set similar to this one but with all composite traits flattened.