Package de.odysseus.calyxo.base.util

Source Code of de.odysseus.calyxo.base.util.ParseUtils

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.util;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Parse basic types.
*
* @author Christoph Beck
*/
public class ParseUtils {
  private static Locale locale = Locale.US;

  private static Object nullValue(Class type) {
    if (type.isPrimitive()) {
      if (type == boolean.class)
        return Boolean.FALSE;
      if (type == byte.class)
        return new Byte((byte)0);
      if (type == char.class)
        return new Character((char)0);
      if (type == short.class)
        return new Short((short)0);
      if (type == int.class)
        return new Integer(0);
      if (type == long.class)
        return new Long(0);
      if (type == float.class)
        return new Float(0);
      if (type == double.class)
        return new Double(0);
    }
    return null;
  }

  private static Class objectType(Class type) {
    if (type.isPrimitive()) {
      if (type == boolean.class)
        return Boolean.class;
      if (type == byte.class)
        return Byte.class;
      if (type == char.class)
        return Character.class;
      if (type == short.class)
        return Short.class;
      if (type == int.class)
        return Integer.class;
      if (type == long.class)
        return Long.class;
      if (type == float.class)
        return Float.class;
      if (type == double.class)
        return Double.class;
    }
    return type;
  }

  private static Object parse(Format format, String value) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Object result = format.parseObject(value, pos);
    if (pos.getIndex() < value.length())
      throw new ParseException("Cannot parse " + value + " (garbage suffix)!", pos.getIndex());
    return result;
  }

  private static Date parseDate(String value) throws ParseException {
    DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    return (Date)parse(format, value);
  }

  private static Boolean parseBoolean(String value) throws ParseException {
    if ("true".equals(value)) {
      return Boolean.TRUE;
    } else if ("false".equals(value)) {
      return Boolean.FALSE;
    } else {
      throw new ParseException("Cannot parse '" + value + "' as boolean", 0);
    }
  }

  private static Character parseCharacter(String value) throws ParseException {
    if (value.length() != 1) {
      throw new ParseException("Cannot parse '" + value + "' as character", value.length());
    }
    return new Character(value.charAt(0));
  }

  /**
   * Parse value of specified type. The string value has to be in
   * standard notation for the specified type.
   */
  public static Object parse(Class type, String value) throws Exception {
    if (value == null) {
      return nullValue(type);
    } else if (value.length() == 0) {
      return type == String.class ? value : nullValue(type);
    }

    type = objectType(type);

    if (type == BigDecimal.class) {
      return new BigDecimal(value);
    } else if (type == BigInteger.class) {
      return new BigInteger(value);
    } else if (type == Boolean.class) {
      return parseBoolean(value);
    } else if (type == Byte.class) {
      return Byte.valueOf(value);
    } else if (type == Character.class) {
      return parseCharacter(value);
    } else if (type == Date.class) {
      return parseDate(value);
    } else if (type == Double.class) {
      return Double.valueOf(value);
    } else if (type == Float.class) {
      return Float.valueOf(value);
    } else if (type == Integer.class) {
      return Integer.valueOf(value);
    } else if (type == Long.class) {
      return Long.valueOf(value);
    } else if (type == Short.class) {
      return Short.valueOf(value);
    } else if (type == String.class) {
      return value;
    }
    throw new ParseException("Cannot parse type " + type, 0);
  }
}
TOP

Related Classes of de.odysseus.calyxo.base.util.ParseUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.