java.lang.Object
dev.getelements.elements.sdk.cluster.path.Path
All Implemented Interfaces:
HasNodeId, Serializable

public final class Path extends Object implements Serializable, HasNodeId
Represents the path scheme for use in the server. This implements HasNodeId which uses the getContext() to attempt to derive the NodeId or throw an exception if the context does not produce a valid NodeId. If the path has a wildcard context, then it returns a null NodeId. Created by patricktwohig on 9/4/15.
See Also:
  • Field Details

    • CONTEXT_SEPARATOR

      public static final String CONTEXT_SEPARATOR
      The separator of the context from the path components. Literal value "://", e.g. "myContext://foo/bar".
      See Also:
    • PATH_SEPARATOR

      public static final String PATH_SEPARATOR
      The path separator. Literal value "/"
      See Also:
    • EXTENSION_SEPARATOR

      public static final String EXTENSION_SEPARATOR
      The extension separator.
      See Also:
    • WILDCARD

      public static final String WILDCARD
      The wildcard character. Literal value "*"
      See Also:
    • WILDCARD_RECURSIVE

      public static final String WILDCARD_RECURSIVE
      The recursive-wildcard character. Literal value "**"
      See Also:
    • WILDCARD_CONTEXT_REPRESENTATION

      public static final String WILDCARD_CONTEXT_REPRESENTATION
      Deprecated.
      The representation for the wildcard context. Literal value "*", e.g. "*://foo/bar".
      See Also:
    • VALID_PATH_COMPONENT

      public static final Pattern VALID_PATH_COMPONENT
      A Pattern to match valid path components.
    • ENCODING

      public static final Charset ENCODING
      The default encoding for converting a Path into an array of bytes.
  • Constructor Details

    • Path

      public Path()
    • Path

      public Path(String path)
      Parses the path into components and checks for hte wildcard character.
      Parameters:
      path - the path as represented by a String
    • Path

      public Path(Path parent, Path path)
      Creates a Path with the path relative to the given path.
      Parameters:
      parent - the parent path
      path - the path
    • Path

      public Path(List<String> components)
      Constructs a Path from the supplied components.
      Parameters:
      components - the list of components
    • Path

      public Path(String context, List<String> components)
      Creates a path with components and the wildcard flag.
      Parameters:
      context - the context
      components - the path components
    • Path

      public Path(Path.ContextAndComponents contextAndComponents)
      Creates a path with the supplied Path.ContextAndComponents
      Parameters:
      contextAndComponents - the Path.ContextAndComponents
  • Method Details

    • valueOf

      public static Path valueOf(String string)
      Implements the conventional valueOf method by invoking Path(java.lang.String).
      Parameters:
      string - the path to parse
      Returns:
      the Path valueOf
    • formatPath

      public static Path formatPath(String fmt, Object... fmtArgs)
      Formats a Path.
      Parameters:
      fmt -
      fmtArgs -
      Returns:
    • append

      public Path append(Path otherPath)
      Appends the following path to this path such that the final path is expressed as follows: newPath = this/otherPath
      Parameters:
      otherPath - the other path to append
      Returns:
      a new Path, appending the components of this path
    • appendComponents

      public Path appendComponents(String first)
      Appends components to the path and returns a new Path.
      Parameters:
      first - the first component to add
      Returns:
      the newly created path.
    • appendComponents

      public Path appendComponents(String first, String... subsequent)
      Appends components to the path and returns a new Path.
      Parameters:
      first - the first component to add
      subsequent - the subsequent components
      Returns:
      the newly created path.
    • appendIfWildcard

      public Path appendIfWildcard(Supplier<String> stringSupplier)
      Appends a single component if this path ends in a wildcard or recursive wildcard, then this method will return a new Path with the result of the Supplier. Otherwise, this method will return this instance as-is. This will avoid invoking the supplier in case the supplier exhausts a system resource, sou
      Parameters:
      stringSupplier - a supplier for the component to add.
      Returns:
      the newly created path.
    • appendUUIDIfWildcard

      public Path appendUUIDIfWildcard()
      Appends a UUID component if this path is a wildcard path.
      Returns:
      the newly created path.
    • appendExtension

      public Path appendExtension(String extension)
      Appends an extension using the EXTENSION_SEPARATOR.
      Parameters:
      extension - the extension
      Returns:
      a new Path, applying the supplied extension
    • appendExtension

      public Path appendExtension(String extension, String pathSeparator)
      Appends an extension to this Path, using the supplied separator. The final resulting Path is the result of appending the separator and the extension to the last component of the string.
      Parameters:
      extension - the extension
      pathSeparator - the separator
      Returns:
      a new Path, applying the supplied extension
    • parent

      public Path parent()
      Returns a Path that is the parent to this Path, preserving the context (if any). If this path is the root path (ie having no components), then this will return this object.
      Returns:
      the parent Path, or this if this is a root path
    • toWildcardRecursive

      public Path toWildcardRecursive()
      Returns a Path which is a recursive wildcard if this instance is not a recursive wildcard. If the path is already a wildcard recursive, then this path will
      Returns:
      this Path
    • isRoot

      public boolean isRoot()
      Returns true if this is a root path (ie having no components).
      Returns:
      true if this is a root path
    • contextRootPath

      public Path contextRootPath()
      Returns the root path of this one, preserving context.
      Returns:
      the root path
    • getContext

      public String getContext()
      Gets the context of this Path, or null if no context exists.
      Returns:
      the context or null
    • hasContext

      public boolean hasContext()
      Returns true if this Path has a context.
      Returns:
      true if this Path has a context
    • isWildcardContext

      public boolean isWildcardContext()
      Returns true if this Path both has a context and that context is a wildcard context.
      Returns:
      true if the context is a wildcard context
    • getComponent

      public String getComponent(int index)
      Gets the component of a Path at the supplied index. Additionally, this allows for negative numbers indicating the reverse-order index of the list.
      Parameters:
      index - the index or reverse-index specified by a netagive integer
    • getComponents

      public List<String> getComponents()
      Gets the components of this path.
      Returns:
      the components of this path
    • getContextAndComponents

      public Path.ContextAndComponents getContextAndComponents()
      Gets the Path.ContextAndComponents instance for this path.
      Returns:
      the Path.ContextAndComponents instance
    • streamWildcardIndices

      public IntStream streamWildcardIndices()
      Gets an IntStream of all wild card indices.
      Returns:
      the wildcard indices
    • getWildcardIndices

      public List<Integer> getWildcardIndices()
    • isWildcard

      public boolean isWildcard()
      True if the path is a wildcard.
      Returns:
      true if wildcard, false otherwise
    • isWildcardTerminated

      public boolean isWildcardTerminated()
      True if the path is a wildcard.
      Returns:
      true if wildcard, false otherwise
    • isWildcardRecursive

      public boolean isWildcardRecursive()
      True if the path is a recursive wildcard.
      Returns:
      true if wildcard, false otherwise
    • stripWildcard

      public Path stripWildcard(int wildcardIndex)
      Returns a new Path which strips all components up to the wildcard index. The wildcard index is the zero indexed n'th position of the Path. The index may be negative, indicating the wildcard index will be stripped from the end of the indices. If the path has no wildcard components, then this will simply return this object.
      Parameters:
      wildcardIndex - >= 0 for the n'th from beginning, <0 for the n'th index from the end
      Returns:
      the Path
    • stripWildcardRecursive

      public Path stripWildcardRecursive()
      Returns this Path as a non-wildcard path. If the path is not a wildcard, this will simply return this object.
      Returns:
      this path, stripping the wildcard.
    • matches

      public boolean matches(Path other)
      Checks if this path matches the other path. Note that this considers wildcards whereas the hashCode() and equals(Object) methods determine absolute equality.
      Parameters:
      other - the other path
      Returns:
      true if this path matches the other
    • toNormalizedPathString

      public String toNormalizedPathString()
      Returns the normalized path string with context and default path separator using PATH_SEPARATOR and including context.
      Returns:
      the normalized path as a string
    • toFileSystemPathString

      @Deprecated public String toFileSystemPathString()
      Deprecated.
      Returns the String representation of this Path as a file system path using File.separator and not including context.
      Returns:
      the string representation
    • toRelativeFilesystemPath

      public Path toRelativeFilesystemPath()
      Converts this path to a relative FS path.
      Returns:
      the Path
    • toRelativePathString

      public String toRelativePathString(String pathSeparator)
      If this Path has no context, then this will return the path string w/ a path separator.
      Parameters:
      pathSeparator - the path separator
      Returns:
      a String representing the relative portion of this path.
    • toNormalizedPathString

      public String toNormalizedPathString(String pathSeparator)
      Returns the normalized path string. Note that toString() does not return a properly formatted path. But rather a path useful for debugging and logging information.
      Returns:
      the normalized path as a string
    • toNormalizedPathString

      public String toNormalizedPathString(String pathSeparator, boolean shouldIncludeContext)
      Converts this Path to a String representing the path. Optionally including the context.
      Parameters:
      pathSeparator - the path separator
      shouldIncludeContext - true if the context should be included
      Returns:
      the String representing the path
    • toPathWithContext

      public Path toPathWithContext(String newContext)
      Returns a Path which will have the context specified.
      Parameters:
      newContext - the context
      Returns:
      the Path, or this if the context matches
      Throws:
      IllegalArgumentException - if the context mismatches
    • toPathWithContextIfAbsent

      public Path toPathWithContextIfAbsent(HasNodeId hasNodeId)
      Returns a Path which will have the context specified.
      Parameters:
      hasNodeId - the HasNodeId instance
      Returns:
      the Path, or this if the context matches
      Throws:
      IllegalArgumentException - if the context mismatches
    • toPathWithContextIfAbsent

      public Path toPathWithContextIfAbsent(String newContext)
      Returns a Path which will have the context specified.
      Parameters:
      newContext - the context
      Returns:
      the Path, or this if the context matches
      Throws:
      IllegalArgumentException - if the context mismatches
    • toPathWithoutContext

      public Path toPathWithoutContext()
      Returns a Path without any context.
      Returns:
      the Path with no context
    • toPathWithNodeId

      public Path toPathWithNodeId(HasNodeId hasNodeId)
      Returns a Path with the supplied HasNodeId, throwing an exception if the supplied HasNodeId cannot produce a NodeId.
      Parameters:
      hasNodeId - the HasNodeId instance
      Returns:
      a new Path with the NodeId context
    • getNodeId

      public NodeId getNodeId() throws InvalidNodeIdException
      Description copied from interface: HasNodeId
      Returns a NodeId for this instance, throwing an exception if the NodeId is not valid. This may return null to indicate that the NodeId is neither valid nor invalid, but rather simply not present.
      Specified by:
      getNodeId in interface HasNodeId
      Returns:
      the NodeId or null.
      Throws:
      InvalidNodeIdException - if this instance is unable to derive a NodeId
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toByteArray

      public byte[] toByteArray()
      Gets a byte[] representation of this Path.
      Returns:
      the byte array
    • toByteBuffer

      public ByteBuffer toByteBuffer()
      Converts this Path to a ByteBuffer.
      Returns:
      this, as a byte buffer
    • equals

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

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

      public static Path fromComponents(String... components)
      Converts the supplied components to a Path.
      Parameters:
      components - the components in the Path
      Returns:
      the Path
    • fromContextAndComponents

      public static Path fromContextAndComponents(String context, String... components)
      Gets the a Path from the supplied context and components.
      Parameters:
      context - the context
      components - the components
      Returns:
      the Path instance
    • fromContextAndComponents

      public static Path fromContextAndComponents(HasNodeId hasNodeId, String... components)
      Gets the a Path from the supplied context and components.
      Parameters:
      hasNodeId - the HasNodeId from which to derive the context string
      components - the components
      Returns:
      the Path instance
    • fromBytes

      public static Path fromBytes(byte[] pathBytes)
      Converts the supplied byte array representation to a Path
      Parameters:
      pathBytes - the bytes of the path
      Returns:
      the Path
    • fromByteBuffer

      public static Path fromByteBuffer(ByteBuffer byteBuffer)
      Gets a path from a ByteBuffer
      Parameters:
      byteBuffer - the ByteBuffer
      Returns:
      the Path
    • fromPathString

      public static Path fromPathString(String pathString)
      Converts the supplied string representation of he Path using PATH_SEPARATOR as the separator.
      Parameters:
      pathString - the components in the Path
      Returns:
      the fully formed Path
    • fromPathString

      public static Path fromPathString(String pathString, String pathSeparator)
      Converts the supplied string representation of he Path with the supplied separator string.
      Parameters:
      pathString - the components in the Path
      pathString - the separator string
      Returns:
      the fully formed Path
    • matches

      public static boolean matches(Path lhs, Path rhs)
      Checks if two paths match.
      Parameters:
      lhs -
      rhs -
      Returns:
    • contextAndComponentsFromPath

      public static Path.ContextAndComponents contextAndComponentsFromPath(String path, String pathSeparator)