Package DisplayProject.binding

Source Code of DisplayProject.binding.TypeConverter

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.binding;

import org.apache.log4j.Logger;

import Framework.BooleanData;
import Framework.TextData;

public class TypeConverter {
    private static Logger logger = Logger.getLogger(TypeConverter.class);

    /**
     * Convert the passed value into a new object of the passed type and return this value
     * @param value
     * @param valueType
     * @return
     */
    public static Object convert(Object value, Class<?> valueType) {
        if (value != null && !valueType.isAssignableFrom(value.getClass()) )
        {
            // If the value is a Number then try and convert it
            if (value instanceof Number)
            {
                if (valueType == Integer.TYPE || valueType == Integer.class)
                {
                    logger.debug("Converting number to integer");
                    return new Integer(((Number) value).intValue());
                }                               
                else if (valueType == Long.TYPE || valueType == Long.class)
                {
                    logger.debug("Converting number to long");
                    return new Long(((Number) value).longValue());
                }
                else if (valueType == Float.TYPE || valueType == Float.class)
                {
                    logger.debug("Converting number to float");
                    return new Float(((Number) value).floatValue());
                }
                else if (valueType == Double.TYPE || valueType == Double.class)
                {
                    logger.debug("Converting number to double");
                    return new Double(((Number) value).doubleValue());
                }
                else if (valueType == Byte.TYPE || valueType == Byte.class)
                {
                    logger.debug("Converting number to byte");
                    return new Byte(((Number) value).byteValue());
                }
                else if (valueType == Short.TYPE || valueType == Short.class)
                {
                    logger.debug("Converting number to short");
                    return new Short(((Number) value).shortValue());
                }
            }
            else if (valueType.equals(String.class)) {
              return value.toString();
            }
            else if (TextData.class.isAssignableFrom(valueType)) {
              TextData result;
              try {
                result = (TextData)valueType.newInstance();
                result.setValue(value.toString());
              }
              catch (InstantiationException e) {
                result = new TextData(value.toString());
              }
              catch (IllegalAccessException e) {
                result = new TextData(value.toString());
              }
              return result;
            }
           
            // CraigM:07/10/2008 - Handle conversion from something to an int (won't be an number to an int, as that is handled in the first if condition).  JIRA: JCT-605.
            else if (valueType == Integer.TYPE) {
              try {
                return Integer.parseInt(value.toString());
              }
              catch (NumberFormatException e) {
                return 0; // Just return 0 if it is not possible to convert (Forte would do this)
              }
            }

            // CraigM:02/12/2008 - Handle BooleanData and sub classes of BooleanData
            else if (BooleanData.class.isAssignableFrom(valueType)) {
              if (value instanceof Boolean) {
                BooleanData result;
          try {
            result = (BooleanData)valueType.newInstance();
                  result.setBooleanValue((Boolean)value);
          } catch (InstantiationException e) {
            result = new BooleanData((Boolean)value);
          } catch (IllegalAccessException e) {
            result = new BooleanData((Boolean)value);
          }
          return result;
              }
            }
        }
        return value;

    }
}
TOP

Related Classes of DisplayProject.binding.TypeConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.