Open Source Repository

Home /jodd/jodd-3.3.2 | Repository Home



jodd/typeconverter/impl/LongConverter.java
// Copyright (c) 2003-2012, Jodd Team (jodd.org). All Rights Reserved.

package jodd.typeconverter.impl;

import jodd.typeconverter.TypeConversionException;
import jodd.typeconverter.TypeConverter;
import jodd.util.StringUtil;

/**
 * Converts given object to a <code>Long</code>.
 * Conversion rules:
 <li><code>null</code> value is returned as <code>null</code>
 <li>object of destination type is simply casted
 <li>object is converted to string, trimmed, and then converted if possible.
 * Number string may start with plus and minus sign.
 */
public class LongConverter implements TypeConverter<Long> {

  public Long convert(Object value) {
    if (value == null) {
      return null;
    }

    if (value.getClass() == Long.class) {
      return (Longvalue;
    }
    if (value instanceof Number) {
      return Long.valueOf(((Number)value).longValue());
    }
    try {
      String stringValue = value.toString().trim();
      if (StringUtil.startsWithChar(stringValue, '+')) {
        stringValue = stringValue.substring(1);
      }
      return Long.valueOf(stringValue);
    catch (NumberFormatException nfex) {
      throw new TypeConversionException(value, nfex);
    }
  }

}