Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.util
Enum Primitive

java.lang.Object
  extended by java.lang.Enum<Primitive>
      extended by dk.rode.thesis.meta.util.Primitive
All Implemented Interfaces:
Serializable, Comparable<Primitive>

public enum Primitive
extends Enum<Primitive>

A primitive constant associates a primitive type with its wrapper class.

For example, the primitive type int is represented by the java.lang.Integer.TYPE class, and the associated wrapper class is the java.lang.Integer class.

The order of declaration is as follows:

Author:
Gunni Rode / rode.dk

Enum Constant Summary
Boolean
          Corresponding to the primitive type boolean (java.lang.Boolean.TYPE) and the non-primitive wrapper class java.lang.Boolean.
Byte
          Corresponding to the primitive type byte (java.lang.Byte.TYPE) and the non-primitive wrapper class java.lang.Byte.
Character
          Corresponding to the primitive type char (java.lang.Character.TYPE) and the non-primitive wrapper class java.lang.Character.
Double
          Corresponding to the primitive type double (java.lang.Double.TYPE) and the non-primitive wrapper class java.lang.Double.
Float
          Corresponding to the primitive type float (java.lang.Float.TYPE) and the non-primitive wrapper class java.lang.Float.
Integer
          Constant corresponding to the primitive type int, represented by the java.lang.Integer.TYPE class, and the non-primitive wrapper class java.lang.Integer.
Long
          Corresponding to the primitive type long (java.lang.Long.TYPE) and the non-primitive wrapper class java.lang.Long.
Short
          Corresponding to the primitive type short (java.lang.Short.TYPE) and the non-primitive wrapper class java.lang.Short.
 
Field Summary
 Class<?> nonPrimitive
          The non-primitive wrapper class associated with this enumeration type.
 Class<?> primitive
          The primitive type associated with this enumeration type, e.g.
 
Method Summary
<T> T
asPrimitive(Object value, Class<T> type)
          Returns the value supplied as value wrapped in the non-primitive type corresponding to the (primitive) type supplied as type.
protected abstract  Object asPrimitive(Object value, Primitive primitive)
          Returns the value supplied as value wrapped in the non-primitive type stored in in primitive.
abstract  Object getDefaultValue()
          Returns the default primitive value of the primitive type associated with this enumeration type, wrapped in a wrapper class.
static Object getDefaultValue(Class<?> type)
          Returns the default primitive value of the primitive (wrapper) type supplied as type, wrapped in an appropriate wrapper class, or null if type is neither primitive nor a primitive wrapper class.
static Map<Class<?>,Class<?>> getNonPrimitivesMap()
          Returns a Map where the keys are the non-primitive primitives having an associated primitive type, which is also the value associated with a key.
static Class<?> getNonPrimitiveType(Class<?> type)
          Returns the non-primitive type corresponding to the (primitive) type supplied as type, if any.
static Class<?> getNonPrimitiveType(Primitive primitive)
          Returns the non-primitive type corresponding to the enumeration type supplied as primitive; for example java.lang.Integer for Integer.
static Class<?>[] getNonPrimitiveTypes(Class<?>... primitives)
          Returns an array of the non-primitives corresponding to the primitives supplied in primitives.
static Class<?>[] getNonPrimitiveTypes(Primitive primitive, Primitive... primitives)
          Returns an array containing the non-primitives corresponding to the enumeration primitives supplied in primitive and primitives.
static Primitive getPrimitive(Class<?> type)
          Returns the enumeration type corresponding to type, if any; for example Integer for either java.lang.Integer or int (java.lang.Integer.TYPE).
static Map<Class<?>,Class<?>> getPrimitivesMap()
          Returns a Map where the keys are the primitive primitives, and the values the associated non-primitives.
static Class<?> getPrimitiveType(Class<?> type)
          Returns the primitive type corresponding to the type supplied as type, if any.
static Class<?> getPrimitiveType(Primitive primitive)
          Returns the primitive type corresponding to the enumeration type supplied as primitive; for example int (java.lang.Integer.TYPE) for Integer.
static Class<?>[] getPrimitiveTypes(Class<?>... primitives)
          Returns an array containing the primitives corresponding to the primitives supplied in primitives.
static Class<?>[] getPrimitiveTypes(Primitive primitive, Primitive... primitives)
          Returns an array containing the primitives corresponding to the enumeration primitives supplied in primitive and primitives.
 boolean isAssignableFromIgnorePrimitives(Class<?> type)
          Returns true if the non-primitive type of this enumeration type is assignable from the non-primitive type of type.
static boolean isAssignableFromIgnorePrimitives(Class<?>[] a, Class<?>[] b)
          Returns true if and only if: a and b are both null or identical (==); or a and b contains the same number of elements, and each class in a is assignable from the class in b at the corresponding index ignoring primitives, respectively.
static boolean isAssignableFromIgnorePrimitives(Class<?> a, Class<?> b)
          Returns true if a is assignable from b when primitive primitives are ignored.
 boolean isCompatibleWith(Class<?> type)
          Returns true if the type corresponding to type is compatible with this enumeration type, false if not.
abstract  boolean isCompatibleWith(Primitive primitive)
          Returns true if the type supplied as primitive is compatible with this enumeration type, false if not.
 boolean isEqualIgnorePrimitives(Class<?> type)
          Returns true if the non-primitive type of type represents the same non-primitive type as this enumeration type does, false if not.
static boolean isEqualIgnorePrimitives(Class<?>[] a, Class<?>[] b)
          Returns true if and only if: a and b are both null or identical (==); or a and b contains the same number of elements and primitives at the same index in a and b have the same non-primitive type (using equals).
static boolean isEqualIgnorePrimitives(Class<?> a, Class<?> b)
          Returns true if and only if: a and b are both null; or a and b have the same non-primitive type.
private static Primitive[] merge(Primitive primitive, Primitive... primitives)
          Merges primitive with primitives.
static String toString(Object object)
          Returns the string representing of object that ensures that arrays of primitives are formatted correctly.
static Primitive valueOf(String name)
          Returns the enum constant of this type with the specified name.
static Primitive[] values()
          Returns an array containing the constants of this enum type, in the order they are declared.
 
Methods inherited from class java.lang.Enum
clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Enum Constant Detail

Boolean

public static final Primitive Boolean
Corresponding to the primitive type boolean (java.lang.Boolean.TYPE) and the non-primitive wrapper class java.lang.Boolean.

This type is compatible with the following primitives:

A value of true will be translated to one (1), and a value of false will be translated to zero (0).


Byte

public static final Primitive Byte
Corresponding to the primitive type byte (java.lang.Byte.TYPE) and the non-primitive wrapper class java.lang.Byte.

This type is compatible with the following primitives:


Character

public static final Primitive Character
Corresponding to the primitive type char (java.lang.Character.TYPE) and the non-primitive wrapper class java.lang.Character.

This type is compatible with the following primitives:


Double

public static final Primitive Double
Corresponding to the primitive type double (java.lang.Double.TYPE) and the non-primitive wrapper class java.lang.Double.

This type is only compatible with itself.


Float

public static final Primitive Float
Corresponding to the primitive type float (java.lang.Float.TYPE) and the non-primitive wrapper class java.lang.Float.

This type is compatible with the following primitives:

Note that neither Integer or Long are not compatible, since the decimal points would then be truncated.


Integer

public static final Primitive Integer
Constant corresponding to the primitive type int, represented by the java.lang.Integer.TYPE class, and the non-primitive wrapper class java.lang.Integer.

This type is compatible with the following types:


Long

public static final Primitive Long
Corresponding to the primitive type long (java.lang.Long.TYPE) and the non-primitive wrapper class java.lang.Long.

This type is compatible with the following primitives:


Short

public static final Primitive Short
Corresponding to the primitive type short (java.lang.Short.TYPE) and the non-primitive wrapper class java.lang.Short.

This type is compatible with the following primitives:

Field Detail

nonPrimitive

public final Class<?> nonPrimitive
The non-primitive wrapper class associated with this enumeration type.

Never null.


primitive

public final Class<?> primitive
The primitive type associated with this enumeration type, e.g. java.lang.Integer.TYPE for the primitive type int.

Never null.

Method Detail

asPrimitive

public final <T> T asPrimitive(Object value,
                               Class<T> type)
Returns the value supplied as value wrapped in the non-primitive type corresponding to the (primitive) type supplied as type.

value must have the type stored as the non-primitive type for this enumeration type.

Type Parameters:
T - The desired type.
Parameters:
value - The value; can be null.
type - The type as a class literal; cannot be null.
Returns:
The value as the compatible type supplied as type; can be null.
Throws:
NullPointerException - If type is null.
IllegalArgumentException - If type is not compatible with this type, or if value is not null and not an instance of the non-primitive type of this type.

asPrimitive

protected abstract Object asPrimitive(Object value,
                                      Primitive primitive)
Returns the value supplied as value wrapped in the non-primitive type stored in in primitive.

Parameters:
value - The value; never null and known to be of correct type.
primitive - The type to represent value as; never null and known to be compatible.
Returns:
The value as the compatible type; never null.

getDefaultValue

public abstract Object getDefaultValue()
Returns the default primitive value of the primitive type associated with this enumeration type, wrapped in a wrapper class.

Returns:
The default primitive value; never null.

getDefaultValue

public static final Object getDefaultValue(Class<?> type)
Returns the default primitive value of the primitive (wrapper) type supplied as type, wrapped in an appropriate wrapper class, or null if type is neither primitive nor a primitive wrapper class.

Returns:
The default primitive value; or null if neither primitive nor a primitive wrapper class.
Throws:
NullPointerException - If type is null.
See Also:
getDefaultValue()

getNonPrimitivesMap

public static final Map<Class<?>,Class<?>> getNonPrimitivesMap()
Returns a Map where the keys are the non-primitive primitives having an associated primitive type, which is also the value associated with a key.

Returns:
A map; never null.

getNonPrimitiveType

public static final Class<?> getNonPrimitiveType(Class<?> type)
Returns the non-primitive type corresponding to the (primitive) type supplied as type, if any.

If type is non-primitive, it is simply returned.

Parameters:
type - The type to find the corresponding non-primitive type for; cannot be null.
Returns:
The corresponding non-primitive type; never null.
Throws:
NullPointerException - If type is null.

getNonPrimitiveType

public static final Class<?> getNonPrimitiveType(Primitive primitive)
Returns the non-primitive type corresponding to the enumeration type supplied as primitive; for example java.lang.Integer for Integer.

Parameters:
primitive - The primitive; cannot be null.
Returns:
The corresponding non-primitive type; never null.
Throws:
NullPointerException - If primitive is null.

getNonPrimitiveTypes

public static final Class<?>[] getNonPrimitiveTypes(Class<?>... primitives)
Returns an array of the non-primitives corresponding to the primitives supplied in primitives.

If primitives is null, or has zero length, all non-primitive primitives having an associated primitive type will be returned. The order corresponds to the order of the Primitive enumeration.

If primitives is not null, the length of the returned array is always equal to the length of primitives. primitives may contain duplicates. Non-primitives are included as is.

The order is maintained in the returned array.

Parameters:
primitives - An array of primitives; can be null.
Returns:
An array containing the corresponding non-primitives, in the same order as primitives; never null.

getNonPrimitiveTypes

public static final Class<?>[] getNonPrimitiveTypes(Primitive primitive,
                                                    Primitive... primitives)
Returns an array containing the non-primitives corresponding to the enumeration primitives supplied in primitive and primitives.

If primitive is null and primitives is null or empty, the returned array will contain all non-primitives having an associated primitive type. The order corresponds to the declaration order of this enumeration.

primitive and primitives may contain duplicates.

The order is maintained in the returned array.

Parameters:
primitive - The first primitive; can be null.
primitives - The last primitives; can contain null entries.
Returns:
An array containing the corresponding non-primitives, in the same order as defined by primitive and primitives; never null.

getPrimitive

public static final Primitive getPrimitive(Class<?> type)
Returns the enumeration type corresponding to type, if any; for example Integer for either java.lang.Integer or int (java.lang.Integer.TYPE).

Parameters:
type - The type; cannot be null.
Returns:
The corresponding enumeration type, or null.
Throws:
NullPointerException - If type is null.

getPrimitivesMap

public static final Map<Class<?>,Class<?>> getPrimitivesMap()
Returns a Map where the keys are the primitive primitives, and the values the associated non-primitives.

Returns:
A map; never null.

getPrimitiveType

public static final Class<?> getPrimitiveType(Class<?> type)
Returns the primitive type corresponding to the type supplied as type, if any. If type is primitive, it is simply returned.

If no such primitive type can be found, null will be returned.

Parameters:
type - The type to find the corresponding primitive type for; cannot be null.
Returns:
The corresponding primitive type, or null.
Throws:
NullPointerException - If type is null.

getPrimitiveType

public static final Class<?> getPrimitiveType(Primitive primitive)
Returns the primitive type corresponding to the enumeration type supplied as primitive; for example int (java.lang.Integer.TYPE) for Integer.

Parameters:
primitive - The primitive; cannot be null.
Returns:
The corresponding primitive type; never null.
Throws:
NullPointerException - If primitive is null.

getPrimitiveTypes

public static final Class<?>[] getPrimitiveTypes(Class<?>... primitives)
Returns an array containing the primitives corresponding to the primitives supplied in primitives.

If primitives is null, or has zero length, all primitive primitives will be returned. The order corresponds to the order of the Primitive enumeration.

Primitives having no associated primitive type are ignored, and primitives may contain duplicates. Primitive primitives are included as is.

The order is maintained in the returned array.

Parameters:
primitives - An array of primitives; can be null.
Returns:
An array containing the corresponding primitives, if any, in the same order as primitives; never null.

getPrimitiveTypes

public static final Class<?>[] getPrimitiveTypes(Primitive primitive,
                                                 Primitive... primitives)
Returns an array containing the primitives corresponding to the enumeration primitives supplied in primitive and primitives.

If primitive is null and primitives is null or empty, the returned array will contain all primitives. The order corresponds to the order of the Primitive enumeration.

primitive and primitives may contain duplicates.

The order is maintained in the returned array.

Parameters:
primitive - The first primitive; can be null.
primitives - The last primitives; can contain null entries.
Returns:
An array containing the corresponding primitives, in the same order as defined by primitive and primitives; never null.

isAssignableFromIgnorePrimitives

public boolean isAssignableFromIgnorePrimitives(Class<?> type)
Returns true if the non-primitive type of this enumeration type is assignable from the non-primitive type of type.

Parameters:
type - The type to test; cannot be null.
Returns:
True if the non-primitive type of this type is assignable from the non-primitive type of type, false if not.
Throws:
NullPointerException - If type is null.

isAssignableFromIgnorePrimitives

public static final boolean isAssignableFromIgnorePrimitives(Class<?>[] a,
                                                             Class<?>[] b)
Returns true if and only if:

  1. a and b are both null or identical (==); or

  2. a and b contains the same number of elements, and each class in a is assignable from the class in b at the corresponding index ignoring primitives, respectively.

For example, if a[x] is a Number and b[x] is int (Integer.TYPE), then the normal assignable test would fail. Here, int is treated as an Integer, and the assignable test succeeds.

Parameters:
a - The first array; can be null.
b - The second array; can be null.
Returns:
True if each class in a is assignable from each class in b at the same index, respectively, false if not.
Throws:
NullPointerException - If a or b contains a null entry.

isAssignableFromIgnorePrimitives

public static final boolean isAssignableFromIgnorePrimitives(Class<?> a,
                                                             Class<?> b)
Returns true if a is assignable from b when primitive primitives are ignored.

For example, if a is a Number and b is int (Integer.TYPE), then the normal assignable test would fail. Here, int is treated as an Integer, and the assignable test succeeds.

Parameters:
a - The first type; cannot be null.
b - The second type; cannot be null.
Returns:
True if a is assignable from b ignoring primitives, false if not.
Throws:
NullPointerException - If either argument is null.

isCompatibleWith

public boolean isCompatibleWith(Class<?> type)
Returns true if the type corresponding to type is compatible with this enumeration type, false if not.

The corresponding enumeration type is compatible if an actual primitive value of this type can be (implicitly) represented by the corresponding enumeration type without loss of precision. For example, the primitive int value stored in a java.lang.Integer wrapper can also be represented by by a java.lang.Long wrapper without loss of precision.

If the corresponding enumeration type is compatible with this type, it is safe to invoke asPrimitive(Object, Class) on this type using type as the second argument.

Parameters:
type - The type to test; cannot be null.
Returns:
True if compatible, false if not.
Throws:
NullPointerException - If type is null.
See Also:
getPrimitive(Class)

isCompatibleWith

public abstract boolean isCompatibleWith(Primitive primitive)
Returns true if the type supplied as primitive is compatible with this enumeration type, false if not.

primitive is compatible if an actual primitive value of this type can be (implicitly) represented by primitive without loss of precision. For example, the primitive int value stored in a java.lang.Integer wrapper can also be represented by by a java.lang.Long wrapper without loss of precision.

If primitive is compatible with this type, it is safe to invoke asPrimitive(Object, Class) on this type the non-primitive type associated with primitive as the second argument.

Parameters:
primitive - The primitive to test; cannot be null.
Returns:
True if compatible, false if not.
Throws:
NullPointerException - If primitive is null.

isEqualIgnorePrimitives

public boolean isEqualIgnorePrimitives(Class<?> type)
Returns true if the non-primitive type of type represents the same non-primitive type as this enumeration type does, false if not.

Parameters:
type - The type to test; can be null in which case false is returned.
Returns:
True if equal, false if not.

isEqualIgnorePrimitives

public static final boolean isEqualIgnorePrimitives(Class<?>[] a,
                                                    Class<?>[] b)
Returns true if and only if:

  1. a and b are both null or identical (==); or

  2. a and b contains the same number of elements and primitives at the same index in a and b have the same non-primitive type (using equals).

Parameters:
a - The first array; can be null.
b - The second array; can be null.
Returns:
True if equal, false if not.
Throws:
NullPointerException - If a or b contains a null entry.

isEqualIgnorePrimitives

public static final boolean isEqualIgnorePrimitives(Class<?> a,
                                                    Class<?> b)
Returns true if and only if:

  1. a and b are both null; or

  2. a and b have the same non-primitive type.

Parameters:
a - The first type; can be null.
b - The second type; can be null.
Returns:
True if equal, false if not.

merge

private static final Primitive[] merge(Primitive primitive,
                                       Primitive... primitives)
Merges primitive with primitives.

Null Primitive objects are ignored.

Parameters:
primitive - The first primitive; can be null (ignored).
primitives - The remaining primitives; can contain null entries (ignored).
Returns:
An array containing the (non-null) Primitive objects; never null.

toString

public static final String toString(Object object)
Returns the string representing of object that ensures that arrays of primitives are formatted correctly.

If object is null, the string null is returned. If the class of object is not an array type, the object.toString() is returned.

If the class of object is an array and the component type is primitive, the the proper Arrays.toString(..) method is invoked, e.g. Arrays.toString(boolean[]) for the boolean primitive type. If the component type is not primitive, the Arrays.toString(Object[]) method is used.

Parameters:
object - The object to format into a string; can be null.
Returns:
The string representation; never null.

valueOf

public static Primitive valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)

Parameters:
name - the name of the enum constant to be returned.
Returns:
the enum constant with the specified name
Throws:
IllegalArgumentException - if this enum type has no constant with the specified name
NullPointerException - if the argument is null

values

public static Primitive[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
for (Primitive c : Primitive.values())
    System.out.println(c);

Returns:
an array containing the constants of this enum type, in the order they are declared

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.