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

org.infinispan.commons.configuration.attributes.AttributeDefinition Maven / Gradle / Ivy

The newest version!
package org.infinispan.commons.configuration.attributes;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

import org.infinispan.commons.CacheConfigurationException;

/**
 * AttributeDefinition. Defines the characteristics of a configuration attribute. It is used to
 * construct an actual {@link Attribute} holder.
 * 

* An attribute definition has the following characteristics: *

    *
  • A name
  • *
  • A default value or a value initializer
  • *
  • A type, which needs to be specified if it cannot be inferred from the default value, i.e. * when it is null
  • *
  • Whether an attribute is immutable or not, i.e. whether its value is constant after * initialization or it can be changed
  • *
  • A validator which intercepts invalid values
  • *
* * @author Tristan Tarrant * @since 7.2 */ public final class AttributeDefinition { private final String name; private final T defaultValue; private final boolean immutable; private final boolean autoPersist; private final boolean global; private final AttributeCopier copier; private final AttributeInitializer initializer; private final AttributeValidator validator; private final AttributeSerializer serializer; private final AttributeParser parser; private final Class type; private final int deprecatedMajor; private final int deprecatedMinor; private final int sinceMajor; private final int sinceMinor; private final AttributeMatcher matcher; private AttributeDefinition(Builder builder) { this.name = builder.name; this.defaultValue = builder.defaultValue; this.immutable = builder.immutable; this.autoPersist = builder.autoPersist; this.global = builder.global; this.copier = builder.copier; this.initializer = builder.initializer; this.matcher = builder.matcher; this.validator = builder.validator; this.serializer = builder.serializer; this.parser = builder.parser; this.type = builder.type; this.deprecatedMajor = builder.deprecatedMajor; this.deprecatedMinor = builder.deprecatedMinor; this.sinceMajor = builder.sinceMajor; this.sinceMinor = builder.sinceMinor; } public String name() { return name; } public Class getType() { return type; } public T getDefaultValue() { return initializer != null ? initializer().initialize() : defaultValue; } public boolean isImmutable() { return immutable; } public boolean isAutoPersist() { return autoPersist; } public boolean isRepeated() { return type.isArray() || Collection.class.isAssignableFrom(type); } public boolean isGlobal() { return global; } public boolean isDeprecated(int major, int minor) { return (major > deprecatedMajor || (major == deprecatedMajor && minor > deprecatedMinor)); } public boolean isSince(int major, int minor) { return major > sinceMajor || (major == sinceMajor && minor >= sinceMinor); } public AttributeCopier copier() { return copier; } public AttributeInitializer initializer() { return initializer; } public AttributeMatcher matcher() { return matcher; } AttributeValidator validator() { return validator; } public AttributeSerializer serializer() { return serializer; } public T parse(String value) { return (T) parser.parse(type, value); } public Attribute toAttribute() { return new Attribute<>(this); } public void validate(T value) { if (validator != null) { validator.validate(value); } } public static Builder builder(Enum name, T defaultValue) { return builder(name.toString(), defaultValue); } public static Builder builder(String name, T defaultValue) { if (defaultValue != null) { return new Builder(name, defaultValue, (Class) defaultValue.getClass()); } else { throw new CacheConfigurationException("Must specify type when passing null for AttributeDefinition " + name); } } public static Builder builder(Enum name, T defaultValue, Class klass) { return new Builder<>(name.toString(), defaultValue, klass); } public static Builder builder(String name, T defaultValue, Class klass) { return new Builder<>(name, defaultValue, klass); } public static Builder> classBuilder(String name, Class klass) { return new Builder(name, null, Class.class); } public static Builder> supplierBuilder(String name, Class klass) { return new Builder(name, null, Supplier.class); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AttributeDefinition that = (AttributeDefinition) o; if (immutable != that.immutable) return false; if (autoPersist != that.autoPersist) return false; if (global != that.global) return false; if (!Objects.equals(name, that.name)) return false; if (!Objects.equals(defaultValue, that.defaultValue)) return false; if (!Objects.equals(copier, that.copier)) return false; if (!Objects.equals(initializer, that.initializer)) return false; if (!Objects.equals(validator, that.validator)) return false; if (!Objects.equals(serializer, that.serializer)) return false; return type == that.type; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (defaultValue != null ? defaultValue.hashCode() : 0); result = 31 * result + (immutable ? 1 : 0); result = 31 * result + (autoPersist ? 1 : 0); result = 31 * result + (global ? 1 : 0); result = 31 * result + (copier != null ? copier.hashCode() : 0); result = 31 * result + (initializer != null ? initializer.hashCode() : 0); result = 31 * result + (validator != null ? validator.hashCode() : 0); result = 31 * result + (serializer != null ? serializer.hashCode() : 0); result = 31 * result + (type != null ? type.hashCode() : 0); return result; } public static final class Builder { private final String name; private final T defaultValue; private final Class type; private boolean immutable = false; private boolean autoPersist = true; private boolean global = true; private AttributeCopier copier = null; private AttributeInitializer initializer; private AttributeValidator validator; private AttributeSerializer serializer = AttributeSerializer.DEFAULT; private AttributeParser parser = AttributeParser.DEFAULT; private AttributeMatcher matcher; private int deprecatedMajor = Integer.MAX_VALUE; private int deprecatedMinor = Integer.MAX_VALUE; // We started with Infinispan 4.0 private int sinceMajor = 4; private int sinceMinor = 0; private Builder(String name, T defaultValue, Class type) { this.name = name; this.defaultValue = defaultValue; this.type = type; } public Builder immutable() { this.immutable = true; return this; } public Builder copier(AttributeCopier copier) { this.copier = copier; return this; } public Builder initializer(AttributeInitializer initializer) { this.initializer = initializer; return this; } public Builder matcher(AttributeMatcher matcher) { this.matcher = matcher; return this; } public Builder autoPersist(boolean autoPersist) { this.autoPersist = autoPersist; return this; } public Builder global(boolean global) { this.global = global; return this; } public Builder validator(AttributeValidator validator) { this.validator = validator; return this; } public Builder serializer(AttributeSerializer serializer) { this.serializer = serializer; return this; } public Builder parser(AttributeParser parser) { this.parser = parser; return this; } public Builder deprecated(int major, int minor) { this.deprecatedMajor = major; this.deprecatedMinor = minor; return this; } public Builder since(int major, int minor) { this.sinceMajor = major; this.sinceMinor = minor; return this; } public AttributeDefinition build() { return new AttributeDefinition<>(this); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy