public class TypeBasedMultiplexer extends Object
org.everit.json.schema.loader.SchemaLoader.SchemaLoader during schema loading for
type-based action selections. In other words this utility class is used for avoiding
if..instanceof..casting constructs. Together with the TypeBasedMultiplexer.OnTypeConsumer
implementations it forms a fluent API to deal with the parts of the JSON schema where multiple
kind of values are valid for a given key.
Example usage:
Object additProps = schemaJson.get("additionalProperties");
typeMultiplexer(additionalProps)
.ifIs(JSONArray.class).then(arr -> {...if additProps is a JSONArray then process it... })
.ifObject().then(obj -> {...if additProps is a JSONArray then process it... })
.requireAny(); // throw a SchemaException if additProps is neither a JSONArray nor a JSONObject
This class it NOT thread-safe.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TypeBasedMultiplexer.OnTypeConsumer<E>
Created and used by
TypeBasedMultiplexer to set actions (consumers) for matching
classes. |
| Constructor and Description |
|---|
TypeBasedMultiplexer(Object obj)
Constructor with
null keyOfObj and null id. |
TypeBasedMultiplexer(String keyOfObj,
Object obj)
Contstructor with
null id. |
TypeBasedMultiplexer(String keyOfObj,
Object obj,
URI id)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addResolutionScopeChangeListener(ResolutionScopeChangeListener resolutionScopeChangeListener) |
<E> TypeBasedMultiplexer.OnTypeConsumer<E> |
ifIs(Class<E> predicateClass)
Creates a setter which will be invoked by
orElse(Consumer) or requireAny() if
obj is an instance of predicateClass. |
TypeBasedMultiplexer.OnTypeConsumer<org.json.JSONObject> |
ifObject()
Creates a
JSONObject consumer setter. |
void |
orElse(java.util.function.Consumer<Object> orElseConsumer)
Checks if the
obj is an instance of any previously set classes (by ifIs(Class)
or ifObject()), performs the mapped action of found or invokes orElseConsumer
with the obj. |
void |
requireAny()
Checks if the
obj is an instance of any previously set classes (by ifIs(Class)
or ifObject()), performs the mapped action of found or throws with a
SchemaException. |
public TypeBasedMultiplexer(Object obj)
null keyOfObj and null id.obj - the object which' class is matched against the classes defined by ifIs(Class)
(or ifObject()) calls.public TypeBasedMultiplexer(String keyOfObj, Object obj)
null id.keyOfObj - is an optional (nullable) string used by requireAny() to construct the
message of the SchemaException if no appropriate consumer action is found.obj - the object which' class is matched against the classes defined by ifIs(Class)
(or ifObject()) calls.public TypeBasedMultiplexer(String keyOfObj, Object obj, URI id)
keyOfObj - is an optional (nullable) string used by requireAny() to construct the
message of the SchemaException if no appropriate consumer action is found.obj - the object which' class is matched against the classes defined by ifIs(Class)
(or ifObject()) calls.id - the scope id at the point where the multiplexer is initialized.public void addResolutionScopeChangeListener(ResolutionScopeChangeListener resolutionScopeChangeListener)
public <E> TypeBasedMultiplexer.OnTypeConsumer<E> ifIs(Class<E> predicateClass)
orElse(Consumer) or requireAny() if
obj is an instance of predicateClass.E - the type represented by predicateClass.predicateClass - the predicate class (the callback set by a subsequent
TypeBasedMultiplexer.OnTypeConsumer.then(Consumer) will be executed if obj is an instance
of predicateClass).OnTypeConsumer implementation to be used to set the action performed if
obj is an instance of predicateClass.IllegalArgumentException - if predicateClass is JSONObject. Use ifObject() for matching
obj's class against JSONObject.public TypeBasedMultiplexer.OnTypeConsumer<org.json.JSONObject> ifObject()
JSONObject consumer setter.
The returned TypeBasedMultiplexer.OnTypeConsumer implementation will wrap the
passed consumer action with an other consumer which
properly maintains the SchemaLoader.id attribute, ie. if
obj is a JSONObject instance and it has an id property then it will
append this id value to SchemaLoader.id for the duration
of the action execution, then it will restore the original id.
OnTypeConsumer implementation to be used to set the action performed if
obj is a JSONObject instance.public void orElse(java.util.function.Consumer<Object> orElseConsumer)
obj is an instance of any previously set classes (by ifIs(Class)
or ifObject()), performs the mapped action of found or invokes orElseConsumer
with the obj.orElseConsumer - the callback to be called if no types matched.public void requireAny()
obj is an instance of any previously set classes (by ifIs(Class)
or ifObject()), performs the mapped action of found or throws with a
SchemaException.Copyright © 2011–2016 Everit Kft.. All rights reserved.