APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.dynalink

public final Class NamespaceOperation

extends Object
implements Operation
Class Inheritance
All Implemented Interfaces
jdk.dynalink.Operation
Imports
java.util.Arrays, .Objects

Describes an operation that operates on at least one Namespace of an object. E.g. a property getter would be described as
Operation propertyGetter = new NamespaceOperation(
    StandardOperation.GET,
    StandardNamespace.PROPERTY);
They are often combined with NamedOperation, e.g. to express a property getter for a property named "color", you would construct:
Operation colorPropertyGetter = new NamedOperation(
    new NamespaceOperation(
        StandardOperation.GET,
        StandardNamespace.PROPERTY),
    "color");

While NamespaceOperation can be constructed directly, it is often convenient to use the Operation#withNamespace(Namespace) and Operation#withNamespaces(Namespace...) factory methods instead, e.g.:

Operation getElementOrPropertyEmpty =
    StandardOperation.GET
        .withNamespace(StandardNamespace.PROPERTY)
        .named("color");

Operations on multiple namespaces

If multiple namespaces are specified, the namespaces are treated as alternatives to each other in order of preference. The semantics of such operation is "first applicable". That is, a composite of GET:PROPERTY|ELEMENT:color should be interpreted as get the property named "color" on the object, but if the property does not exist, then get the collection element named "color" instead.

Operations with multiple namespaces are helpful in implementation of languages that don't distinguish between one or more of the namespaces, or when expressing operations against objects that can be considered both ordinary objects and collections, e.g. Java java.util.Map objects. A GET:PROPERTY|ELEMENT:empty operation against a Java map will always match the java.util.Map#isEmpty() property, but GET:ELEMENT|PROPERTY:empty will actually match a map element with key "empty" if the map contains that key, and only fall back to the isEmpty() property getter if the map does not contain the key. If the source language mandates this semantics, it can be easily achieved using operations on multiple namespaces.

Even if the language itself doesn't distinguish between some of the namespaces, it can be helpful to map different syntaxes to different namespace orderings. E.g. the source expression obj.color could map to GET:PROPERTY|ELEMENT|METHOD:color, but a different source expression that looks like collection element access obj[key] could be expressed instead as GET:ELEMENT|PROPERTY|METHOD in order to favor the element semantics. Finally, if the retrieved value is subsequently called, then it makes sense to bring METHOD to the front of the namespace list: the getter part of the source expression obj.color() could be GET:METHOD|PROPERTY|ELEMENT:color and the one for obj[key]() could be GET:METHOD|ELEMENT|PROPERTY.

The base operation of a namespace operation can not itself be a namespace or named operation, but rather one of simple operations such are elements of StandardOperation. A namespace operation itself can serve as the base operation of a named operation, though; a typical way to construct e.g. the GET:ELEMENT|PROPERTY:empty from above would be:

Operation getElementOrPropertyEmpty = StandardOperation.GET
    .withNamespaces(
        StandardNamespace.ELEMENT,
        StandardNamespace.PROPERTY)
    .named("empty");
Since
9

Field Summary

Modifier and TypeField and Description
private final Operation
private final Namespace[]

Constructor Summary

AccessConstructor and Description
public
NamespaceOperation(final Operation
the base operation that operates on one or more namespaces.
baseOperation
,
final Namespace...
one or more namespaces this operation operates on.
namespaces
)

Constructs a new namespace operation.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the if this namespace operation contains a namespace equal to the specified namespace.
contains
(final Namespace
the namespace being searched for. Must not be null.
namespace
)

Returns true if this namespace operation contains a namespace equal to the specified namespace.

public static boolean

Returns:

true if the if the passed operation is a NamespaceOperation, its base operation equals the searched base operation, and contains a namespace equal to the searched namespace.
contains
(final Operation
the operation. Must not be null.
op
,
final Operation
the base operation being searched for. Must not be null.
baseOperation
,
final Namespace
the namespace being searched for. Must not be null.
namespace
)

Returns true if the specified operation is a NamespaceOperation and its base operation is equal to the specified operation, and it contains the specified namespace.

public boolean

Returns:

true if this object is equal to the other one, false otherwise.
equals
(final Object
the object to compare to
obj
)

Overrides java.lang.Object.equals.

Returns true if the other object is also a namespace operation and their base operation and namespaces are equal.

public Operation

Returns:

the base operation of this named operation.
getBaseOperation
()

Returns the base operation of this named operation.

public static Operation

Returns:

the base operation of the passed operation.
getBaseOperation
(final Operation
the operation
op
)

If the passed operation is a namespace operation, returns its getBaseOperation(), otherwise returns the operation as is.

public Namespace

Returns:

the i-th namespace in this namespace operation.
getNamespace
(final int
the namespace index
i
)

Returns the i-th namespace in this namespace operation.

public int

Returns:

the number of namespaces in this namespace operation.
getNamespaceCount
()

Returns the number of namespaces in this namespace operation.

public Namespace[]

Returns:

the namespaces in this namespace operation.
getNamespaces
()

Returns the namespaces in this namespace operation.

public static Namespace[]

Returns:

the namespaces of the passed operation.
getNamespaces
(final Operation
the operation
op
)

If the passed operation is a namespace operation, returns its getNamespaces(), otherwise returns an empty array.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this namespace operation.

public String

Returns:

the string representation of this namespace operation.
toString
()

Overrides java.lang.Object.toString.

Returns the string representation of this namespace operation.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

baseOperationback to summary
private final Operation baseOperation
namespacesback to summary
private final Namespace[] namespaces

Constructor Detail

NamespaceOperationback to summary
public NamespaceOperation(final Operation baseOperation, final Namespace... namespaces)

Constructs a new namespace operation.

Parameters
baseOperation:Operation

the base operation that operates on one or more namespaces.

namespaces:Namespace[]

one or more namespaces this operation operates on.

Exceptions
IllegalArgumentException:
if less than one namespace is specified, or the base operation is itself a NamespaceOperation or a NamedOperation.
NullPointerException:
if either the namespaces array or any of its elements are null, or if baseOperation is null.

Method Detail

containsback to summary
public boolean contains(final Namespace namespace)

Returns true if this namespace operation contains a namespace equal to the specified namespace.

Parameters
namespace:Namespace

the namespace being searched for. Must not be null.

Returns:boolean

true if the if this namespace operation contains a namespace equal to the specified namespace.

containsback to summary
public static boolean contains(final Operation op, final Operation baseOperation, final Namespace namespace)

Returns true if the specified operation is a NamespaceOperation and its base operation is equal to the specified operation, and it contains the specified namespace. If it is not a NamespaceOperation, then it returns false.

Parameters
op:Operation

the operation. Must not be null.

baseOperation:Operation

the base operation being searched for. Must not be null.

namespace:Namespace

the namespace being searched for. Must not be null.

Returns:boolean

true if the if the passed operation is a NamespaceOperation, its base operation equals the searched base operation, and contains a namespace equal to the searched namespace.

equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Returns true if the other object is also a namespace operation and their base operation and namespaces are equal.

Parameters
obj:Object

the object to compare to

Returns:boolean

true if this object is equal to the other one, false otherwise.

Annotations
@Override
getBaseOperationback to summary
public Operation getBaseOperation()

Returns the base operation of this named operation.

Returns:Operation

the base operation of this named operation.

getBaseOperationback to summary
public static Operation getBaseOperation(final Operation op)

If the passed operation is a namespace operation, returns its getBaseOperation(), otherwise returns the operation as is.

Parameters
op:Operation

the operation

Returns:Operation

the base operation of the passed operation.

getNamespaceback to summary
public Namespace getNamespace(final int i)

Returns the i-th namespace in this namespace operation.

Parameters
i:int

the namespace index

Returns:Namespace

the i-th namespace in this namespace operation.

Exceptions
IndexOutOfBoundsException:
if the index is out of range.
getNamespaceCountback to summary
public int getNamespaceCount()

Returns the number of namespaces in this namespace operation.

Returns:int

the number of namespaces in this namespace operation.

getNamespacesback to summary
public Namespace[] getNamespaces()

Returns the namespaces in this namespace operation. The returned array is a copy and changes to it don't have effect on this object.

Returns:Namespace[]

the namespaces in this namespace operation.

getNamespacesback to summary
public static Namespace[] getNamespaces(final Operation op)

If the passed operation is a namespace operation, returns its getNamespaces(), otherwise returns an empty array.

Parameters
op:Operation

the operation

Returns:Namespace[]

the namespaces of the passed operation.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns the hash code of this namespace operation. Defined to be equal to baseOperation.hashCode() + 31 * Arrays.hashCode(namespaces).

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the string representation of this namespace operation. Defined to be the toString of its base operation, followed by a colon character, followed with the list of its namespaces separated with the vertical line character (e.g. "GET:PROPERTY|ELEMENT").

Returns:String

the string representation of this namespace operation.

Annotations
@Override