org.red5.io.utils.ConversionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ant-media-server Show documentation
Show all versions of ant-media-server Show documentation
Ant Media Server supports RTMP, RTSP, MP4, HLS, WebRTC, Adaptive Streaming, etc.
/*
* RED5 Open Source Media Server - https://github.com/Red5/ Copyright 2006-2016 by respective authors (see below). All rights reserved. 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.red5.io.utils;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Misc utils for conversions
*
* @author The Red5 Project
* @author Luke Hubbard, Codegent Ltd ([email protected])
* @author Paul Gregoire ([email protected])
*/
public class ConversionUtils {
private static final Logger log = LoggerFactory.getLogger(ConversionUtils.class);
private static final Class>[] PRIMITIVES = { boolean.class, byte.class, char.class, short.class, int.class, long.class, float.class, double.class };
private static final Class>[] WRAPPERS = { Boolean.class, Byte.class, Character.class, Short.class, Integer.class, Long.class, Float.class, Double.class };
private static final String NUMERIC_TYPE = "[-]?\\b\\d+\\b|[-]?\\b[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?\\b";
/**
* Parameter chains
*/
private static final Class>[][] PARAMETER_CHAINS = { { boolean.class, null }, { byte.class, Short.class }, { char.class, Integer.class }, { short.class, Integer.class }, { int.class, Long.class }, { long.class, Float.class }, { float.class, Double.class }, { double.class, null } };
/** Mapping of primitives to wrappers */
private static Map, Class>> primitiveMap = new HashMap, Class>>();
/** Mapping of wrappers to primitives */
private static Map, Class>> wrapperMap = new HashMap, Class>>();
/**
* Mapping from wrapper class to appropriate parameter types (in order) Each entry is an array of Classes, the last of which is either null (for no chaining) or the next class to try
*/
private static Map, Class>[]> parameterMap = new HashMap, Class>[]>();
/**
* Method names to skip when scanning for usable application methods.
*/
private static String ignoredMethodNames;
static {
for (int i = 0; i < PRIMITIVES.length; i++) {
primitiveMap.put(PRIMITIVES[i], WRAPPERS[i]);
wrapperMap.put(WRAPPERS[i], PRIMITIVES[i]);
parameterMap.put(WRAPPERS[i], PARAMETER_CHAINS[i]);
}
ignoredMethodNames = new String("equals,hashCode,toString,wait,notifyAll,getClass,clone,compareTo");
}
/**
* Returns true for base types or arrays of base type.
*
* @param obj Object
* @return true if base-type or array and false otherwise
*/
public static boolean isBaseTypeOrArray(Object obj) {
final Class> c = obj.getClass();
if (c.isPrimitive() || c.equals(String.class) || c.isArray()) {
return true;
} else if (wrapperMap.containsKey(c)) {
return true;
}
return false;
}
/**
* Convert source to given class
*
* @param source
* Source object
* @param target
* Target class
* @return Converted object
* @throws ConversionException
* If object can't be converted
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Object convert(Object source, Class> target) throws ConversionException {
if (target == null) {
throw new ConversionException("Unable to perform conversion, target was null");
}
if (source == null) {
if (target.isPrimitive()) {
throw new ConversionException(String.format("Unable to convert null to primitive value of %s", target));
}
return source;
} else if ((source instanceof Float && ((Float) source).isNaN()) || (source instanceof Double && ((Double) source).isNaN())) {
// Don't convert NaN values
return source;
}
if (target.isInstance(source)) {
return source;
}
if (target.isAssignableFrom(source.getClass())) {
return source;
}
if (target.isArray()) {
return convertToArray(source, target);
}
if (target.equals(String.class)) {
return source.toString();
}
if (target.isPrimitive()) {
return convertToWrappedPrimitive(source, primitiveMap.get(target));
}
if (wrapperMap.containsKey(target)) {
return convertToWrappedPrimitive(source, target);
}
if (target.equals(Map.class)) {
return convertBeanToMap(source);
}
if (target.equals(List.class) || target.equals(Collection.class)) {
if (source.getClass().equals(LinkedHashMap.class)) {
return convertMapToList((LinkedHashMap, ?>) source);
} else if (source.getClass().isArray()) {
return convertArrayToList((Object[]) source);
}
}
if (target.equals(Set.class) && source.getClass().isArray()) {
return convertArrayToSet((Object[]) source);
}
if (target.equals(Set.class) && source instanceof List) {
return new HashSet((List) source);
}
if (source instanceof Map) {
return convertMapToBean((Map) source, target);
}
throw new ConversionException(String.format("Unable to preform conversion from %s to %s", source, target));
}
/**
* Convert to array
*
* @param source
* Source object
* @param target
* Target class
* @return Converted object
* @throws ConversionException
* If object can't be converted
*/
public static Object convertToArray(Object source, Class> target) throws ConversionException {
try {
Class> targetType = target.getComponentType();
if (source.getClass().isArray()) {
Object targetInstance = Array.newInstance(targetType, Array.getLength(source));
for (int i = 0; i < Array.getLength(source); i++) {
Array.set(targetInstance, i, convert(Array.get(source, i), targetType));
}
return targetInstance;
}
if (source instanceof Collection>) {
Collection> sourceCollection = (Collection>) source;
Object targetInstance = Array.newInstance(target.getComponentType(), sourceCollection.size());
Iterator> it = sourceCollection.iterator();
int i = 0;
while (it.hasNext()) {
Array.set(targetInstance, i++, convert(it.next(), targetType));
}
return targetInstance;
}
throw new ConversionException("Unable to convert to array");
} catch (Exception ex) {
throw new ConversionException("Error converting to array", ex);
}
}
public static List