All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.brunocvcunha.digesteroids.cast.DigesteroidsDefaultCaster Maven / Gradle / Ivy

There is a newer version: 0.3
Show newest version
/**
 * Copyright (C) 2015 Bruno Candido Volpato da Cunha ([email protected])
 *
 * 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.brunocvcunha.digesteroids.cast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * Reflection Difference Handling
 * 
 * @author Bruno Candido Volpato da Cunha
 */
public class DigesteroidsDefaultCaster implements DigesteroidsCaster {

  private static Logger log = Logger.getLogger(DigesteroidsDefaultCaster.class);

  /**
   * Constant for Map of String to Object
   */
  public static final Type _obj_map_token = new TypeToken>() {
    //no impl
  }.getType();
  
  /**
   * Constant for List of Map from String to Object
   */
  public static final Type _obj_map_list_token = new TypeToken>>() {
    //no impl
  }.getType();

  /**
   * The default format for date and time
   */
  public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
  
  /**
   * The default format for date
   */
  public static final String DEFAULT_DATE_ONLY_FORMAT = "yyyy-MM-dd";
  
  /**
   * The default date/time formatter
   */
  public static final DateTimeFormatter DATE_FORMAT_PARSER = DateTimeFormat.forPattern(DEFAULT_DATE_FORMAT).withZoneUTC();

  
  private static Gson gson = new GsonBuilder().create();

  @Override
  public String json(Object value) {
    if (value == null) {
      return null;
    }
    
    return gson.toJson(value);
  }

  @Override
  public String string(Object value) {
    if (value == null) {
      return null;
    }
    
    if (value instanceof String) {
      return (String) value;
    }

    return String.valueOf(value);
  }

  @Override
  public Map map(Object value) {
    if (value instanceof Map) {
      return (Map) value;
    }
    
    String json;
    if (value instanceof String) {
      json = (String) value;
    } else {
      json = gson.toJson(value);
    }

    return gson.fromJson(json, _obj_map_token);
  }

  @Override
  public List> mapList(Object value) {
    String json;
    if (value instanceof String) {
      json = (String) value;
    } else {
      json = gson.toJson(value);
    }

    return gson.fromJson(json, _obj_map_list_token);
  }

  
  @Override
  public Date date(Object value) {
    if (value instanceof GregorianCalendar) {
      return ((GregorianCalendar) value).getTime();
    }
    
    return DATE_FORMAT_PARSER.parseDateTime(String.valueOf(value)).toDate();
  }
  
  @Override
  public  T cast(Object value, Type targetType) {
    return cast(value, targetType, null);
  }
  
  @SuppressWarnings("unchecked")
  @Override
  public  T cast(Object value, Type targetType, String source) {

    if (value == null) {
      return null;
    }
    
    TypeToken typeToken = (TypeToken) TypeToken.get(targetType);
    Class toClazz = (Class) typeToken.getRawType();

    log.debug("Handling value conversion (" + value.getClass() + " / " + value.toString() + ") to "
        + toClazz);

    if (toClazz.isAssignableFrom(String.class)) {
      return (T) string(value);
    }

    if (toClazz.isAssignableFrom(Byte.class)) {
      return (T) Byte.valueOf(value.toString());
    }

    if (toClazz.isAssignableFrom(Short.class)) {
      return (T) Short.valueOf(value.toString());
    }

    if (toClazz.isAssignableFrom(Integer.class)) {
      return (T) Integer.valueOf(Double.valueOf(value.toString()).intValue());
    }

    if (toClazz.isAssignableFrom(Long.class)) {
      return (T) Long.valueOf(Double.valueOf(value.toString()).longValue());
    }

    if (toClazz.isAssignableFrom(Double.class)) {
      return (T) Double.valueOf(value.toString());
    }

    if (toClazz.isAssignableFrom(Date.class)) {
      return (T) date(value);
    }

    
//    throw new IllegalArgumentException("Need to convert value (" + value.getClass() + " / " + value.toString() + ") to " + toClazz);
    return gson.fromJson(gson.toJson(value), targetType);

  }

  @Override
  public Element htmlElement(Object value) {
    if (value instanceof Element) {
      return (Element) value;
    }
    
    Document document = Jsoup.parse(string(value));
    return document;
  }
  
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy