001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.util.converter;
018:
019: import java.text.NumberFormat;
020:
021: /**
022: * Convert to and from numbers. <br>
023: *
024: * The following convertions are supported:
025: *
026: * <table>
027: * <tr>
028: * <th>From</th>
029: * <th>To</th>
030: * <th>Reverse</th>
031: * </tr>
032: * <tr>
033: * <td>Number (and subclasses)</td>
034: * <td>Number (and subclasses)</td>
035: * <td>Yes</td>
036: * </tr>
037: * </table>
038: */
039: public class NumberConverters implements Converter {
040:
041: private static NumberFormat defaultFormat;
042:
043: private NumberFormat format;
044:
045: public NumberConverters() {
046: this (getDefaultFormat());
047: }
048:
049: public NumberConverters(NumberFormat format) {
050: this .format = format;
051: }
052:
053: public static NumberFormat getDefaultFormat() {
054: synchronized (NumberConverters.class) {
055: if (defaultFormat == null) {
056: defaultFormat = NumberFormat.getNumberInstance();
057: defaultFormat.setMinimumIntegerDigits(1);
058: defaultFormat.setMaximumIntegerDigits(64);
059: defaultFormat.setMinimumFractionDigits(0);
060: defaultFormat.setMaximumFractionDigits(64);
061: }
062: }
063: return defaultFormat;
064: }
065:
066: public void register(ConverterRegistry registry) {
067: registry.addConverter(Number.class, Double.class, this );
068: registry.addConverter(Number.class, Float.class, this );
069: registry.addConverter(Number.class, Integer.class, this );
070: registry.addConverter(Number.class, Long.class, this );
071: registry.addConverter(Number.class, Short.class, this );
072:
073: registry.addConverter(Double.class, Double.class, this );
074: registry.addConverter(Double.class, Float.class, this );
075: registry.addConverter(Double.class, Integer.class, this );
076: registry.addConverter(Double.class, Long.class, this );
077: registry.addConverter(Double.class, Short.class, this );
078: registry.addConverter(Double.class, String.class, this );
079:
080: registry.addConverter(Float.class, Double.class, this );
081: registry.addConverter(Float.class, Float.class, this );
082: registry.addConverter(Float.class, Integer.class, this );
083: registry.addConverter(Float.class, Long.class, this );
084: registry.addConverter(Float.class, Short.class, this );
085: registry.addConverter(Float.class, String.class, this );
086:
087: registry.addConverter(Integer.class, Double.class, this );
088: registry.addConverter(Integer.class, Float.class, this );
089: registry.addConverter(Integer.class, Integer.class, this );
090: registry.addConverter(Integer.class, Long.class, this );
091: registry.addConverter(Integer.class, Short.class, this );
092: registry.addConverter(Integer.class, String.class, this );
093:
094: registry.addConverter(Long.class, Double.class, this );
095: registry.addConverter(Long.class, Float.class, this );
096: registry.addConverter(Long.class, Integer.class, this );
097: registry.addConverter(Long.class, Long.class, this );
098: registry.addConverter(Long.class, Short.class, this );
099: registry.addConverter(Long.class, String.class, this );
100:
101: registry.addConverter(Short.class, Double.class, this );
102: registry.addConverter(Short.class, Float.class, this );
103: registry.addConverter(Short.class, Integer.class, this );
104: registry.addConverter(Short.class, Long.class, this );
105: registry.addConverter(Short.class, Short.class, this );
106: registry.addConverter(Short.class, String.class, this );
107:
108: registry.addConverter(String.class, Double.class, this );
109: registry.addConverter(String.class, Float.class, this );
110: registry.addConverter(String.class, Integer.class, this );
111: registry.addConverter(String.class, Long.class, this );
112: registry.addConverter(String.class, Short.class, this );
113: }
114:
115: public Object convert(Class targetType, Object value) {
116: // are we dealing with a number to number conversion?
117: if ((value instanceof Number)
118: && Number.class.isAssignableFrom(targetType)) {
119: if (Double.class.equals(targetType)) {
120: return new Double(((Number) value).doubleValue());
121: } else if (Float.class.equals(targetType)) {
122: return new Float(((Number) value).floatValue());
123: } else if (Integer.class.equals(targetType)) {
124: return new Integer(((Number) value).intValue());
125: } else if (Long.class.equals(targetType)) {
126: return new Long(((Number) value).longValue());
127: } else if (Short.class.equals(targetType)) {
128: return new Short(((Number) value).shortValue());
129: } else {
130: throw new IllegalArgumentException(
131: "this code must not be reached");
132: }
133: } else if ((value instanceof Number)
134: && String.class.equals(targetType)) {
135: if ((value instanceof Double) || (value instanceof Float)) {
136: return format.format(((Number) value).doubleValue());
137: } else {
138: return format.format(((Number) value).longValue());
139: }
140: } else if ((value instanceof String)
141: && Number.class.isAssignableFrom(targetType)) {
142: if (Double.class.equals(targetType)) {
143: return new Double((String) value);
144: } else if (Float.class.equals(targetType)) {
145: return new Float((String) value);
146: } else if (Integer.class.equals(targetType)) {
147: return new Integer((String) value);
148: } else if (Long.class.equals(targetType)) {
149: return new Long((String) value);
150: } else if (Short.class.equals(targetType)) {
151: return new Short((String) value);
152: } else {
153: throw new IllegalArgumentException(
154: "this code must not be reached");
155: }
156: }
157: throw new IllegalArgumentException("no conversion supported");
158: }
159:
160: }
|