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

uk.co.it.modular.beans.ValueFactories Maven / Gradle / Ivy

Go to download

Utilities for manipulating and inspecting Java classes implementing the Java Beans standard

There is a newer version: 0.9.10
Show newest version
/*
 * Copyright (c) Modular IT Limited.
 */

package uk.co.it.modular.beans;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.apache.commons.lang.math.RandomUtils.*;
import static org.apache.commons.lang.time.DateUtils.addSeconds;

/**
 * @author Stewart Bissett
 */
@SuppressWarnings({
		"rawtypes", "unchecked"
})
public abstract class ValueFactories {

	private static final int MAX_STRING_LENGTH = 50;
	private static final int MINUTES_PER_HOUR = 60;
	private static final int HOURS_PER_DAY = 24;
	private static final int DAYS_PER_YEAR = 365;
	private static final int SECONDS_IN_A_YEAR = MINUTES_PER_HOUR * HOURS_PER_DAY * DAYS_PER_YEAR;

	public static ValueFactory theValue(final Object value) {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) value;
			}
		};
	}

	public static ValueFactory aNullValue() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return null;
			}
		};
	}

	public static ValueFactory aRandomString() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) randomAlphanumeric(MAX_STRING_LENGTH);
			}
		};
	}

	public static ValueFactory aRandomInteger() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) Integer.valueOf(nextInt());
			}
		};
	}

	public static ValueFactory aRandomShort() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) Short.valueOf((short) nextInt(Short.MAX_VALUE));
			}
		};
	}

	public static ValueFactory aRandomLong() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) Long.valueOf(nextLong());
			}
		};
	}

	public static ValueFactory aRandomDouble() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) Double.valueOf(nextDouble());
			}
		};
	}

	public static ValueFactory aRandomFloat() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) Float.valueOf(nextFloat());
			}
		};
	}

	public static ValueFactory aRandomBoolean() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) Boolean.valueOf(nextBoolean());
			}
		};
	}

	public static ValueFactory aRandomDate() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) addSeconds(new Date(), nextInt(SECONDS_IN_A_YEAR));
			}
		};
	}

	public static ValueFactory aRandomDecimal() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) BigDecimal.valueOf((Double) aRandomDouble().createValue(type));
			}
		};
	}

	public static ValueFactory aRandomByte() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				Byte value = (byte) nextInt(Byte.MAX_VALUE);
				return (T) value;
			}
		};
	}

	public static ValueFactory aRandomChar() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				Character value = randomAlphabetic(1).charAt(0);
				return (T) value;
			}
		};
	}

	public static ValueFactory aRandomEnum() {
		return new ValueFactory() {

			public  T createValue(final Class type) {

				if (!type.isEnum()) {
					throw new IllegalArgumentException("Cannot instantiate non-enum type '" + type.getCanonicalName() + "'");
				}

				Object[] enumerationValues = type.getEnumConstants();
				if (enumerationValues.length == 0) {
					return null;
				} else {
					return (T) enumerationValues[nextInt(enumerationValues.length)];
				}
			}
		};
	}

	public static  ValueFactory aRandomArrayOf(final Class type, final ValueFactory typeFactory) {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				int size = 1;
				Object array = Array.newInstance(type, size);
				if (array != null) {
					for (int i = 0; i < size; ++i) {
						Array.set(array, i, typeFactory.createValue(type));
					}
				}
				return (T) array;
			}
		};
	}

	public static ValueFactory aMap() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) new HashMap();
			}
		};
	}

	public static ValueFactory aSet() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) new HashSet();
			}
		};
	}

	public static ValueFactory aList() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				return (T) new ArrayList();
			}
		};
	}

	public static ArrayFactory anArray() {
		return new ArrayFactory() {

			public  T createValue(final Class type, final int size) {
				return (T) Array.newInstance(type, size);
			}
		};
	}

	public static ValueFactory aNewInstance() {
		return new ValueFactory() {

			public  T createValue(final Class type) {
				try {
					return type.newInstance();
				} catch (Exception e) {
					throw new BeanBuilderException("Failed to instantiate instance of '" + type.getCanonicalName() + "'", e);
				}
			}
		};
	}

	public static  ValueFactory aNewInstanceOf(final Class type) {
		return new ValueFactory() {

			public  I createValue(final Class inner) {
				try {
					if (!inner.isAssignableFrom(type)) {
						throw new IllegalArgumentException("Cannot create instance of '" + type.getCanonicalName() + "' from '" + inner.getCanonicalName() + "'");
					}
					return (I) type.newInstance();
				} catch (Exception e) {
					throw new BeanBuilderException("Failed to instantiate instance of '" + type.getCanonicalName() + "'", e);
				}
			}
		};
	}

	public static ValueFactory oneOf(final ValueFactory... factories) {
		return oneOf(Arrays.asList(factories));
	}

	public static ValueFactory oneOf(final Collection factories) {
		return new ValueFactory() {

			private final List candidates = new ArrayList(factories);

			public  T createValue(final Class type) {
				return candidates.get(nextInt(candidates.size())).createValue(type);
			}
		};
	}
}