
org.jsonddl.JsonDdlVisitor Maven / Gradle / Ivy
/*
* Copyright 2011 Robert W. Vawter III
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.jsonddl;
import java.util.List;
import org.jsonddl.model.Kind;
/**
* Indicates that an object can traverse an object graph. Visitors will receive objects by
* implementing methods with the following signatures:
*
*
* boolean visit(SomeType t, Context<SomeType> ctx);
*
* void endVisit(SomeType t, Context<SomeType> ctx);
*
*
* Visitors need only implement the methods that they are interested in. The {@code visit} method
* returns a boolean value to indicate if the object being visited should be traversed. If
* {@code visit} is unimplemented, the default behavior is to traverse the properties of the object.
* The {@link Context} parameter may be omittedd if not required.
*/
public interface JsonDdlVisitor {
/**
* The Context interface provides a visitor with information about the object or property being
* visited. When a visitor is traversing a Bulider, the Context can be used to mutate the object
* in-place.
*
* If a raw Context interface is used, it is possible to pass arbitrary data into the mutator
* methods (i.e. {@link #insertAfter(Object)}, {@link #insertBefore(Object)},
* {@link #replace(Object)} ). To prevent errors, these methods will throw a
* {@link ClassCastException} if the object provided is not appropriate for the current slot.
*
* @param the type of data stored in the slot being traversed
*/
public interface Context {
/**
* Indicates if {@link #insertAfter(Object)} and {@link #insertBefore(Object)} can be
* successfully called.
*/
boolean canInsert();
/**
* Indicates if {@link #remove()} can be successfully called.
*/
boolean canRemove();
/**
* Indicates if {@link #replace(Object)} can be succesfully called.
*/
boolean canReplace();
/**
* Returns the basic json data type of the property being visited.
*/
Kind getKind();
/**
* Provides the declared java type of the leaf type in a map or list.
*/
Class> getLeafType();
/**
* Provides the parameterization of list or map properties.
*/
List getNestedKinds();
/**
* Returns the name of the property being visited.
*/
String getProperty();
/**
* Available while traversing a mutable list context. The inserted value will be traversed.
*/
void insertAfter(J next);
/**
* Available while traversing a mutable list context. The inserted value will not be traversed.
*/
void insertBefore(J previous);
/**
* Available in all mutable contexts to delete the current value.
*/
void remove();
/**
* Available in all mutable contexts to replace the current value.
*/
void replace(J replacement);
}
/**
* Optional capability interface for visitors that wish to consume object properties dynamically.
* The {@link #visitProperty(Object, Context)} method will be called before visiting the value of
* the property, and any side-effects of {@code visitProperty} will be visible to a type-specific
* {@code visit} method. Similarly, a type-specific {@code endVisit} will be called before
* {@link #endVisitProperty(Object, Context)}.
*/
public interface PropertyVisitor extends JsonDdlVisitor {
void endVisitProperty(T value, Context ctx);
boolean visitProperty(T value, Context ctx);
}
}