Operation
public final class NamespaceOperation extends Object implements Operation
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");
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
Map
objects. A GET:PROPERTY|ELEMENT:empty
operation
against a Java map will always match
the 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");
Constructor | Description |
---|---|
NamespaceOperation(Operation baseOperation,
Namespace... namespaces) |
Constructs a new namespace operation.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
contains(Namespace namespace) |
Returns true if this namespace operation contains a namespace equal to
the specified namespace.
|
static boolean |
contains(Operation op,
Operation baseOperation,
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. |
boolean |
equals(Object obj) |
Returns true if the other object is also a namespace operation and their
base operation and namespaces are equal.
|
Operation |
getBaseOperation() |
Returns the base operation of this named operation.
|
static Operation |
getBaseOperation(Operation op) |
If the passed operation is a namespace operation, returns its
getBaseOperation() , otherwise returns the operation as is. |
Namespace |
getNamespace(int i) |
Returns the i-th namespace in this namespace operation.
|
int |
getNamespaceCount() |
Returns the number of namespaces in this namespace operation.
|
Namespace[] |
getNamespaces() |
Returns the namespaces in this namespace operation.
|
static Namespace[] |
getNamespaces(Operation op) |
If the passed operation is a namespace operation, returns its
getNamespaces() , otherwise returns an empty array. |
int |
hashCode() |
Returns the hash code of this namespace operation.
|
String |
toString() |
Returns the string representation of this namespace operation.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
named, withNamespace, withNamespaces
public NamespaceOperation(Operation baseOperation, Namespace... namespaces)
baseOperation
- the base operation that operates on one or more namespaces.namespaces
- one or more namespaces this operation operates on.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
.public Operation getBaseOperation()
public Namespace[] getNamespaces()
public int getNamespaceCount()
public Namespace getNamespace(int i)
i
- the namespace indexIndexOutOfBoundsException
- if the index is out of range.public boolean contains(Namespace namespace)
namespace
- the namespace being searched for. Must not be null.public boolean equals(Object obj)
equals
in class Object
obj
- the object to compare toObject.hashCode()
,
HashMap
public int hashCode()
baseOperation.hashCode() + 31 * Arrays.hashCode(namespaces)
.hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
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"
).public static Operation getBaseOperation(Operation op)
getBaseOperation()
, otherwise returns the operation as is.op
- the operationpublic static Namespace[] getNamespaces(Operation op)
getNamespaces()
, otherwise returns an empty array.op
- the operationpublic static boolean contains(Operation op, Operation baseOperation, Namespace namespace)
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.op
- the operation. Must not be null.baseOperation
- the base operation being searched for. Must not be null.namespace
- the namespace being searched for. Must not be null.NamespaceOperation
,
its base operation equals the searched base operation, and contains a namespace
equal to the searched namespace. Submit a bug or feature
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 2015, 2016, Oracle and/or its affiliates. 500 Oracle Parkway
Redwood Shores, CA 94065 USA. All rights reserved.
DRAFT 9-Ubuntu+0-9b149-1