org.exparity.beans.GraphUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of exparity-bean Show documentation
Show all versions of exparity-bean Show documentation
A Java library of bean utilities for manipulating and inspecting Java classes implementing the Java Beans standard
package org.exparity.beans;
import static org.exparity.beans.Graph.graph;
import java.util.List;
import java.util.Map;
/**
* Utility methods for inspecting objects which expose properties which follow the Java Bean get/set standard.
*
* {@link GraphUtils} will perform a deep inspection of the object and perform the requested action across the object and across all it's children and their children and so on.
*
*
* The sister utility {@link BeanUtils} should be use if only a shallow inspection is required.
*
*
* @author Stewart Bissett
*/
public abstract class GraphUtils {
/**
* Visit the graph and notify the visitor for each bean property found. This method will descend into the object graph by looping over collections and will
* visit all properties on assosciated objects and their assosciated objects and so on. This method will at most visit each object once even when the object refers to
* itself in order to prevent stack overflows.
*
*
* GraphUtils.visit(myUser, new BeanVisitor() {
*
* public void visit(final BeanPropertyInstance property, final Object current, final BeanPropertyPath path, final Object[] stack) {
* System.out.println(path.fullPath());
* }
* });
*
*
* @param instance an object to visit the properties on
* @param visitor the visitor which will be notified of every bean property encountered
*/
public static void visit(final Object instance, final BeanVisitor visitor) {
graph(instance).visit(visitor);
}
/**
* Apply the {@link BeanPropertyFunction} to all properties which match the predicate in the supplied instance and in any of the objects assosciates. For example
*
*
* GraphUtils.apply(myFamilyTree, deletePerson(), isDeceased()));
*
*
* @param instance an object to apply the function to
* @param function the function to be applied to each matching property
* @param predicate the predicate to select which properties to apply the function to
*/
public static void apply(final Object instance, final BeanPropertyFunction function, final BeanPropertyPredicate predicate) {
graph(instance).apply(function, predicate);
}
/**
* Apply the {@link BeanPropertyFunction} to all properties in the supplied instance and in any of the objects assosciates. For example
*
*
* GraphUtils.apply(myFamilyTree, pruneDeceased()));
*
*
* @param instance an object to apply the function to
* @param function the function to be applied to each matching property
*/
public static void apply(final Object instance, final BeanPropertyFunction function) {
graph(instance).apply(function);
}
/**
* Find all property instances which match the predicate in the supplied graph. For example
*
*
* List relativesCalledBob = GraphUtils.find(myFamilyTree, BeanPredicates.likeValue("name","Bob.*"))
*
*
* @param instance an object to find the properties on
* @param predicate the predicate to select which properties to return
*/
public static List find(final Object instance, final BeanPropertyPredicate predicate) {
return graph(instance).find(predicate);
}
/**
* Find the first instance of the property which matches the given predicate in the given instance and it's assosciates. For example
*
*
* BeanPropertyInstance property = GraphUtils.findAny(myFamilyTree, BeanPredicates.withProperty("name" "Bob"));
*
*
* @param instance an object to find the properties on
* @param predicate the predicate to select which properties to return
*/
public static BeanProperty findAny(final Object instance, final BeanPropertyPredicate predicate) {
return graph(instance).findAny(predicate);
}
/**
* Return a list of the publicly exposes get/set properties on the graph. For example:
*
*
*
* List<BeanPropertyInstance> properties = GraphUtils.propertyList(myObject)
*
*
* @param instance an object to get the properties list from
*/
public static List propertyList(final Object instance) {
return graph(instance).propertyList();
}
/**
* Return a map of the publicly exposes get/set properties on the graph with the property name as the key and the initial character lowercased. Child properties will use
* dot-notation for the property name. For example:
*
*
*
* Map<String, BeanPropertyInstance> propertyMap = GraphUtils.propertyMap(anyPerson)
* BeanPropertyInstance property = propertyMap.get("person.address")
*
*
* @param instance an object to get the properties for
*/
public static Map propertyMap(final Object instance) {
return graph(instance).propertyMap();
}
/**
* Test if the supplied instance or it's children have a property with the given name. For example
*
*
* if (GraphUtils.hasProperty(user, "person.children")) {
* // do something
* }
*
*
* @param instance an object to test against
* @param name the property name
*/
public static boolean hasProperty(final Object instance, final String name) {
return graph(instance).hasProperty(name);
}
/**
* Test if the supplied instance or it's children have a property with the given name. For example
*
*
* if (GraphUtils.hasProperty(user, "person.employment.employed", true)) {
* // do something
* }
*
*
* @param instance an object to test against
* @param name the property name
* @param value the value to test for
*/
public static boolean hasProperty(final Object instance, final String name, final Object value) {
return graph(instance).hasProperty(name, value);
}
/**
* Test if the supplied instance or it's children have a property which matches the given predicate. For example
*
*
* if (GraphUtils.hasProperty(user, BeanPredicates.withValue("surname", "Smith"))) {
* System.Out.println("Found a Smith")
* }
*
*
* @param instance an object to test against
* @param name the property name
*/
public static boolean hasProperty(final Object instance, final BeanPropertyPredicate predicate) {
return graph(instance).hasProperty(predicate);
}
/**
* Get the requested property from the graph or return null
if the property is not present. For example
*
*
* BeanPropertyInstance surname = GraphUtils.propertyNamed(myUser, "surname")
*
*
* @param instance an object to get the property from
* @param name the property name
*/
public static BeanProperty propertyNamed(final Object instance, final String name) {
return graph(instance).propertyNamed(name);
}
/**
* Get the requested property from the graph or return null
if the property is not present. For example
*
*
* BeanPropertyInstance surname = GraphUtils.get(myUser, "surname")
*
*
* @param instance an object to get the property from
* @param name the property name
*/
public static BeanProperty get(final Object instance, final String name) {
return graph(instance).get(name);
}
/**
* Get the firt property from the graph which matches the predicate or return null
if the property is not present. For example
*
*
* BeanPropertyInstance surname = GraphUtils.get(myUser, BeanPredicates.named("surname"))
*
*
* @param instance an object to get the property from
* @param name the property name
*/
public static BeanProperty get(final Object instance, final BeanPropertyPredicate predicate) {
return graph(instance).get(predicate);
}
/**
* Set the property value on the instance or it's children to the supplied value. For example:
*
*
* GraphUtils.setProperty(myUser, "surname", "Smith")
*
*
* @param instance an object to get the property from
* @param name the property name
* @param value the value to set the property to
*/
public static boolean setProperty(final Object instance, final String name, final Object value) {
return graph(instance).setProperty(name, value);
}
/**
* Set the property value on the instance or it's children which matches the predicate to the supplied value. For example:
*
*
* GraphUtils.setProperty(myUser, BeanPredicates.named("surname"), "Smith")
*
*
* @param instance an object to get the property from
* @param name the property name
* @param value the value to set the property to
*/
public static boolean setProperty(final Object instance, final BeanPropertyPredicate predicate, final Object value) {
return graph(instance).setProperty(predicate, value);
}
/**
* Return the property value on the instance for the supplied property name or return null
if the property is not present on the graph. For example:
*
*
*
* Integer age = (Integer)GraphUtils.propertyValue(myUser, "age")
*
*
* @param instance an object to get the property from
* @param name the property name
*/
public static Object propertyValue(final Object instance, final String propertyName) {
return graph(instance).propertyValue(propertyName);
}
/**
* Return the property value on the instance for the supplied property name or return null
if the property is not present on the graph. For example:
*
*
*
* Integer age = GraphUtils.propertyValue(myUser, "age", Integer.class)
*
*
* @param instance an object to get the property from
* @param name the property name
* @param name the type to return the property as
*/
public static T propertyValue(final Object instance, final String propertyName, final Class type) {
return graph(instance).propertyValue(propertyName, type);
}
/**
* Return the first property value on the instance for the property which matches the supplied predicate or return null
if the property is not present on the
* graph. For example:
*
*
*
* Object age = GraphUtils.propertyValue(myUser, BeanPredicates.named("age"))
*
*
* @param instance an object to get the property from
* @param predicate a predicate to match the property
*/
public static Object propertyValue(final Object instance, final BeanPropertyPredicate predicate) {
return graph(instance).propertyValue(predicate);
}
/**
* Return the first property value on the instance for the property which matches the supplied predicate or return null
if the property is not present on the
* graph. For example:
*
*
*
* Intenger age = GraphUtils.propertyValue(myUser, BeanPredicates.named("age"), Integer.class)
*
*
* @param instance an object to get the property from
* @param predicate a predicate to match the property
* @param type the type to return the property as
*/
public static T propertyValue(final Object instance, final BeanPropertyPredicate predicate, final Class type) {
return graph(instance).propertyValue(predicate, type);
}
/**
* Return the property type on the instance for the first property which matches the predicate or null
if no matching property exists. For example:
*
*
* Class<?> managerType = GraphUtils.propertyType(myUser, BeanPr"person.manager")
*
*
* @param instance an object to get the property from
* @param predicate a predicate to match the property
*/
public static Class> propertyType(final Object instance, final BeanPropertyPredicate predicate) {
return graph(instance).propertyType(predicate);
}
/**
* Return the property type on the instance for the supplied property name or null
if the property doesn't exist. For example:
*
*
* Class<?> managerType = GraphUtils.propertyType(myUser, "person.manager")
*
*
* @param instance an object to get the property from
* @param name the property name
*/
public static Class> propertyType(final Object instance, final String propertyName) {
return graph(instance).propertyType(propertyName);
}
/**
* Test if the property on the supplied instance is of the supplied type. For example:
*
*
* if (GraphUtils.isPropertyType(myUser, "person.manager", Director.class)) {
* // Do something
* }
*
*
* @param instance an object to get the property from
* @param name the property name
* @param type the expected type of the property
*/
public static boolean isPropertyType(final Object instance, final String propertyName, final Class> type) {
return graph(instance).isPropertyType(propertyName, type);
}
/**
* Test if the property on the supplied instance is of the supplied type. For example
*
*
*
* if (GraphUtils.isPropertyType(myUser, BeanPredicates.named("person.manager"), Director.class)) {
* // Do something
* }
*
*
* @param instance an object to get the property from
* @param name the property name
* @param type the expected type of the property
*/
public static boolean isPropertyType(final Object instance, final BeanPropertyPredicate predicate, final Class> type) {
return graph(instance).isPropertyType(predicate, type);
}
}