Android Open Source - jepldroid J E P L Util Impl






From Project

Back to project page jepldroid.

License

The source code is released under:

Apache License

If you think the Android project jepldroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
   Copyright 2011 Jose Maria Arranz Santamaria
//from   w  w w  .j  a  va2 s  .  co m
   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 jepl.impl;

import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import jepl.JEPLException;

/**
 *
 * @author jmarranz
 */
public class JEPLUtilImpl
{

    public static <T> T castDynamic(Object obj,Class<T> returnType)
    {
        try
        {
            Constructor<T> constr = returnType.getConstructor(obj.getClass());
            return constr.newInstance(obj);
        }
        catch (Exception ex)
        {
            throw new JEPLException("Cannot cast " + obj.getClass() + " to " + returnType,ex);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T cast(Object obj,Class<T> returnType)
    {
        // Nuestros casts sern sencillos no como en:
        // http://www.docjar.com/html/api/com/mysql/jdbc/ResultSetImpl.java.html
        // No tiene sentido por ejemplo convertir una cadena conteniendo un valor real
        // a double y luego convertir a integer por ejemplo, es absurdo guardar
        // un valor real como cadena y luego pedirlo como entero via APIs de persistencia
        // lo lgico es hacer esa conversin fuera de la persistencia.

        if (obj == null) return null;

        if (returnType.isAssignableFrom(obj.getClass()))
            return (T)obj;

        if (returnType.equals(String.class))
            return (T)obj.toString();

        if (obj instanceof Number)
        {
            if (returnType.equals(Byte.class) || returnType.equals(byte.class))
                return (T)(Byte)((Number)obj).byteValue();
            else if (returnType.equals(Short.class) || returnType.equals(short.class))
                return (T)(Short)((Number)obj).shortValue();
            else if (returnType.equals(Integer.class) || returnType.equals(int.class))
                return (T)(Integer)((Number)obj).intValue();
            else if (returnType.equals(Long.class) || returnType.equals(long.class))
                return (T)(Long)((Number)obj).longValue();
            else if (returnType.equals(Float.class) || returnType.equals(float.class))
                return (T)(Float)((Number)obj).floatValue();
            else if (returnType.equals(Double.class) || returnType.equals(double.class))
                return (T)(Double)((Number)obj).doubleValue();
            else if (returnType.equals(BigInteger.class))
            {
                long value = cast(obj,long.class);
                return (T)new BigInteger(Long.toString(value));
            }
            else if (returnType.equals(BigDecimal.class))
            {
                double value = cast(obj,double.class);
                return (T)new BigDecimal(value);
            }
            else if (returnType.equals(java.sql.Date.class))
            {
                long value = cast(obj,long.class);
                return (T)new java.sql.Date(value);
            }
            else if (returnType.equals(java.sql.Time.class))
            {
                long value = cast(obj,long.class);
                return (T)new java.sql.Time(value);
            }
            else if (returnType.equals(java.sql.Timestamp.class))
            {
                long value = cast(obj,long.class);
                return (T)new java.sql.Time(value);
            }

            return castDynamic(obj,returnType);
        }
        else if (obj instanceof String)
        {
            if (returnType.equals(Boolean.class) || returnType.equals(Boolean.class))
                return (T)Boolean.valueOf((String)obj);
            if (returnType.equals(Byte.class) || returnType.equals(byte.class))
                return (T)new Byte((String)obj);
            else if (returnType.equals(Character.class) || returnType.equals(char.class))
                return (T)Character.valueOf( ((String)obj).charAt(0) );
            else if (returnType.equals(Short.class) || returnType.equals(short.class))
                return (T)Short.valueOf((String)obj);
            else if (returnType.equals(Integer.class) || returnType.equals(int.class))
                return (T)Integer.valueOf((String)obj);
            else if (returnType.equals(Long.class) || returnType.equals(long.class))
                return (T)Long.valueOf((String)obj);
            else if (returnType.equals(Float.class) || returnType.equals(float.class))
                return (T)Float.valueOf((String)obj);
            else if (returnType.equals(Double.class) || returnType.equals(double.class))
                return (T)Double.valueOf((String)obj);
            else if (returnType.equals(BigInteger.class))
                return (T)new BigInteger((String)obj);
            else if (returnType.equals(BigDecimal.class))
                return (T)new BigDecimal((String)obj);
            else if (returnType.equals(java.sql.Date.class))
                return (T)java.sql.Date.valueOf((String)obj);
            else if (returnType.equals(java.sql.Time.class))
                return (T)java.sql.Time.valueOf((String)obj);
            else if (returnType.equals(java.sql.Timestamp.class))
                return (T)java.sql.Timestamp.valueOf((String)obj);


            return castDynamic(obj,returnType);
        }
        else
        {
            return castDynamic(obj,returnType);
        }
    }

    public static Object getResultSetColumnObject(ResultSet rs,int column,Class<?> targetType) throws SQLException
    {
        // Llamamos al mtodo ms apropiado de acuerdo con el tipo de datos esperado
        if (targetType.isPrimitive())
        {
            if (targetType.equals(boolean.class))
                return rs.getBoolean(column);
            else if (targetType.equals(byte.class))
                return rs.getByte(column);
            else if (targetType.equals(char.class))
                return rs.getString(column);
            else if (targetType.equals(short.class))
                return rs.getShort(column);
            else if (targetType.equals(int.class))
                return rs.getInt(column);
            else if (targetType.equals(long.class))
                return rs.getLong(column);
            else if (targetType.equals(float.class))
                return rs.getFloat(column);
            else if (targetType.equals(double.class))
                return rs.getDouble(column);
            else
                return rs.getObject(column); // NO hay ms pero por si acaso se aaden ms en un futuro Java
        }
        else
        {
            Object obj = rs.getObject(column);
            if (obj == null) return null;

            if (Number.class.isAssignableFrom(targetType))
            {
                if (targetType.equals(Byte.class))
                    return rs.getByte(column);
                else if (targetType.equals(Short.class))
                    return rs.getShort(column);
                else if (targetType.equals(Integer.class))
                    return rs.getInt(column);
                else if (targetType.equals(Long.class))
                    return rs.getLong(column);
                else if (targetType.equals(Float.class))
                    return rs.getFloat(column);
                else if (targetType.equals(Double.class))
                    return rs.getDouble(column);
                else if (targetType.equals(BigDecimal.class))
                    return rs.getBigDecimal(column);
                else
                    return obj; // Puede ser BigInteger
            }
            else if (targetType.equals(Boolean.class))
                return rs.getBoolean(column);
            else if (targetType.equals(Character.class))
                return rs.getString(column);
            else if (targetType.equals(String.class))
                return rs.getString(column);
            else if (targetType.equals(java.sql.Date.class))
                return rs.getDate(column);
            else if (targetType.equals(java.sql.Time.class))
                return rs.getTime(column);
            else if (targetType.equals(java.sql.Timestamp.class))
                return rs.getTimestamp(column);
            else
                return obj;
        }
    }

}




Java Source Code List

com.innowhere.jepldroidtest.JEPLDroidTestActivity.java
jepl.JEPLBootNonJTA.java
jepl.JEPLBootRoot.java
jepl.JEPLBoot.java
jepl.JEPLCachedResultSet.java
jepl.JEPLConnectionListener.java
jepl.JEPLConnection.java
jepl.JEPLDALQuery.java
jepl.JEPLDAL.java
jepl.JEPLDAOQuery.java
jepl.JEPLDAO.java
jepl.JEPLDataSource.java
jepl.JEPLException.java
jepl.JEPLListener.java
jepl.JEPLNonJTADataSource.java
jepl.JEPLParameter.java
jepl.JEPLPreparedStatementListener.java
jepl.JEPLPreparedStatement.java
jepl.JEPLResultSetDALListener.java
jepl.JEPLResultSetDAOListenerDefault.java
jepl.JEPLResultSetDAOListener.java
jepl.JEPLResultSetDAO.java
jepl.JEPLResultSet.java
jepl.JEPLRowBeanMapper.java
jepl.JEPLStatement.java
jepl.JEPLTask.java
jepl.JEPLTransactionPropagation.java
jepl.JEPLTransaction.java
jepl.JEPLTransactionalJTA.java
jepl.JEPLTransactionalNonJTA.java
jepl.JEPLUserData.java
jepl.impl.JEPLBootImpl.java
jepl.impl.JEPLBootRootImpl.java
jepl.impl.JEPLConnectionImpl.java
jepl.impl.JEPLCurrentTransactionImpl.java
jepl.impl.JEPLDALDefaultImpl.java
jepl.impl.JEPLDALImpl.java
jepl.impl.JEPLDAOImpl.java
jepl.impl.JEPLDataSourceImpl.java
jepl.impl.JEPLListenerListImpl.java
jepl.impl.JEPLPreparedStatementDefaultImpl.java
jepl.impl.JEPLPreparedStatementImpl.java
jepl.impl.JEPLStatementImpl.java
jepl.impl.JEPLTaskExecContextImpl.java
jepl.impl.JEPLTaskExecContextInConnectionImpl.java
jepl.impl.JEPLTaskOneExecWithConnectionImpl.java
jepl.impl.JEPLTaskOneExecWithConnectionWrapperImpl.java
jepl.impl.JEPLTaskOneExecutionImpl.java
jepl.impl.JEPLTaskOneExecutionWrapperImpl.java
jepl.impl.JEPLUserDataMonoThreadImpl.java
jepl.impl.JEPLUserDataMultiThreadImpl.java
jepl.impl.JEPLUtilImpl.java
jepl.impl.lex.Cursor.java
jepl.impl.lex.Identifier.java
jepl.impl.lex.JDBCParamStandardToken.java
jepl.impl.lex.JDBCParamToken.java
jepl.impl.lex.JDBCParamWithNameToken.java
jepl.impl.lex.JDBCParamWithNumberToken.java
jepl.impl.lex.OtherCharToken.java
jepl.impl.lex.SourceCode.java
jepl.impl.lex.Space.java
jepl.impl.lex.StringDoubleQuote.java
jepl.impl.lex.StringLiteral.java
jepl.impl.lex.StringSimpleQuote.java
jepl.impl.lex.TestSourceCode.java
jepl.impl.lex.TokenFilter.java
jepl.impl.lex.Token.java
jepl.impl.nonjta.JEPLBootNonJTAImpl.java
jepl.impl.nonjta.JEPLCurrentTransactionNonJTAImpl.java
jepl.impl.nonjta.JEPLNonJTAConnectionDefaultImpl.java
jepl.impl.nonjta.JEPLNonJTAConnectionImpl.java
jepl.impl.nonjta.JEPLNonJTADataSourceDefaultImpl.java
jepl.impl.nonjta.JEPLNonJTADataSourceImpl.java
jepl.impl.nonjta.JEPLTaskExecContextInConnectionNonJTAImpl.java
jepl.impl.nonjta.android.JEPLNonJTAConnectionSQLDroidImpl.java
jepl.impl.nonjta.android.JEPLNonJTADataSourceAndroidImpl.java
jepl.impl.nonjta.android.JEPLPreparedStatementSQLDroidImpl.java
jepl.impl.query.JEPLCachedResultSetImpl.java
jepl.impl.query.JEPLDALQueryImpl.java
jepl.impl.query.JEPLDAOQueryImpl.java
jepl.impl.query.JEPLParameterDecImpl.java
jepl.impl.query.JEPLParameterDecWithNameImpl.java
jepl.impl.query.JEPLParameterDecWithNumberImpl.java
jepl.impl.query.JEPLParameterImpl.java
jepl.impl.query.JEPLParameterValueImpl.java
jepl.impl.query.JEPLParameterValueNamedImpl.java
jepl.impl.query.JEPLParameterWithNameImpl.java
jepl.impl.query.JEPLParameterWithNumberImpl.java
jepl.impl.query.JEPLPropertyDescriptorImpl.java
jepl.impl.query.JEPLPropertyDescriptorJavaBeansImpl.java
jepl.impl.query.JEPLQueryParsedCacheImpl.java
jepl.impl.query.JEPLQueryParsedImpl.java
jepl.impl.query.JEPLResultSetBeanInfo.java
jepl.impl.query.JEPLResultSetDAOImpl.java
jepl.impl.query.JEPLResultSetDAOIteratorImpl.java
jepl.impl.query.JEPLResultSetDAOListenerDefaultImpl.java
jepl.impl.query.JEPLResultSetDefaultImpl.java
jepl.impl.query.JEPLResultSetImpl.java