Class ODataQuery

java.lang.Object
io.skipcheck.tm1.client.odata.ODataQuery

public final class ODataQuery extends Object
OData query options: $select, $expand (with nested options), $filter, $orderby, $top, $skip, $count, custom options such as TM1's !sandbox, and parameter aliases. Immutable: each method returns a new query.
String query = ODataQuery.none()
	.select("Name", "Type")
	.expand("Hierarchies", h -> h.select("Name").top(5))
	.filter(Filter.startsWith(ODataProperty.of("Name"), "Q"))
	.toQueryString();
// $select=Name,Type&$expand=Hierarchies($select=Name;$top=5)&$filter=startswith(Name,'Q'),
// each value encoded

Options are written in a fixed order, lowercase with $ as OData 4.0 requires, and each value is percent-encoded once by toQueryString(). Nested options inside $expand are separated by ;.

  • Method Details

    • none

      public static ODataQuery none()
      No options: the server's default response.
      Returns:
      the empty query
    • select

      public ODataQuery select(String... properties)
      Adds properties to $select. Like every option here it's an instance method, so it works the same at the top level and inside expand(String, UnaryOperator).
      Parameters:
      properties - the property paths, added after any already selected
      Returns:
      a new query
    • select

      public ODataQuery select(ODataProperty... properties)
      Adds properties to $select.
      Parameters:
      properties - the properties, added after any already selected
      Returns:
      a new query
    • expand

      public ODataQuery expand(String navigation)
      Expands a navigation property with no nested options.
      Parameters:
      navigation - the navigation property's name
      Returns:
      a new query
    • expand

      public ODataQuery expand(String navigation, UnaryOperator<ODataQuery> options)
      Expands a navigation property, with nested options: expand("Hierarchies", h -> h.select("Name")).
      Parameters:
      navigation - the navigation property's name
      options - adds the nested options to the query it's given: the empty query, or the nested options so far if navigation is already expanded
      Returns:
      a new query
    • expand

      public ODataQuery expand(ODataProperty navigation)
      Expands a navigation property with no nested options: expand(Cube.DIMENSIONS).
      Parameters:
      navigation - the navigation property
      Returns:
      a new query
    • expand

      public ODataQuery expand(ODataProperty navigation, UnaryOperator<ODataQuery> options)
      Expands a navigation property, with nested options: expand(Dimension.HIERARCHIES, h -> h.select(Hierarchy.NAME)).
      Parameters:
      navigation - the navigation property
      options - adds the nested options to the query it's given, as for expand(String, UnaryOperator)
      Returns:
      a new query
    • filter

      public ODataQuery filter(Filter filter)
      Sets $filter.
      Parameters:
      filter - the filter, replacing any set before
      Returns:
      a new query
    • orderBy

      public ODataQuery orderBy(String property)
      Adds an ascending $orderby key.
      Parameters:
      property - the property path, after any keys already added
      Returns:
      a new query
    • orderByDescending

      public ODataQuery orderByDescending(String property)
      Adds a descending $orderby key.
      Parameters:
      property - the property path, after any keys already added
      Returns:
      a new query
    • orderBy

      public ODataQuery orderBy(ODataProperty property)
      Adds an ascending $orderby key: orderBy(Element.NAME).
      Parameters:
      property - the property, after any keys already added
      Returns:
      a new query
    • orderByDescending

      public ODataQuery orderByDescending(ODataProperty property)
      Adds a descending $orderby key.
      Parameters:
      property - the property, after any keys already added
      Returns:
      a new query
    • top

      public ODataQuery top(int n)
      Sets $top: at most this many items.
      Parameters:
      n - the maximum number of items, zero or more
      Returns:
      a new query
      Throws:
      IllegalArgumentException - if n is negative
    • skip

      public ODataQuery skip(int n)
      Sets $skip: skip this many items first.
      Parameters:
      n - the number of items to skip, zero or more
      Returns:
      a new query
      Throws:
      IllegalArgumentException - if n is negative
    • withCount

      public ODataQuery withCount()
      Sets $count=true: include the total number of matching items.
      Returns:
      a new query
    • option

      public ODataQuery option(String name, String value)
      Sets a custom query option, such as TM1's !sandbox. Custom options must not start with $ or @. Setting one again replaces it.
      Parameters:
      name - the option's name, e.g. !sandbox
      value - the option's value, unencoded
      Returns:
      a new query
      Throws:
      IllegalArgumentException - if name isn't a valid custom option name
    • alias

      public ODataQuery alias(ODataPath.Alias alias, String literalOrJson)
      Defines a parameter alias for a function call: @p=<literal or JSON>, written as given.
      Parameters:
      alias - the alias, as used in the function call
      literalOrJson - the alias's value, an OData literal or JSON, unencoded
      Returns:
      a new query
    • isEmpty

      public boolean isEmpty()
      True if this query has no options.
      Returns:
      true if there are no options
    • toQueryString

      public String toQueryString()
      The encoded query string, without a leading ?; empty if there are no options.
      Returns:
      the encoded query string, or an empty string
    • toString

      public String toString()
      The query unencoded, for logs and messages.
      Overrides:
      toString in class Object
    • equals

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

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

      public @Nullable String option(String name)
      The custom option's value, e.g. the current !sandbox, if set.
      Parameters:
      name - the option's name
      Returns:
      the option's value, unencoded, or null if it isn't set