commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2 ConversionException.java ConvertUtils.java ConverterSet.java ConverterRegistry.java
Date Sat, 14 Feb 2004 12:48:52 GMT
scolebourne    2004/02/14 04:48:52

  Added:       convert/src/java/org/apache/commons/convert2/conversion
                        ToBooleanConversion.java ToStringConversion.java
                        ConverterFactory.java ToIntConversion.java
                        Conversion.java
               convert/src/java/org/apache/commons/convert2
                        ConversionException.java ConvertUtils.java
                        ConverterSet.java ConverterRegistry.java
  Log:
  Initial checkin of alternate API
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ToBooleanConversion.java
  
  Index: ToBooleanConversion.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * Defines a Converter that will convert an object to an <code>boolean</code>.
   *
   * @author Stephen Colebourne
   * @version $Id: ToBooleanConversion.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public interface ToBooleanConversion extends Conversion {
  
      /**
       * Converts an input object to a primitive boolean.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public boolean convertToBoolean(Object value);
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ToStringConversion.java
  
  Index: ToStringConversion.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * Defines a Converter that will convert an object to a <code>String</code>.
   *
   * @author Stephen Colebourne
   * @version $Id: ToStringConversion.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public interface ToStringConversion extends Conversion {
  
      /**
       * Converts an input object to a String.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public String convertToString(Object value);
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ConverterFactory.java
  
  Index: ConverterFactory.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * Defines a factory for Converter objects that will create a converter on demand
   * or return a singleton.
   *
   * @author Stephen Colebourne
   * @version $Id: ConverterFactory.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public interface ConverterFactory {
  
      /**
       * Creates a Converter that can be used for the conversion.
       */
      public Conversion create();
  
      /**
       * The type to convert from.
       *
       * @return the Class object representing the class to convert to
       */
      public Class getFromType();
  
      /**
       * The type to convert to.
       *
       * @return the Class object representing the class to convert from
       */
      public Class getToType();
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/ToIntConversion.java
  
  Index: ToIntConversion.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * Defines a Converter that will convert an object to an <code>int</code>.
   *
   * @author Stephen Colebourne
   * @version $Id: ToIntConversion.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public interface ToIntConversion extends Conversion {
  
      /**
       * Converts an input object to a primitive int.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public int convertToInt(Object value);
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/conversion/Conversion.java
  
  Index: Conversion.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * Defines a Converter that will convert an object to an object of another type.
   * <p>
   * All configuration data for the conversion must be set on the converter
   * separately.
   *
   * @author Stephen Colebourne
   * @version $Id: Conversion.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public interface Conversion {
  
      /**
       * Convert the specified input object into an output object
       * of the another type.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public Object convert(Object value);
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConversionException.java
  
  Index: ConversionException.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * ConversionException is used when a conversion has failed for some reason.
   *
   * @author Craig McClanahan
   * @author Paulo Gaspar
   * @author Stephen Colebourne
   * @version $Id: ConversionException.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public class ConversionException extends IllegalArgumentException {
  
      /**
       * The root cause of this <code>ConversionException</code>, compatible with
       * JDK 1.4's extensions to <code>java.lang.Throwable</code>.
       */
      protected Throwable cause;
      
      /**
       * Construct a new exception with the specified message.
       *
       * @param message  a message describing this exception
       */
      public ConversionException(String message) {
          super(message);
      }
  
      /**
       * Construct a new exception with the specified root cause.
       *
       * @param cause  the root cause of this exception
       */
      public ConversionException(Throwable cause) {
          super(cause.getMessage());
          this.cause = cause;
      }
  
      /**
       * Construct a new exception with the specified message and root cause.
       *
       * @param message  a message describing this exception
       * @param cause  the root cause of this exception
       */
      public ConversionException(String message, Throwable cause) {
          super(message);
          this.cause = cause;
      }
  
      /**
       * Gets the root cause of the exception.
       * 
       * @return the root cause
       */
      public Throwable getCause() {
          return cause;
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConvertUtils.java
  
  Index: ConvertUtils.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * Simple public API for the conversion system consisting of static methods.
   * <p>
   * The methods on this class access the default range of converters. If you wish
   * to add to the default set of converters you cannot use this class. Instead,
   * you must create an instance of <code>ConverterSet</code> and add the converters
   * there.
   *
   * @author Stephen Colebourne
   * @version $Id: ConvertUtils.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public class ConvertUtils {
      
      /** The default ConverterSet, private to prevent subclasses altering the default */
      private static final ConverterSet DEFAULT = new ConverterSet();
  
      /**
       * Restricted constructor.
       * The main access to this class is via static methods, and it is not intended
       * to be subclassed.
       */
      protected ConvertUtils() {
          super();
      }
  
      //-----------------------------------------------------------------------
      /**
       * Convert the specified input object into an output object
       * of the another type.
       *
       * @param value  the input value to be converted
       * @param toClass  the class to convert to
       * @throws ConversionException (runtime) if conversion fails
       */
      public static Object convert(Object value, Class toClass) {
          return DEFAULT.convert(value, toClass);
      }
  
      /**
       * Convert the specified input object into a <code>String</code>.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public static String convertToString(Object value) {
          return DEFAULT.convertToString(value);
      }
  
      /**
       * Convert the specified input object into a <code>String</code>.
       *
       * @param value  the input value to be converted
       * @param fromClass  the class to convert from, useful if null passed in
       * @throws ConversionException (runtime) if conversion fails
       */
      public static String convertToString(Object value, Class fromClass) {
          return DEFAULT.convertToString(value, fromClass);
      }
  
      /**
       * Convert the specified input object into a primitive <code>int</code>.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public static int convertToInt(Object value) {
          return DEFAULT.convertToInt(value);
      }
  
      /**
       * Convert the specified input object into a primitive <code>int</code>.
       *
       * @param value  the input value to be converted
       * @param fromClass  the class to convert from, useful if null passed in
       * @throws ConversionException (runtime) if conversion fails
       */
      public static int convertToInt(Object value, Class fromClass) {
          return DEFAULT.convertToInt(value, fromClass);
      }
  
      /**
       * Convert the specified input object into a primitive <code>boolean</code>.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public static boolean convertToBoolean(Object value) {
          return DEFAULT.convertToBoolean(value);
      }
  
      /**
       * Convert the specified input object into a primitive <code>boolean</code>.
       *
       * @param value  the input value to be converted
       * @param fromClass  the class to convert from, useful if null passed in
       * @throws ConversionException (runtime) if conversion fails
       */
      public static boolean convertToBoolean(Object value, Class fromClass) {
          return DEFAULT.convertToBoolean(value, fromClass);
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConverterSet.java
  
  Index: ConverterSet.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * ConverterSet is the central class that holds a set of registered converters
   * together and allows conversion to occur.
   * <p>
   * This class allows applications to add their own converters. This is achieved
   * by creating a new instance and using that within the application. Generally,
   * the instance is stored in a static variable in the application.
   * <p>
   * In this way, the application remains independent of other converter sets used
   * by other applications or libraries.
   *
   * @author Stephen Colebourne
   * @version $Id: ConverterSet.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public class ConverterSet {
      
      /* The converter registry */
      protected ConverterRegistry registry;
      
      /**
       * Constructs a new instance of the ConverterSet useful to create a set
       * of conversions separate from the default set.
       */
      public ConverterSet() {
          super();
          registry = new ConverterRegistry();
      }
  
      //-----------------------------------------------------------------------
      /**
       * Convert the specified input object into an output object
       * of the another type.
       *
       * @param value  the input value to be converted
       * @param toClass  the class to convert to
       * @throws ConversionException (runtime) if conversion fails
       */
      public Object convert(Object value, Class toClass) {
          return null;
      }
  
      /**
       * Convert the specified input object into a <code>String</code>.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public String convertToString(Object value) {
          return (String) convert(value, String.class);
      }
  
      /**
       * Convert the specified input object into a <code>String</code>.
       *
       * @param value  the input value to be converted
       * @param fromClass  the class to convert from, useful if null passed in
       * @throws ConversionException (runtime) if conversion fails
       */
      public String convertToString(Object value, Class fromClass) {
          return null;
      }
  
      /**
       * Convert the specified input object into a primitive <code>int</code>.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public int convertToInt(Object value) {
          return 0;
      }
  
      /**
       * Convert the specified input object into a primitive <code>int</code>.
       *
       * @param value  the input value to be converted
       * @param fromClass  the class to convert from, useful if null passed in
       * @throws ConversionException (runtime) if conversion fails
       */
      public int convertToInt(Object value, Class fromClass) {
          return 0;
      }
  
      /**
       * Convert the specified input object into a primitive <code>boolean</code>.
       *
       * @param value  the input value to be converted
       * @throws ConversionException (runtime) if conversion fails
       */
      public boolean convertToBoolean(Object value) {
          return false;
      }
  
      /**
       * Convert the specified input object into a primitive <code>boolean</code>.
       *
       * @param value  the input value to be converted
       * @param fromClass  the class to convert from, useful if null passed in
       * @throws ConversionException (runtime) if conversion fails
       */
      public boolean convertToBoolean(Object value, Class fromClass) {
          return false;
      }
  
      //-----------------------------------------------------------------------
      /**
       * Gets the converter registry used for adding and removing converters.
       *
       * @return the converter registry, never null
       */
      public ConverterRegistry getRegistry() {
          return registry;
      }
  
      //-----------------------------------------------------------------------
      /**
       * Returns a string describing this object.
       *
       * @return a string describing this object
       */
      public String toString() {
          return "ConverterSet";
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/convert/src/java/org/apache/commons/convert2/ConverterRegistry.java
  
  Index: ConverterRegistry.java
  ===================================================================
  /*
   *  Copyright 2003-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;
  
  /**
   * ConverterRegistry allows the addition and removal of converters.
   *
   * @author Stephen Colebourne
   * @version $Id: ConverterRegistry.java,v 1.1 2004/02/14 12:48:52 scolebourne Exp $
   * @since 1.0
   */
  public class ConverterRegistry {
      
      /**
       * Restricted constructor, use ConverterSet.
       */
      protected ConverterRegistry() {
          super();
      }
  
      //-----------------------------------------------------------------------
      /**
       * Returns a string describing this object.
       *
       * @return a string describing this object
       */
      public String toString() {
          return "ConverterRegistry";
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message