java.lang.Object
ch.tocco.nice2.persist.core.api.entity.Path
All Implemented Interfaces:
Serializable

public final class Path extends Object implements Serializable
Path class for working with entity field and relation paths.

 Example usage:
   Path path = Path.parse("someRel.anotherRel.aField");
   FieldModel fieldModel = (FieldModel) path.resolve(myEntityModelThatHasSomeRel);
 

Note: Features are turned off by default!

 Example with features:
   Path path = Path.parse("someRel[myselector].anotherRel?.aField", Path.Feature.SELECTORS, Path.Feature.OPTIONALS)
 
See Also:
  • Constructor Details

  • Method Details

    • parse

      public static Path parse(String path) throws IllegalArgumentException
      Parses the given string path into a path object and returns it.

      NOTE that this method is not too picky and does not try to resolve; invalid stuff will be detected in the resolve(ch.tocco.nice2.model.entity.api.EntityModel, ch.tocco.nice2.persist.core.api.entity.Path.Feature...) method.

      Chunks that are given single-quoted like 'foo' lose the quotes here, calls to get(int) etc. will be without.

      Parameters:
      path - For example "someRel.someField" or "'someRel'.'someField'". Will be trimmed, may not be empty.
      Returns:
      The parsed path object
      Throws:
      IllegalArgumentException - On illegal syntax or empty input.
    • parse

      public static Path parse(String path, Path.Feature... features) throws IllegalArgumentException
      Throws:
      IllegalArgumentException
    • getFullPaths

      public List<Path> getFullPaths()
      Returns:
      all paths (including all sub paths rewritten as a full path)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      A machine-usable, re-parsable representation of the path, which happens to be very human-readable too.
    • toString

      public String toString(boolean quoteIdentifiers)
    • length

      public int length()
    • get

      public Path.PathElement get(int index)
    • getHead

      public Path.PathElement getHead()
    • getHead

      public Path getHead(int count)
    • getTail

      public Path.PathElement getTail()
    • getTail

      public Path getTail(int count)
    • stepUp

      public Path stepUp()
      Example: "aa.bb.cc" → "aa.bb"
      Returns:
      A new path without the last element.
      Throws:
      IndexOutOfBoundsException - If the path contains just one element.
    • stepDown

      public Path stepDown()
      Example: "aa.bb.cc" → "bb.cc"
      Returns:
      A new path without the first element.
      Throws:
      IndexOutOfBoundsException - If the path contains just one element.
    • hasFeature

      public boolean hasFeature(Path.Feature feature)
    • stripFeatures

      public Path stripFeatures(Path.Feature... features)
    • resolve

      public ModelElement resolve(EntityModel source, Path.Feature... features)
      Resolves the last element of this path and returns its model.
      Parameters:
      source - The model of the first element to start resolving.
      features - Enabled path features.
      Returns:
      Either an instance of FieldModel or RelationModel.
      Throws:
      PersistException - On a syntax or model error. Never contacts the persistence layer so it's more of a model exception.
    • resolveToLastEntity

      public Path.LastEntityResult resolveToLastEntity(EntityModel source, Path.Feature... features)
      Resolves the path to the last entity, returning that entity's model plus the path element after it.

      Example:

         Path "someRel.otherRel.someField"
          - The returned EntityModel is the target entity of the "otherRel" relation.
          - The returned PathElement is for the "someField", which is either for a standard field,
            for a localized field, or for a relation. If it is for a localized field then it may
            be just its base name.
       
      Parameters:
      source - The model of the first element to start resolving.
      features - Enabled path features.
      Throws:
      PersistException - On a syntax or model error. Never contacts the persistence layer so it's more of a model exception.
    • resolve

      public <T extends Path.Resolver> T resolve(EntityModel source, T resolver, Path.Feature... features)
      Resolves this path using a resolver.
      Parameters:
      source - The model of the first element to start resolving.
      resolver - A handler that handlers each step in the path.
      features - Enabled path features.
      Returns:
      The passed in resolver.
      Throws:
      PersistException - On a syntax or model error. Never contacts the persistence layer so it's more of a model exception.