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

jalse.attributes.Attributes Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package jalse.attributes;

import java.lang.reflect.Type;
import java.util.Objects;

/**
 * A utility for {@link AttributeType} value related functionality.
 *
 * @author Elliot Ford
 *
 */
public final class Attributes {

    /**
     * {@link Boolean} Attribute type.
     */
    public static final AttributeType BOOLEAN_TYPE = new AttributeType() {};

    /**
     * {@link Integer} Attribute type.
     */
    public static final AttributeType INTEGER_TYPE = new AttributeType() {};

    /**
     * {@link String} Attribute type.
     */
    public static final AttributeType STRING_TYPE = new AttributeType() {};

    /**
     * {@link Double} Attribute type.
     */
    public static final AttributeType DOUBLE_TYPE = new AttributeType() {};

    /**
     * {@link Character} Attribute type.
     */
    public static final AttributeType CHARACTER_TYPE = new AttributeType() {};

    /**
     * {@link Long} Attribute type.
     */
    public static final AttributeType LONG_TYPE = new AttributeType() {};

    /**
     * {@link Byte} Attribute type.
     */
    public static final AttributeType BYTE_TYPE = new AttributeType() {};

    /**
     * {@link Float} Attribute type.
     */
    public static final AttributeType FLOAT_TYPE = new AttributeType() {};

    /**
     * {@link Short} Attribute type.
     */
    public static final AttributeType SHORT_TYPE = new AttributeType() {};

    /**
     * {@link Object} Attribute type.
     */
    public static final AttributeType OBJECT_TYPE = new AttributeType() {};

    /**
     * An empty AttributeContainer.
     */
    public static final AttributeContainer EMPTY_ATTRIBUTECONTAINER = new UnmodifiableDelegateAttributeContainer(null);

    /**
     * Creates an immutable empty attribute container.
     *
     * @return Empty attribute container.
     */
    public static AttributeContainer emptyAttributeContainer() {
	return EMPTY_ATTRIBUTECONTAINER;
    }

    /**
     * Creates a new attribute type (Boolean).
     *
     * @param name
     *            Name of the attribute type.
     * @return New boolean attribute type.
     *
     * @see #BOOLEAN_TYPE
     */
    public static NamedAttributeType newNamedBooleanType(final String name) {
	return new NamedAttributeType<>(name, BOOLEAN_TYPE);
    }

    /**
     * Creates a new attribute type (Byte).
     *
     * @param name
     *            Name of the attribute type.
     * @return New byte attribute type.
     *
     * @see #BYTE_TYPE
     */
    public static NamedAttributeType newNamedByteType(final String name) {
	return new NamedAttributeType<>(name, BYTE_TYPE);
    }

    /**
     * Creates a new attribute type (String).
     *
     * @param name
     *            Name of the attribute type.
     * @return New string attribute type.
     *
     * @see #CHARACTER_TYPE
     */
    public static NamedAttributeType newNamedCharacterType(final String name) {
	return new NamedAttributeType<>(name, CHARACTER_TYPE);
    }

    /**
     * Creates a new attribute type (Double).
     *
     * @param name
     *            Name of the attribute type.
     * @return New double attribute type.
     *
     * @see #DOUBLE_TYPE
     */
    public static NamedAttributeType newNamedDoubleType(final String name) {
	return new NamedAttributeType<>(name, DOUBLE_TYPE);
    }

    /**
     * Creates a new attribute type (Float).
     *
     * @param name
     *            Name of the attribute type.
     * @return New float attribute type.
     *
     * @see #FLOAT_TYPE
     */
    public static NamedAttributeType newNamedFloatType(final String name) {
	return new NamedAttributeType<>(name, FLOAT_TYPE);
    }

    /**
     * Creates a new attribute type (Integer).
     *
     * @param name
     *            Name of the attribute type.
     * @return New integer attribute type.
     *
     * @see #INTEGER_TYPE
     */
    public static NamedAttributeType newNamedIntegerType(final String name) {
	return new NamedAttributeType<>(name, INTEGER_TYPE);
    }

    /**
     * Creates a new attribute type (Long).
     *
     * @param name
     *            Name of the attribute type.
     * @return New long attribute type.
     *
     * @see #LONG_TYPE
     */
    public static NamedAttributeType newNamedLongType(final String name) {
	return new NamedAttributeType<>(name, LONG_TYPE);
    }

    /**
     * Creates a new attribute type (Object).
     *
     * @param name
     *            Name of attribute type.
     * @return New object attribute type.
     *
     * @see #OBJECT_TYPE
     */
    public static NamedAttributeType newNamedObjectType(final String name) {
	return new NamedAttributeType<>(name, OBJECT_TYPE);
    }

    /**
     * Creates a new attribute type (Short).
     *
     * @param name
     *            Name of the attribute type.
     * @return New short attribute type.
     *
     * @see #SHORT_TYPE
     */
    public static NamedAttributeType newNamedShortType(final String name) {
	return new NamedAttributeType<>(name, SHORT_TYPE);
    }

    /**
     * Creates a new attribute type (String).
     *
     * @param name
     *            Name of the attribute type.
     * @return New string attribute type.
     *
     * @see #STRING_TYPE
     */
    public static NamedAttributeType newNamedStringType(final String name) {
	return new NamedAttributeType<>(name, STRING_TYPE);
    }

    /**
     * Creates a new named type of the supplied simple type.
     *
     * @param name
     *            Name of the attribute type.
     * @param type
     *            Simple type.
     * @return New named attribute type.
     *
     * @see #newTypeOf(Class)
     */
    public static  NamedAttributeType newNamedTypeOf(final String name, final Class type) {
	return new NamedAttributeType<>(name, newTypeOf(type));
    }

    /**
     * Creates a new named unknown type.
     *
     * @param name
     *            Attribute type name.
     * @param type
     *            Unknown type.
     * @return New named unknown type.
     */
    public static NamedAttributeType newNamedUnknownType(final String name, final Type type) {
	return new NamedAttributeType<>(name, new AttributeType(type) {});
    }

    /**
     * Creates a new attribute type of the supplied simple type.
     *
     * @param type
     *            Simple type.
     * @return Newly created simple type.
     *
     * @see AttributeType
     */
    public static  AttributeType newTypeOf(final Class type) {
	return new AttributeType(type) {};
    }

    /**
     * Creates a new unknown attribute type from the supplied type.
     *
     * @param type
     *            Unknown type.
     * @return Newly created unknown type.
     */
    public static AttributeType newUnknownType(final Type type) {
	return new AttributeType(type) {};
    }

    /**
     * Ensures the String is not null or empty.
     *
     * @param str
     *            String to check.
     * @return The string.
     * @throws IllegalArgumentException
     *             If the string was null or empty.
     *
     */
    public static String requireNotEmpty(final String str) throws IllegalArgumentException {
	if (str == null || str.length() == 0) {
	    throw new IllegalArgumentException();
	}
	return str;
    }

    /**
     * Creates an immutable read-only delegate attribute container for the supplied container.
     *
     * @param container
     *            Container to delegate for.
     * @return Immutable attribute container.
     */
    public static AttributeContainer unmodifiableAttributeContainer(final AttributeContainer container) {
	return new UnmodifiableDelegateAttributeContainer(Objects.requireNonNull(container));
    }

    private Attributes() {
	throw new UnsupportedOperationException();
    }
}