com.gregmarut.support.beangenerator.DefaultValues Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-beangen Show documentation
Show all versions of test-beangen Show documentation
Supports unit testing by dynamically creating bean objects and populating their fields to default values.
/*******************************************************************************
*
* Copyright (c) 2015 Greg Marut.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Greg Marut - initial API and implementation
*
******************************************************************************/
package com.gregmarut.support.beangenerator;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.gregmarut.support.beangenerator.value.StaticValue;
import com.gregmarut.support.beangenerator.value.StringValue;
import com.gregmarut.support.beangenerator.value.Value;
/**
*
* A map of classes that hold the default objects for setting on fields in
* a model object. Custom values can be added to this class by calling the
* {@link Map}'s "put" method.
*
* By default, Strings have been purposely omitted from the default values.
* A String field's default value is the name of the field that it is assigning/
*
*
* @author Greg Marut
*/
public class DefaultValues
{
// holds the initial default values
public static final Integer DEFAULT_INTEGER = 1;
public static final Short DEFAULT_SHORT = 1;
public static final Float DEFAULT_FLOAT = 1.0f;
public static final Double DEFAULT_DOUBLE = 1.0;
public static final Long DEFAULT_LONG = 1L;
public static final Boolean DEFAULT_BOOLEAN = true;
public static final Byte DEFAULT_BYTE = (byte) 0x01;
public static final Character DEFAULT_CHARACTER = 'A';
public static final Date DEFAULT_DATE = new Date();
public static final Class> DEFAULT_CLASS = Class.class;
// holds the map that stores the class to value mapping
private final Map, Value>> map;
/**
* Constructs the default values map
*/
public DefaultValues()
{
map = new HashMap, Value>>();
// add the default values to the map
setupDefaultValues();
}
/**
* Sets up the map with all of the default values
*/
protected void setupDefaultValues()
{
put(String.class, new StringValue());
put(Integer.class, DEFAULT_INTEGER);
put(Integer[].class, new Integer[] { DEFAULT_INTEGER });
put(int.class, DEFAULT_INTEGER);
put(int[].class, new int[] { DEFAULT_INTEGER });
put(Short.class, DEFAULT_SHORT);
put(Short[].class, new Short[] { DEFAULT_SHORT });
put(short.class, DEFAULT_SHORT);
put(short[].class, new short[] { DEFAULT_SHORT });
put(Float.class, DEFAULT_FLOAT);
put(Float[].class, new Float[] { DEFAULT_FLOAT });
put(float.class, DEFAULT_FLOAT);
put(float[].class, new float[] { DEFAULT_FLOAT });
put(Double.class, DEFAULT_DOUBLE);
put(Double[].class, new Double[] { DEFAULT_DOUBLE });
put(double.class, DEFAULT_DOUBLE);
put(double[].class, new double[] { DEFAULT_DOUBLE });
put(Long.class, DEFAULT_LONG);
put(Long[].class, new Long[] { DEFAULT_LONG });
put(long.class, DEFAULT_LONG);
put(long[].class, new long[] { DEFAULT_LONG });
put(Boolean.class, DEFAULT_BOOLEAN);
put(Boolean[].class, new Boolean[] { DEFAULT_BOOLEAN });
put(boolean.class, DEFAULT_BOOLEAN);
put(boolean[].class, new boolean[] { DEFAULT_BOOLEAN });
put(Byte.class, DEFAULT_BYTE);
put(Byte[].class, new Byte[] { DEFAULT_BYTE });
put(byte.class, DEFAULT_BYTE);
put(byte[].class, new byte[] { DEFAULT_BYTE });
put(Character.class, DEFAULT_CHARACTER);
put(Character[].class, new Character[] { DEFAULT_CHARACTER });
put(char.class, DEFAULT_CHARACTER);
put(char[].class, new char[] { DEFAULT_CHARACTER });
put(Date.class, DEFAULT_DATE);
put(Class.class, DEFAULT_CLASS);
}
@SuppressWarnings("unchecked")
public final void put(final Class key, final V value)
{
// make sure the value is not null
if (null == value)
{
throw new IllegalArgumentException("value cannot be null.");
}
// create a new value object
map.put(key, new StaticValue(value, (Class) value.getClass()));
}
public final void put(final Class key, final Value value)
{
// make sure the value is not null
if (null == value)
{
throw new IllegalArgumentException("value cannot be null.");
}
// create a new value object
map.put(key, value);
}
@SuppressWarnings("unchecked")
public final Value get(final Class key)
{
// retrieve the value object from the map
return (Value) map.get(key);
}
@SuppressWarnings("unchecked")
public final Value remove(final Class key)
{
// remove the object from the map if it exists
return (Value) map.remove(key);
}
public final boolean containsKey(final Class> key)
{
return map.containsKey(key);
}
}