001 /* 002 * Copyright 2003-2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.convert1; 017 018 import org.apache.commons.convert1.string.*; 019 import org.apache.commons.convert1.array.*; 020 021 /** 022 * Basic tasks for the Converting system. Note, this class bears 023 * no resemblance to the BeanUtils ConvertUtils class. ConvertRegistry 024 * replaces that class. 025 * 026 * @author Henri Yandell 027 * @since 0.1 028 */ 029 public class ConvertUtils { 030 031 // array, collection, map converters 032 // these converters reuse other converters, so what this does is 033 // provide collection adaptors 034 public static void registerStandardCollectionConverters(ConvertRegistry registry) { 035 // TODO: Figure out if we have to register primitive arrays 036 // TODO: if long[] is not a subclass of Object[], then 037 // we cannot register a converter for it, as we have to 038 // know the toClass up front. Maybe allow a wildcard entry 039 // concept into the registry which maps a fromClass to any 040 // toClass. 041 registry.register( new ArrayConverter( registry ), Object[].class, Object[].class ); 042 } 043 044 // aka Parsers 045 public static void registerStandardFromStringConverters(ConvertRegistry registry) { 046 // primitives 047 registry.register( new BooleanConverter(), String.class, Boolean.TYPE ); 048 registry.register( new ByteConverter(), String.class, Byte.TYPE ); 049 registry.register( new CharacterConverter(), String.class, Character.TYPE ); 050 registry.register( new DoubleConverter(), String.class, Double.TYPE ); 051 registry.register( new FloatConverter(), String.class, Float.TYPE ); 052 registry.register( new IntegerConverter(), String.class, Integer.TYPE ); 053 registry.register( new LongConverter(), String.class, Long.TYPE ); 054 registry.register( new ShortConverter(), String.class, Short.TYPE ); 055 056 // objects 057 registry.register( new BigDecimalConverter(), String.class, java.math.BigDecimal.class ); 058 registry.register( new BigIntegerConverter(), String.class, java.math.BigInteger.class ); 059 registry.register( new BooleanConverter(), String.class, Boolean.class ); 060 registry.register( new ByteConverter(), String.class, Byte.class ); 061 registry.register( new CharacterConverter(), String.class, Character.class ); 062 registry.register( new ClassConverter(), String.class, Class.class ); 063 registry.register( new DoubleConverter(), String.class, Double.class ); 064 registry.register( new FileConverter(), String.class, java.io.File.class ); 065 registry.register( new FloatConverter(), String.class, Float.class ); 066 registry.register( new IntegerConverter(), String.class, Integer.class ); 067 registry.register( new LongConverter(), String.class, Long.class ); 068 registry.register( new ShortConverter(), String.class, Short.class ); 069 registry.register( new SqlDateConverter(), String.class, java.sql.Date.class ); 070 registry.register( new SqlTimeConverter(), String.class, java.sql.Time.class ); 071 registry.register( new SqlTimestampConverter(), String.class, java.sql.Timestamp.class ); 072 registry.register( new StringConverter(), String.class, String.class ); 073 registry.register( new URLConverter(), String.class, java.net.URL.class ); 074 075 registry.register( new ArrayConverter( registry ), String.class, Object[].class ); 076 // registry.register( new CollectionConverter( registry ), String.class, java.util.Collection.class ); 077 } 078 079 // aka Formatters 080 public static void registerStandardToStringConverters(ConvertRegistry registry) { 081 registry.register( new StringConverter(), String.class, String.class ); 082 } 083 084 // includes collections, parsers, formatters and more 085 public static void registerStandardConverters(ConvertRegistry registry) { 086 registerStandardToStringConverters(registry); 087 registerStandardFromStringConverters(registry); 088 registerStandardCollectionConverters(registry); 089 } 090 091 092 }