You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2004/03/18 00:44:45 UTC
cvs commit: jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion DefaultValueConversionFactoryDecorator.java DefaultValueConversionDecorator.java
scolebourne 2004/03/17 15:44:45
Added: convert/src/java/org/apache/commons/convert2/conversion
DefaultValueConversionFactoryDecorator.java
DefaultValueConversionDecorator.java
Log:
Add default value decorators for conversions
Revision Changes Path
1.1 jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/DefaultValueConversionFactoryDecorator.java
Index: DefaultValueConversionFactoryDecorator.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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 org.apache.commons.convert2.conversion;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.ConversionFactory;
/**
* A specialized conversion factory that constructs conversion objects that are
* decorated by a <code>DefaultValueConversionDecorator</code> object.
* <p>
* As <code>DefaultValueConversionDecorator</code> decorates other conversions,
* this class decorates conversion factories. The conversions created through
* this factory all support typical default value features.
* <p>
* When created a <code>DefaultValueConversionFactoryDecorator</code> object is
* initialized with the conversion factory to be decorated, and the properties
* related to default values. The conversions created by this factory object
* will also be initialized with these properties.
*
* @see DefaultValueConversionDecorator
* @author Oliver Heger
* @author Stephen Colebourne
* @version $Id: DefaultValueConversionFactoryDecorator.java,v 1.1 2004/03/17 23:44:45 scolebourne Exp $
* @since 1.0
*/
public class DefaultValueConversionFactoryDecorator implements ConversionFactory {
/** The decorated conversion factory */
private ConversionFactory factory;
/** The default value to return */
private Object defaultValue;
/** Whether to default on a conversion exception */
private boolean defaultOnException;
//-----------------------------------------------------------------------
/**
* Constructs a new conversion factory decorator that also returns the
* default value when a <code>ConversionException</code> occurs.
*
* @param factory the conversion factory to be decorated
* @param defaultValue the default value
*/
public DefaultValueConversionFactoryDecorator(ConversionFactory factory, Object defaultValue) {
this(factory, defaultValue, false);
}
/**
* Constructs a new conversion factory decorator.
*
* @param factory the conversion factory to be decorated
* @param defaultValue the default value
* @param defaultOnException true to default on exception, false to throw exceptions
*/
public DefaultValueConversionFactoryDecorator(
ConversionFactory factory, Object defaultValue, boolean defaultOnException) {
super();
this.factory = factory;
this.defaultValue = defaultValue;
this.defaultOnException = defaultOnException;
}
//-----------------------------------------------------------------------
/**
* Gets the conversion factory being decorated.
*
* @return the decorated conversion factory
*/
public ConversionFactory getDecoratedConversionFactory() {
return factory;
}
/**
* Gets the default value to be returned.
*
* @return the default value
*/
public Object getDefaultValue() {
return defaultValue;
}
/**
* Gets whether the default value is to be returned when an exception occurs.
*
* @return true if defaultValue is returned in the case of an exception
*/
public boolean isDefaultOnException() {
return defaultOnException;
}
//-----------------------------------------------------------------------
/**
* Returns a numerical value for the appropriateness of this factory for the
* specified conversion. This implementation delegates to the decorated factory.
*
* @param value the value to be converted
* @param fromType the type to be converted from
* @param toType the type to be converted to
* @return a percentage value for the appropriateness of this class
*/
public int getMatchPercent(Object value, Class fromType, Class toType) {
return getDecoratedConversionFactory().getMatchPercent(value, fromType, toType);
}
/**
* Creates a conversion instance for the specified conversion.
* This implementation lets the decorated factory create an instance.
* This instance is then decorated with defaulting behaviour.
*
* @param value the value to be converted
* @param fromType the type to convert from
* @param toType the type to convert to
* @return a conversion instance
*/
public Conversion getInstance(Object value, Class fromType, Class toType) {
Conversion conversion = getDecoratedConversionFactory().getInstance(value, fromType, toType);
return new DefaultValueConversionDecorator(conversion, getDefaultValue(), isDefaultOnException());
}
}
1.1 jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/DefaultValueConversionDecorator.java
Index: DefaultValueConversionDecorator.java
===================================================================
/*
* Copyright 2004 The Apache Software Foundation
*
* 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 org.apache.commons.convert2.conversion;
import org.apache.commons.convert2.Conversion;
import org.apache.commons.convert2.Converter;
/**
* A specialized <code>Conversion</code> class that deals with default values
* for conversions.
* <p>
* This class acts as a <em>decorator</em> for other conversion implementations.
* It checks if the value to be converted is <b>null </b>, and optionally if an
* exception occurs during conversion. In either case it returns the defined
* default value to the caller.
* <p>
* Usage of this class is quite easy. Just create an instance and initialize it
* with the conversion object to be decorated. After properties have been set
* the new conversion object can be used wherever the original conversion
* would have been used.
*
* @see DefaultValueConversionFactoryDecorator
* @author Oliver Heger
* @author Stephen Colebourne
* @version $Id: DefaultValueConversionDecorator.java,v 1.1 2004/03/17 23:44:45 scolebourne Exp $
* @since 1.0
*/
public class DefaultValueConversionDecorator implements Conversion {
/** The decorated conversion */
private final Conversion conversion;
/** The default value to return */
private Object defaultValue;
/** Whether to default on a conversion exception */
private boolean defaultOnException;
//-----------------------------------------------------------------------
/**
* Constructs a new conversion decorator that also returns the default value
* when a <code>ConversionException</code> occurs.
*
* @param conversion the conversion to be decorated
* @param defaultValue the default value
*/
public DefaultValueConversionDecorator(Conversion conversion, Object defaultValue) {
this(conversion, defaultValue, false);
}
/**
* Constructs a new conversion decorator.
*
* @param conversion the conversion to be decorated
* @param defaultValue the default value
* @param defaultOnException true to default on exception, false to throw exceptions
*/
public DefaultValueConversionDecorator(
Conversion conversion, Object defaultValue, boolean defaultOnException) {
super();
this.conversion = conversion;
this.defaultValue = defaultValue;
this.defaultOnException = defaultOnException;
}
//-----------------------------------------------------------------------
/**
* Gets the conversion being decorated.
*
* @return the decorated conversion
*/
public Conversion getDecoratedConversion() {
return conversion;
}
/**
* Gets the default value to be returned.
*
* @return the default value
*/
public Object getDefaultValue() {
return defaultValue;
}
/**
* Gets whether the default value is to be returned when an exception occurs.
*
* @return true if defaultValue is returned in the case of an exception
*/
public boolean isDefaultOnException() {
return defaultOnException;
}
//-----------------------------------------------------------------------
/**
* Performs the conversion by delegating to the original conversion and
* then handling the default value.
*
* @param value the object to be converted
* @param converter the associated converter
* @return the result of the conversion
*/
public Object convert(Object value, Converter converter) {
Object result = null;
if (isDefaultOnException()) {
try {
result = getDecoratedConversion().convert(value, converter);
} catch (RuntimeException ex) {
return getDefaultValue();
}
} else {
result = getDecoratedConversion().convert(value, converter);
}
if (result == null) {
return getDefaultValue();
}
return result;
}
/**
* Returns the type to convert from. Delegates to the decorated conversion.
*
* @return the Class object representing the class to convert from
*/
public Class getFromType() {
return getDecoratedConversion().getFromType();
}
/**
* Returns the type to convert to. Delegates to the decorated conversion.
*
* @return the Class object representing the class to convert to
*/
public Class getToType() {
return getDecoratedConversion().getToType();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org