com.maxifier.mxcache.provider.StrategyProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mxcache-runtime Show documentation
Show all versions of mxcache-runtime Show documentation
Constains all classes necessary for launching a MxCache-instrumentated application
/*
* Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
*/
package com.maxifier.mxcache.provider;
import javax.annotation.Nonnull;
import java.lang.annotation.Annotation;
/**
* A generic user caching strategy property.
*
* @see CacheDescriptor#getProperty(StrategyProperty)
* @see CacheDescriptor#getProperty(AnnotationProperty)
* @see AnnotationProperty
*
* @author Alexander Kochurov ([email protected])
*/
public class StrategyProperty {
/**
* Property name to be used in mxcache.xml descriptor.
*/
private final String name;
/**
* Property value type
*/
private final Class type;
/**
* Default value of the property.
* Will be used if no definition is found in either corresponding annotation if any or mxcache.xml.
* It's up to strategy to allow user to leave some properties initialized to default.
*/
private final T defaultValue;
/**
* @param name property name to be used in mxcache.xml descriptor
* @param type property value type
* @param defaultValue default value of the property
*/
public StrategyProperty(@Nonnull String name, @Nonnull Class type, T defaultValue) {
if (defaultValue != null && !type.isInstance(defaultValue)) {
throw new IllegalArgumentException("Invalid defaultValue = " + defaultValue + " for property " + name + ": " + type);
}
this.name = name;
this.type = type;
this.defaultValue = defaultValue;
}
/**
* Creates a property with null-default value.
*
* @param name property name to be used in mxcache.xml descriptor.
* @param type property value type
*/
public StrategyProperty(String name, Class type) {
this(name, type, null);
}
public static StrategyProperty create(@Nonnull String name, @Nonnull Class type, T defaultValue) {
return new StrategyProperty(name, type, defaultValue);
}
public static StrategyProperty create(@Nonnull String name, @Nonnull Class type) {
return new StrategyProperty(name, type);
}
public static AnnotationProperty create(@Nonnull String name, @Nonnull Class type, Class annotationClass, String annotationPropertyName) {
return new ReflectiveAnnotationProperty(name, type, null, annotationClass, annotationPropertyName);
}
public static AnnotationProperty create(@Nonnull String name, @Nonnull Class type, T defaultValue, Class annotationClass, String annotationPropertyName) {
return new ReflectiveAnnotationProperty(name, type, defaultValue, annotationClass, annotationPropertyName);
}
/**
* @return property name to be used in mxcache.xml descriptor.
*/
public String getName() {
return name;
}
/**
* @return Property value type
*/
public Class getType() {
return type;
}
/**
* @return default value of the property.
* Will be used if no definition is found in either corresponding annotation if any or mxcache.xml.
* It's up to strategy to allow user to leave some properties initialized to default.
*/
public T getDefaultValue() {
return defaultValue;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StrategyProperty that = (StrategyProperty) o;
return name.equals(that.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name + ": " + type;
}
}