
org.perfectable.introspection.bean.PropertySchema Maven / Gradle / Ivy
package org.perfectable.introspection.bean;
import org.perfectable.introspection.type.TypeView;
import java.lang.reflect.Type;
import javax.annotation.Nullable;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Schema of a Java Bean property.
*
* This class represents view of a field or getter/setter from perspective of Java Beans. This means that instead of
* fields and methods, property schema schema has type, name, and can be read and written.
*
* @param bean class for this property
* @param type of property values
*/
public abstract class PropertySchema {
/**
* Extracts name of the property.
*
* @return property name
*/
public abstract String name();
/**
* Extracts type of the property.
*
* @return property type
*/
public abstract Type type();
/**
* Answers if the property is readable.
*
* @return if the property can have value read
*/
public abstract boolean isReadable();
/**
* Answers if the property is writeable.
*
* @return if the property can have value written
*/
public abstract boolean isWritable();
final Property bind(B bean) {
return Property.of(bean, this);
}
PropertySchema() {
// package-only inheritance
}
abstract T get(B bean);
abstract void set(B bean, @Nullable T value);
final PropertySchema as(Class propertyClass) {
checkArgument(TypeView.of(type()).isSuperTypeOf(propertyClass));
@SuppressWarnings("unchecked")
PropertySchema casted = (PropertySchema) this;
return casted;
}
}