All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.exparity.beans.Instance Maven / Gradle / Ivy

Go to download

A Java library of bean utilities for manipulating and inspecting Java classes implementing the Java Beans standard

There is a newer version: 1.0.3
Show newest version

package org.exparity.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.exparity.beans.naming.CamelCaseNamingStrategy;

import static org.exparity.beans.BeanPredicates.*;

/**
 * @author Stewart Bissett
 */
abstract class Instance {

	private final InstanceInspector inspector;
	private final Object instance;
	private BeanNamingStrategy naming = new CamelCaseNamingStrategy();

	protected Instance(final InstanceInspector inspector, final Object instance) {
		this.inspector = inspector;
		this.instance = instance;
	}

	public List propertyList() {
		final List propertyList = new ArrayList();
		visit(new BeanVisitor() {

			public void visit(final BeanProperty property, final Object current, final BeanPropertyPath path, final Object[] stack) {
				propertyList.add(property);
			}
		});
		return propertyList;
	}

	public Map propertyMap() {
		final Map propertyMap = new HashMap();
		visit(new BeanVisitor() {

			public void visit(final BeanProperty property, final Object current, final BeanPropertyPath path, final Object[] stack) {
				propertyMap.put(property.getName(), property);
			}
		});
		return propertyMap;
	}

	public boolean setProperty(final BeanPropertyPredicate predicate, final Object value) {
		final List valuesSet = new ArrayList();
		visit(new BeanVisitor() {

			public void visit(final BeanProperty property, final Object current, final BeanPropertyPath path, final Object[] stack) {
				if (predicate.matches(property)) {
					property.setValue(value);
					valuesSet.add(property);
				}
			}
		});
		return !valuesSet.isEmpty();
	}

	public Object propertyValue(final BeanPropertyPredicate predicate) {
		BeanProperty property = findAny(predicate);
		if (property != null) {
			return property.getValue();
		}
		return null;
	}

	public  T propertyValue(final BeanPropertyPredicate predicate, final Class type) {
		BeanProperty property = findAny(predicate);
		if (property != null) {
			return property.getValue(type);
		}
		return null;
	}

	public Object propertyValue(final String name) {
		return propertyNamed(name).getValue();
	}

	public  T propertyValue(final String name, final Class type) {
		return propertyNamed(name).getValue(type);
	}

	public boolean hasProperty(final BeanPropertyPredicate predicate) {
		return findAny(predicate) != null;
	}

	public BeanProperty get(final String propertyName) {
		return propertyNamed(propertyName);
	}

	public BeanProperty get(final BeanPropertyPredicate predicate) {
		return findAny(predicate);
	}

	public BeanProperty propertyNamed(final String propertyName) {
		BeanProperty property = findAny(named(propertyName));
		if (property == null) {
			throw new BeanPropertyNotFoundException(this.instance.getClass(), propertyName);
		}
		return property;
	}

	public Class propertyType(final String name) {
		return propertyNamed(name).getType();
	}

	public Class propertyType(final BeanPropertyPredicate predicate) {
		BeanProperty found = findAny(predicate);
		if (found != null) {
			return found.getType();
		}
		return null;
	}

	public BeanProperty findAny(final BeanPropertyPredicate predicate) {

		@SuppressWarnings("serial")
		class HaltVisitException extends RuntimeException {

			BeanProperty property;

			private HaltVisitException(final BeanProperty property) {
				this.property = property;
			}
		}

		try {
			visit(new BeanVisitor() {

				public void visit(final BeanProperty property, final Object current, final BeanPropertyPath path, final Object[] stack) {
					if (predicate.matches(property)) {
						throw new HaltVisitException(property);
					}
				}
			});
		} catch (HaltVisitException e) {
			return e.property;
		}
		return null;
	}

	public void apply(final BeanPropertyFunction function) {
		apply(function, anyProperty());
	}

	public void apply(final BeanPropertyFunction function, final BeanPropertyPredicate predicate) {
		visit(new BeanVisitor() {

			public void visit(final BeanProperty property, final Object current, final BeanPropertyPath path, final Object[] stack) {
				if (predicate.matches(property)) {
					function.apply(property);
				}
			}
		});
	}

	public List find(final BeanPropertyPredicate predicate) {
		final List collection = new ArrayList();
		visit(new BeanVisitor() {

			public void visit(final BeanProperty property, final Object current, final BeanPropertyPath path, final Object[] stack) {
				if (predicate.matches(property)) {
					collection.add(property);
				}
			}
		});
		return collection;
	}

	public void visit(final BeanVisitor visitor) {
		inspector.inspect(instance, naming, visitor);
	}

	public boolean hasProperty(final String name) {
		return hasProperty(named(name));
	}

	public boolean hasProperty(final String name, final Object value) {
		return hasProperty(withValue(name, value));
	}

	public boolean setProperty(final String name, final Object value) {
		return propertyNamed(name).setValue(value);
	}

	public boolean isPropertyType(final String name, final Class type) {
		return propertyNamed(name).isType(type);
	}

	public boolean isPropertyType(final BeanPropertyPredicate predicate, final Class type) {
		return hasProperty(matchesAll(predicate, ofType(type)));
	}

	public void setNamingStrategy(final BeanNamingStrategy naming) {
		this.naming = naming;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy