View Javadoc
1 /* 2 * Copyright (C) The Spice Group. All rights reserved. 3 * 4 * This software is published under the terms of the Spice 5 * Software License version 1.1, a copy of which has been included 6 * with this distribution in the LICENSE.txt file. 7 */ 8 package org.codehaus.spice.converter; 9 10 import org.codehaus.spice.salt.i18n.Resources; 11 import org.codehaus.spice.salt.i18n.ResourceManager; 12 13 /*** 14 * Instances of this interface are used to convert between different types. 15 * 16 * @author Peter Donald 17 * @version $Revision: 1.1 $ $Date: 2003/12/02 08:37:56 $ 18 */ 19 public abstract class AbstractConverter 20 implements Converter 21 { 22 /*** 23 * i18n resources accessor. 24 */ 25 private static final Resources REZ = 26 ResourceManager.getPackageResources( AbstractConverter.class ); 27 28 /*** 29 * The source type that converter can convert from. 30 */ 31 private final Class m_source; 32 33 /*** 34 * The destination type that converter can convert to. 35 */ 36 private final Class m_destination; 37 38 /*** 39 * The default value that is returned if can perform conversion. 40 */ 41 private final Object m_defaultValue; 42 43 /*** 44 * Constructor for a converter between types source and destination 45 * 46 * @param source the source type 47 * @param destination the destination type 48 */ 49 protected AbstractConverter( final Class source, final Class destination ) 50 { 51 this( source, destination, null ); 52 } 53 54 /*** 55 * Constructor for a converter between types source and destination 56 * with a default value. 57 * 58 * @param source the source type 59 * @param destination the destination type 60 * @param defaultValue the default value 61 */ 62 protected AbstractConverter( final Class source, 63 final Class destination, 64 final Object defaultValue ) 65 { 66 m_source = source; 67 m_destination = destination; 68 m_defaultValue = defaultValue; 69 } 70 71 /*** 72 * Convert an object from original to destination types 73 * 74 * @param destination the destination type 75 * @param original the original Object 76 * @param context the context in which to convert 77 * @return the converted object 78 * @exception ConverterException if an error occurs 79 */ 80 public Object convert( final Class destination, 81 final Object original, 82 final Object context ) 83 throws ConverterException 84 { 85 if( m_destination != destination ) 86 { 87 final String message = 88 REZ.format( "bad-destination.error", destination.getName(), m_destination ); 89 throw new IllegalArgumentException( message ); 90 } 91 92 if( !m_source.isInstance( original ) ) 93 { 94 final String message = 95 REZ.format( "bad-instance.error", original, m_source.getName() ); 96 throw new IllegalArgumentException( message ); 97 } 98 99 return convert( original, context ); 100 } 101 102 /*** 103 * A helper method to throw an exception indicating that could 104 * not perform conversion of specified object due to an exception. 105 */ 106 protected final Object noConvert( final Object value, final Throwable throwable ) 107 throws ConverterException 108 { 109 if( null != m_defaultValue ) 110 { 111 return m_defaultValue; 112 } 113 else 114 { 115 final String message = 116 REZ.format( "no-convert.error", 117 m_source.getName(), 118 m_destination.getName(), 119 value, 120 throwable ); 121 throw new ConverterException( message ); 122 } 123 } 124 125 /*** 126 * Overide this in a particular converter to do the conversion. 127 * 128 * @param original the original Object 129 * @param context the context in which to convert 130 * @return the converted object 131 * @throws ConverterException if an error occurs 132 */ 133 protected abstract Object convert( Object original, Object context ) 134 throws ConverterException; 135 } 136

This page was automatically generated by Maven