Class ResourceIdentifier

java.lang.Object
com.palantir.ri.ResourceIdentifier

@Immutable public final class ResourceIdentifier extends Object
Defines a common format for wrapping existing unique identifiers to provide additional context. This class provides utility method: #of(String) to parse an existing identifier or the ability to generate new identifiers by using provided context with method #of(String, String, String).

Resource identifier specification:

Resource Identifiers contain 4 components, prefixed by a format identifier ri and separated with periods: ri.<service>.<instance>.<type>.<locator>

  1. Service: a string that represents the service (or application) that namespaces the rest of the identifier. Must conform with regex pattern [a-z][a-z0-9\-]*
  2. Instance: an optionally empty string that represents a specific service cluster, to allow disambiguation of artifacts from different service clusters. Must conform to regex pattern ([a-z0-9][a-z0-9\-]*)?
  3. Type: a service-specific resource type to namespace a group of locators. Must conform to regex pattern [a-z][a-z0-9\-]*
  4. Locator: a string used to uniquely locate the specific resource. Must conform to regex pattern [a-zA-Z0-9\-\._]+
  • Method Details

    • getService

      public String getService()
      Returns the service component.
      Returns:
      the service component from this identifier
    • getInstance

      public String getInstance()
      Returns the instance component.
      Returns:
      the instance component from this identifier
    • getType

      public String getType()
      Returns the type component.
      Returns:
      the type component from this identifier
    • getLocator

      public String getLocator()
      Returns the locator component.
      Returns:
      the locator component from this identifier
    • hasService

      public boolean hasService(String service)
      Compares the service component of this identifier with the given service. Returns true if and only if the service component this identifier is equal to the given service.
      Parameters:
      service - the service to be compared for equality with the service component of this identifier
      Returns:
      true if the service component of this identifier is equal to the given service, false otherwise
    • hasInstance

      public boolean hasInstance(String instance)
      Compares the instance component of this identifier with the given instance. Returns true if and only if the instance component this identifier is equal to the given instance.
      Parameters:
      instance - the instance to be compared for equality with the instance component of this identifier
      Returns:
      true if the instance component of this identifier is equal to the given instance, false otherwise
    • hasType

      public boolean hasType(String type)
      Compares the type component of this identifier with the given type. Returns true if and only if the type component this identifier is equal to the given type.
      Parameters:
      type - the type to be compared for equality with the type component of this identifier
      Returns:
      true if the type component of this identifier is equal to the given type, false otherwise
    • hasLocator

      public boolean hasLocator(String locator)
      Compares the locator component of this identifier with the given locator. Returns true if and only if the locator component this identifier is equal to the given locator.
      Parameters:
      locator - the locator to be compared for equality with the locator component of this identifier
      Returns:
      true if the locator component of this identifier is equal to the given locator, false otherwise
    • toString

      public String toString()
      Returns a string representation of this ResourceIdentifier. The string representation follows the format specification using the "ri" header followed by the 4 components separated by periods.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this identifier
    • hashCode

      public int hashCode()
      Returns the hash code value for identifier.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code value for this identifier
    • equals

      public boolean equals(Object obj)
      Compares the specified object with this identifier for equality. Returns true if and only if the specified object is also a resource identifier and contain exactly the same values for all 4 components.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to be compared for equality with this identifier
      Returns:
      true if the specified object is equal to this identifier, false otherwise
    • isValid

      public static boolean isValid(@Nullable String rid)
      Checks if the input string is a valid resource identifier as defined in the specification.
      Parameters:
      rid - the input string to be checked
      Returns:
      true if and only if the input satisfy the resource identifier specification, false otherwise.
    • isValidService

      public static boolean isValidService(String service)
      Checks if the input string is a valid service as defined in the specification.
      Parameters:
      service - the input string to be checked
      Returns:
      true if and only if the input satisfy the service specification, false otherwise.
    • isValidInstance

      public static boolean isValidInstance(String instance)
      Checks if the input string is a valid instance as defined in the specification.
      Parameters:
      instance - the input string to be checked
      Returns:
      true if and only if the input satisfy the instance specification, false otherwise.
    • isValidType

      public static boolean isValidType(String type)
      Checks if the input string is a valid type as defined in the specification.
      Parameters:
      type - the input string to be checked
      Returns:
      true if and only if the input satisfy the type specification, false otherwise.
    • isValidLocator

      public static boolean isValidLocator(String locator)
      Checks if the input string is a valid locator as defined in the specification.
      Parameters:
      locator - the input string to be checked
      Returns:
      true if and only if the input satisfy the locator specification, false otherwise.
    • valueOf

      public static ResourceIdentifier valueOf(String rid)
      Same as of(String). Included to support JAX-RS query and path parameters
    • of

      public static ResourceIdentifier of(String rid)
      Generates a new resource identifier object from the input string. This method will validate that the input is a valid resource identifier string as defined by the specification prior to creating the object.
      Parameters:
      rid - the input string to be converted to a resource identifier
      Returns:
      a resource identifier object representing the input string
      Throws:
      IllegalArgumentException - if the input string is not a valid resource identifier
    • of

      public static ResourceIdentifier of(@Safe @Safe String service, @Safe @Safe String instance, @Safe @Safe String type, String locator)
      Generates a new resource identifier object from each of the 4 input components. Each component must satisfy the requirements as defined by the specification.
      Parameters:
      service - input representing the service component
      instance - input representing the instance component
      type - input representing the type component
      locator - input representing the locator component
      Returns:
      a resource identifier object representing the input components
      Throws:
      IllegalArgumentException - if any of the inputs do not satisfy the resource identifier specification
    • of

      public static ResourceIdentifier of(@Safe @Safe String service, @Safe @Safe String instance, @Safe @Safe String type, String firstLocatorComponent, String... locatorComponents)
      Generates a new resource identifier object from each of the input components. The locator component is produced by joining together locatorComponent and locatorComponents with the default separator.
      Parameters:
      service - input representing the service component
      instance - input representing the instance component
      type - input representing the type component
      firstLocatorComponent - the first part of the locator component
      locatorComponents - the remaining locator components
      Returns:
      a resource identifier object representing the input components
      Throws:
      IllegalArgumentException - if any of the inputs do not satisfy the resource identifier specification