001 /* 002 * Copyright 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.convert.conversion; 017 018 import org.apache.commons.convert.Conversion; 019 import org.apache.commons.convert.ConversionFactory; 020 import org.apache.commons.convert.Converter; 021 022 /** 023 * Conversion from TimeZone to String using <code>getID()</code>. 024 * 025 * @author Stephen Colebourne 026 * @version $Id: IdentityConversionFactory.java 155441 2005-02-26 13:19:22Z dirkv $ 027 * @since 1.0 028 */ 029 public class IdentityConversionFactory implements ConversionFactory { 030 031 /** Singleton instance of this factory */ 032 public static final ConversionFactory INSTANCE = new IdentityConversionFactory(); 033 034 /** 035 * Restricted constructor. 036 */ 037 protected IdentityConversionFactory() { 038 super(); 039 } 040 041 //----------------------------------------------------------------------- 042 /** 043 * Checks if the types are equal, if so return 80. 044 * 045 * @param value the value to be converted, read only, may be null 046 * @param fromType the type to convert from, may be null 047 * @param toType the type to convert to, may be null 048 * @return 80 if types are equal 049 */ 050 public int getMatchPercent(Object value, Class fromType, Class toType) { 051 if (fromType == toType && fromType != null) { 052 return 80; 053 } 054 return 0; 055 } 056 057 /** 058 * Create a new conversion object for the specified from and to types. 059 * <p> 060 * This implementation returns the identity conversion. 061 * 062 * @param value the value to be converted, read only, may be null 063 * @param fromType the type to convert from, may be null 064 * @param toType the type to convert to, may be null 065 * @return a Conversion object for repeatedly performing conversions 066 */ 067 public Conversion getInstance(Object value, Class fromType, Class toType) { 068 return new IdentityConversion(fromType, toType); 069 } 070 071 /** 072 * Gets a string suitable for debugging. 073 * 074 * @return a debugging string 075 */ 076 public String toString() { 077 return "ConversionFactory[Identity]"; 078 } 079 080 //----------------------------------------------------------------------- 081 /** 082 * Generic conversion implementation that returns the input value. 083 */ 084 class IdentityConversion extends AbstractConversion { 085 086 /** 087 * Constructs a Conversion. 088 * 089 * @param fromType the type to convert from 090 * @param toType the type to convert to 091 */ 092 IdentityConversion(Class fromType, Class toType) { 093 super(fromType, toType); 094 } 095 096 /** 097 * Converts the input value by returning it unchanged. 098 * 099 * @param value the input value to be converted, pre-checked to not be null 100 * @param converter the converter being used, not null 101 * @return the input value 102 */ 103 public Object convert(Object value, Converter converter) { 104 return value; 105 } 106 } 107 108 }