Convert the given number into an instance of the given target class. : Number « Data Type « Java






Convert the given number into an instance of the given target class.

        
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * 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 jacky.lanlan.song.extension.struts.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.text.ParseException;

/**
 * Miscellaneous utility methods for number conversion and parsing.
 * Mainly for internal use within the framework; consider Jakarta's
 * Commons Lang for a more comprehensive suite of string utilities.
 *
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 1.1.2
 */
public abstract class NumberUtils {

  /**
   * Convert the given number into an instance of the given target class.
   * @param number the number to convert
   * @param targetClass the target class to convert to
   * @return the converted number
   * @throws IllegalArgumentException if the target class is not supported
   * (i.e. not a standard Number subclass as included in the JDK)
   * @see java.lang.Byte
   * @see java.lang.Short
   * @see java.lang.Integer
   * @see java.lang.Long
   * @see java.math.BigInteger
   * @see java.lang.Float
   * @see java.lang.Double
   * @see java.math.BigDecimal
   */
  public static Number convertNumberToTargetClass(Number number, Class<?> targetClass)
      throws IllegalArgumentException {

  //  Assert.notNull(number, "Number must not be null");
  //  Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
      return number;
    }
    else if (targetClass.equals(Byte.class)) {
      long value = number.longValue();
      if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
        raiseOverflowException(number, targetClass);
      }
      return new Byte(number.byteValue());
    }
    else if (targetClass.equals(Short.class)) {
      long value = number.longValue();
      if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
        raiseOverflowException(number, targetClass);
      }
      return new Short(number.shortValue());
    }
    else if (targetClass.equals(Integer.class)) {
      long value = number.longValue();
      if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
        raiseOverflowException(number, targetClass);
      }
      return new Integer(number.intValue());
    }
    else if (targetClass.equals(Long.class)) {
      return new Long(number.longValue());
    }
    else if (targetClass.equals(Float.class)) {
      return new Float(number.floatValue());
    }
    else if (targetClass.equals(Double.class)) {
      return new Double(number.doubleValue());
    }
    else if (targetClass.equals(BigInteger.class)) {
      return BigInteger.valueOf(number.longValue());
    }
    else if (targetClass.equals(BigDecimal.class)) {
      // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
      // (see BigDecimal javadoc for details)
      return new BigDecimal(number.toString());
    }
    else {
      throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" +
          number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
  }

  /**
   * Raise an overflow exception for the given number and target class.
   * @param number the number we tried to convert
   * @param targetClass the target class we tried to convert to
   */
  private static void raiseOverflowException(Number number, Class<?> targetClass) {
    throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" +
        number.getClass().getName() + "] to target class [" + targetClass.getName() + "]: overflow");
  }

}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Methods for number conversion and parsing
2.Get Digit Number String
3.Convert to numbers
4.Check Number properties and convert from Number
5.Check if number are in a given range
6.Various number-related routines and classes that are frequently used
7.Turns a string value into a java.lang.Number.
8.Provides IEEE-754r variants of NumberUtils methods.
9.Checks whether the String a valid Java number.
10.Specifies sizes of various types in bytes.
11.Class to represent 16-bit unsigned integers.
12.Implements an integer object wrapper which allows changing the integer value.Implements an integer object wrapper which allows changing the integer value.
13.Mixed Radix Number
14.This class allows a number to be converted to it's unsigned value.
15.Numbers will be equals if enough decimals are equals, without thinking about approximations.
16.Ternary Numnber
17.Implements a long object wrapper which allows changing the long value.