Class RelTraitSet

All Implemented Interfaces:
Iterable<RelTrait>, Collection<RelTrait>, List<RelTrait>

public final class RelTraitSet extends AbstractList<RelTrait>
RelTraitSet represents an ordered set of RelTraits.
  • Method Details

    • 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:
      ArrayIndexOutOfBoundsException - if index greater than or equal to size() or less than 0.
    • getTraits

      public <E extends RelMultipleTrait> 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:
      ArrayIndexOutOfBoundsException - if index greater than or equal to size() or less than 0.
    • get

      public RelTrait get(int index)
      Specified by:
      get in interface List<RelTrait>
      Specified by:
      get in class 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> @Nullable 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> @Nullable 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:
    • replace

      public <T extends RelMultipleTrait> RelTraitSet replace(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 RelMultipleTrait> RelTraitSet replace(RelTraitDef<T> def, 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 RelMultipleTrait> RelTraitSet replaceIfs(RelTraitDef<T> def, Supplier<? extends @Nullable List<T>> traitSupplier)
      If a given multiple trait is enabled, replaces it by calling the given function.
    • replaceIf

      public <T extends RelTrait> RelTraitSet replaceIf(RelTraitDef<T> def, Supplier<? extends @Nullable T> traitSupplier)
      If a given trait is enabled, replaces it by calling the given function.
    • apply

      public RelTraitSet apply(Mappings.TargetMapping mapping)
      Applies a mapping to this traitSet.
      Parameters:
      mapping - Mapping
      Returns:
      traitSet with mapping applied
    • isDefault

      public boolean isDefault()
      Returns whether all the traits are default trait value.
    • isDefaultSansConvention

      public boolean isDefaultSansConvention()
      Returns whether all the traits except Convention are default trait value.
    • equalsSansConvention

      public boolean equalsSansConvention(RelTraitSet other)
      Returns whether all the traits except Convention equals with traits in other traitSet.
    • getDefault

      public RelTraitSet getDefault()
      Returns a new traitSet with same traitDefs with current traitSet, but each trait is the default trait value.
    • getDefaultSansConvention

      public RelTraitSet getDefaultSansConvention()
      Returns a new traitSet with same traitDefs with current traitSet, but each trait except Convention is the default trait value. Convention trait remains the same with current traitSet.
    • getConvention

      public @Nullable Convention getConvention()
      Returns Convention trait defined by ConventionTraitDef.INSTANCE, or null if the ConventionTraitDef.INSTANCE is not registered in this traitSet.
    • getDistribution

      public <T extends RelDistribution> @Nullable T getDistribution()
      Returns RelDistribution trait defined by RelDistributionTraitDef.INSTANCE, or null if the RelDistributionTraitDef.INSTANCE is not registered in this traitSet.
    • getCollation

      public <T extends RelCollation> @Nullable T getCollation()
      Returns RelCollation trait defined by RelCollationTraitDef.INSTANCE, or null if the RelCollationTraitDef.INSTANCE is not registered in this traitSet.
    • size

      public int size()
      Returns the size of the RelTraitSet.
      Specified by:
      size in interface Collection<RelTrait>
      Specified by:
      size in interface List<RelTrait>
      Specified by:
      size in class 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(@Nullable Object obj)
      Compares two RelTraitSet objects for equality.
      Specified by:
      equals in interface Collection<RelTrait>
      Specified by:
      equals in interface List<RelTrait>
      Overrides:
      equals in class 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 Collection<RelTrait>
      Specified by:
      hashCode in interface List<RelTrait>
      Overrides:
      hashCode in class 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:
    • 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 String toString()
      Overrides:
      toString in class AbstractCollection<RelTrait>
    • 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
    • plusAll

      public RelTraitSet plusAll(RelTrait[] traits)
    • merge

      public RelTraitSet merge(RelTraitSet additionalTraits)
    • 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.