Java SQL Type getDefaultValue(Class type)

Here you can find the source of getDefaultValue(Class type)

Description

Get the default value for for a type if one is available OR null if there is no default (since null sorta is the default)

License

Apache License

Parameter

Parameter Description
T a parameter
type any class type including primitives

Return

the default value OR null if there is no default

Declaration

@SuppressWarnings("unchecked")
public static <T> T getDefaultValue(Class<T> type) 

Method Source Code

//package com.java2s;
/**//  w  ww. j  a v a 2 s . c  o m
 * $Id: ConstructorUtils.java 61 2009-09-25 11:14:16Z azeckoski $
 * $URL: http://reflectutils.googlecode.com/svn/trunk/src/main/java/org/azeckoski/reflectutils/ConstructorUtils.java $
 * FieldUtils.java - genericdao - May 19, 2008 10:10:15 PM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Aaron Zeckoski
 * Licensed under the Apache License, Version 2.0
 * 
 * A copy of the Apache License has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Aaron Zeckoski (azeckoski @ gmail.com) (aaronz @ vt.edu) (aaron @ caret.cam.ac.uk)
 */

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;

import java.util.Date;
import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<Class<?>, Object> immutableDefaults = null;
    private static Map<Class<?>, Object> primitiveDefaults = null;
    private static char c;

    /**
     * Get the default value for for a type if one is available OR null if there is no default (since null sorta is the default)
     * @param <T>
     * @param type any class type including primitives
     * @return the default value OR null if there is no default
     */
    @SuppressWarnings("unchecked")
    public static <T> T getDefaultValue(Class<T> type) {
        T val = null;
        if (getPrimitiveDefaults().containsKey(type)) {
            val = (T) getPrimitiveDefaults().get(type);
        } else if (getImmutableDefaults().containsKey(type)) {
            val = (T) getImmutableDefaults().get(type);
        }
        return val;
    }

    /**
     * @return the map of all primitive types -> the default values for those types
     */
    public static synchronized Map<Class<?>, Object> getPrimitiveDefaults() {
        if (primitiveDefaults == null || primitiveDefaults.isEmpty()) {
            makePrimitiveDefaultsMap();
        }
        return primitiveDefaults;
    }

    /**
     * @return the map of all immutable types -> the default values for those types
     */
    public static synchronized Map<Class<?>, Object> getImmutableDefaults() {
        if (immutableDefaults == null || immutableDefaults.isEmpty()) {
            makeImmutableDefaultsMap();
        }
        return immutableDefaults;
    }

    private static void makePrimitiveDefaultsMap() {
        primitiveDefaults = new HashMap<Class<?>, Object>();
        primitiveDefaults.put(boolean.class, (Boolean) false);
        primitiveDefaults.put(byte.class, (Byte) (byte) 0);
        primitiveDefaults.put(char.class, (Character) c);
        primitiveDefaults.put(double.class, (Double) 0.0D);
        primitiveDefaults.put(float.class, (Float) 0.0F);
        primitiveDefaults.put(int.class, (Integer) 0);
        primitiveDefaults.put(long.class, (Long) 0L);
        primitiveDefaults.put(short.class, (Short) (short) 0);
    }

    private static void makeImmutableDefaultsMap() {
        immutableDefaults = new HashMap<Class<?>, Object>();
        immutableDefaults.put(BigDecimal.class, new BigDecimal(0));
        immutableDefaults.put(BigInteger.class, BigInteger.valueOf(0l));
        immutableDefaults.put(Boolean.class, Boolean.FALSE);
        immutableDefaults.put(Byte.class, Byte.valueOf((byte) 0));
        immutableDefaults.put(Character.class, (Character) c);
        immutableDefaults.put(Date.class, new Date(0));
        immutableDefaults.put(Double.class, Double.valueOf(0));
        immutableDefaults.put(Float.class, Float.valueOf(0));
        immutableDefaults.put(Long.class, Long.valueOf(0));
        immutableDefaults.put(Integer.class, Integer.valueOf(0));
        immutableDefaults.put(String.class, "");
        immutableDefaults.put(Short.class, Short.valueOf((short) 0));
        immutableDefaults.put(Timestamp.class, new Timestamp(0));
    }
}

Related

  1. getColumnType(String type)
  2. getConnection(String jdbcUrl, Properties properties)
  3. getDbDataType(Object o)
  4. getDefaultPrecision(int sqlType)
  5. getDefaultScale(int sqlType)
  6. getDoubleTypeString(Connection conn)
  7. getDoubleTypeString(Connection conn)
  8. getExpressionChoices(int type)
  9. getFormatTypeAsString(int fieldType)