Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.datatorrent.api;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectReader;
import org.codehaus.jackson.map.ObjectWriter;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import com.google.common.base.Throwables;
/**
* This interface is essentially serializer/deserializer interface which works with String as
* the serialized type. When initializing the attributes from the properties file, attribute
* values represented as Strings are needed to be converted to POJO. This class facilitates the
* conversion from and to String for attribute values.
*
* @param Type of the object which can be converted to/from String.
* @since 0.9.0
*/
public interface StringCodec
{
/**
* Given a string representation (typically from properties file) for an object , create object from it.
*
* @param string Type of the POJO which is created from String representation.
* @return POJO obtained as a result of deserialization
*/
T fromString(String string);
/**
* Given a POJO, serialize it to a String object (typically to be stored in properties file).
*
* @param pojo The object which needs to be serialized.
* @return Serialized representation of pojo..
*/
String toString(T pojo);
class Factory
{
public static StringCodec> getInstance(Class> cls)
{
if (cls == String.class) {
return String2String.getInstance();
} else if (cls == Integer.class) {
return Integer2String.getInstance();
} else if (cls == Long.class) {
return Long2String.getInstance();
} else if (cls == Boolean.class) {
return Boolean2String.getInstance();
} else if (Enum.class.isAssignableFrom(cls)) {
return Enum2String.getInstance(cls);
} else {
return null;
}
}
}
class String2String implements StringCodec, Serializable
{
@SuppressWarnings("deprecation")
private static final String2String instance = new String2String();
public static StringCodec getInstance()
{
return instance;
}
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance()}
*/
@Deprecated
public String2String()
{
}
@Override
public String fromString(String string)
{
return string;
}
@Override
public String toString(String pojo)
{
return pojo;
}
private static final long serialVersionUID = 201310141156L;
}
class Integer2String implements StringCodec, Serializable
{
@SuppressWarnings("deprecation")
private static final Integer2String instance = new Integer2String();
public static StringCodec getInstance()
{
return instance;
}
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance()}
*/
@Deprecated
public Integer2String()
{
}
@Override
public Integer fromString(String string)
{
return Integer.valueOf(string);
}
@Override
public String toString(Integer pojo)
{
return String.valueOf(pojo);
}
private static final long serialVersionUID = 201310141157L;
}
class Long2String implements StringCodec, Serializable
{
@SuppressWarnings("deprecation")
private static final Long2String instance = new Long2String();
public static StringCodec getInstance()
{
return instance;
}
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance()}
*/
@Deprecated
public Long2String()
{
}
@Override
public Long fromString(String string)
{
return Long.valueOf(string);
}
@Override
public String toString(Long pojo)
{
return String.valueOf(pojo);
}
private static final long serialVersionUID = 201310141158L;
}
class Boolean2String implements StringCodec, Serializable
{
@SuppressWarnings("deprecation")
private static final Boolean2String instance = new Boolean2String();
public static StringCodec getInstance()
{
return instance;
}
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance()}
*/
@Deprecated
public Boolean2String()
{
}
@Override
public Boolean fromString(String string)
{
return Boolean.valueOf(string);
}
@Override
public String toString(Boolean pojo)
{
return String.valueOf(pojo);
}
private static final long serialVersionUID = 201310141159L;
}
/**
* The attributes which represent arbitrary objects for which the schema cannot be
* standardized, we allow them to be represented as :: representation.
* This allows us to instantiate the class by invoking its constructor which takes
* as argument. If only the is specified, then just the class is instantiated using default
* constructor. If colon is specified then class is instantiated using constructor with
* string as an argument.If properties are specified then properties will be set on the object. The properties
* are defined in property=value format separated by colon(:)
*
* Note that the {@link #toString(Object) toString} method is by default NOT the proper reverse of the {@link
* #fromString(String) fromString} method. In order for the {@link #toString(Object) toString} method to become a
* proper reverse of the {@link #fromString(String) fromString} method, T's {@link T#toString() toString} method
* must output null or or the : format as stated above.
*
* @param Type of the object which is converted to/from String
*/
class Object2String implements StringCodec, Serializable
{
@SuppressWarnings("deprecation")
private static final Object2String instance = new Object2String();
public static StringCodec getInstance()
{
return instance;
}
public static StringCodec getInstance(String separator)
{
return getInstance(separator, "=");
}
@SuppressWarnings("deprecation")
public static StringCodec getInstance(String separator, String propertySeparator)
{
return new Object2String<>(separator, propertySeparator);
}
public final String separator;
public final String propertySeparator;
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance()}
*/
@SuppressWarnings("deprecation")
@Deprecated
public Object2String()
{
this(":", "=");
}
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance(String)}
*/
@SuppressWarnings("deprecation")
@Deprecated
public Object2String(String separator)
{
this(separator, "=");
}
/**
* @deprecated As of release 3.5.0, replaced by {@link #getInstance(String, String)}
*/
@Deprecated
public Object2String(String separator, String propertySeparator)
{
this.separator = separator;
this.propertySeparator = propertySeparator;
}
@Override
@SuppressWarnings({"UseSpecificCatch", "BroadCatchBlock", "TooBroadCatch"})
public T fromString(String string)
{
String[] parts = string.split(separator);
try {
@SuppressWarnings("unchecked")
Class extends T> clazz = (Class extends T>)Thread.currentThread().getContextClassLoader().loadClass(parts[0]);
if (parts.length == 1) {
return clazz.newInstance();
}
//String[] properties = parts[1].split(separator, 2);
if (parts.length == 2) {
return clazz.getConstructor(String.class).newInstance(parts[1]);
} else {
T object = clazz.getConstructor(String.class).newInstance(parts[1]);
HashMap hashMap = new HashMap<>();
for (int i = 2; i < parts.length; i++) {
String[] keyValPair = parts[i].split(propertySeparator, 2);
hashMap.put(keyValPair[0], keyValPair[1]);
}
BeanUtils.populate(object, hashMap);
return object;
}
} catch (Throwable cause) {
throw Throwables.propagate(cause);
}
}
@Override
public String toString(T pojo)
{
if (pojo == null) {
return null;
}
String arg = pojo.toString();
if (arg == null) {
return pojo.getClass().getCanonicalName();
}
return pojo.getClass().getCanonicalName() + separator + arg;
}
private static final long serialVersionUID = 201311141853L;
}
class Map2String implements StringCodec