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.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import com.datatorrent.netlet.util.DTThrowable;
import java.util.*;
/**
* 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);
public class String2String implements StringCodec, Serializable
{
@Override
public String fromString(String string)
{
return string;
}
@Override
public String toString(String pojo)
{
return pojo;
}
private static final long serialVersionUID = 201310141156L;
}
public class Integer2String implements StringCodec, Serializable
{
@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;
}
public class Long2String implements StringCodec, Serializable
{
@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;
}
public class Boolean2String implements StringCodec, Serializable
{
@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(:)
*
* @param Type of the object which is converted to/from String
*/
public class Object2String implements StringCodec, Serializable
{
public final String separator;
public final String propertySeparator;
public Object2String()
{
separator = ":";
propertySeparator = "=";
}
public Object2String(String separator)
{
this.separator = separator;
this.propertySeparator = "=";
}
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) {
DTThrowable.rethrow(cause);
}
return null;
}
@Override
public String toString(T pojo)
{
String arg = pojo.toString();
if (arg == null) {
return pojo.getClass().getCanonicalName();
}
return pojo.getClass().getCanonicalName() + separator + arg;
}
private static final long serialVersionUID = 201311141853L;
}
public class Map2String implements StringCodec