Java SQL Type convertToProperty(Object value)

Here you can find the source of convertToProperty(Object value)

Description

convert To Property

License

Apache License

Declaration

@SuppressWarnings("rawtypes")
    public static Object convertToProperty(Object value) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) 2014-2015 IoT-Solutions e.U.
 * /*from w  w  w.  j av  a 2  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.
 ************************************************************************/

import java.sql.Time;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import java.util.Map;

public class Main {
    @SuppressWarnings("rawtypes")
    public static Object convertToProperty(Object value) {
        if (value != null) {
            if (Date.class.isAssignableFrom(value.getClass())) {
                return dateToLong((Date) value);
            } else if (Enum.class.isAssignableFrom(value.getClass())) {
                return ((Enum<?>) value).name();
            } else if (value instanceof Map) {
                Map map = (Map) value;
                if (map.isEmpty()) { // empty maps are mapped to empty lists
                    return Collections.EMPTY_LIST;
                }
            } else if (value instanceof Collection<?>) {
                List<Object> ret = new ArrayList<Object>();
                Iterator<?> it = ((Collection<?>) value).iterator();
                while (it.hasNext()) {
                    ret.add(convertToProperty(it.next()));
                }
                return ret;
            }
        }
        return value;
    }

    public static long dateToLong(Date date) {
        long millis = date.getTime();
        if (date instanceof java.sql.Date)
            return millis;
        else if (date instanceof Time)
            return millis;
        else if (date instanceof Timestamp)
            return millis;
        return (millis / 1000) * 1000;
    }
}

Related

  1. convertParam(Object oValue, Class cType, String sFieldName)
  2. convertSQLtype2JavaClassName(int type)
  3. convertStringToType(String type)
  4. convertTemporal(Object value, int srcType, int destType)
  5. convertToJDBCType(String talendType)
  6. convertToStream(final Object o)
  7. convertToString(int datatype)
  8. convertType(final int type)
  9. convertType(final int type, final String typeName)