![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.internal.ConverterUtils Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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 org.apache.juneau.internal;
import static org.apache.juneau.common.internal.ThrowableUtils.*;
import java.lang.reflect.*;
import org.apache.juneau.*;
import org.apache.juneau.common.internal.*;
import org.apache.juneau.parser.*;
import org.apache.juneau.swap.*;
/**
* Utility class for efficiently converting objects between types.
*
*
* If the value isn't an instance of the specified type, then converts the value if possible.
*
*
* The following conversions are valid:
*
* Convert to type Valid input value types Notes
*
*
* A class that is the normal type of a registered {@link ObjectSwap}.
*
*
* A value whose class matches the transformed type of that registered {@link ObjectSwap}.
*
*
*
*
*
* A class that is the transformed type of a registered {@link ObjectSwap}.
*
*
* A value whose class matches the normal type of that registered {@link ObjectSwap}.
*
*
*
*
*
* {@code Number} (e.g. {@code Integer}, {@code Short}, {@code Float},...)
*
Number.TYPE
(e.g. Integer.TYPE
,
* Short.TYPE
, Float.TYPE
,...)
*
*
* {@code Number}, {@code String}, null
*
*
* For primitive {@code TYPES}, null returns the JVM default value for that type.
*
*
*
*
* {@code Map} (e.g. {@code Map}, {@code HashMap}, {@code TreeMap}, {@code JsonMap})
*
*
* {@code Map}
*
*
* If {@code Map} is not constructible, an {@code JsonMap} is created.
*
*
*
*
* Collection (e.g. List , LinkedList , HashSet , JsonList )
*
*
* Collection<Object>
*
Object[]
*
*
* If Collection is not constructible, a JsonList is created.
*
*
*
*
* X[] (array of any type X)
*
*
* List<X>
*
*
*
*
*
* X[][] (multi-dimensional arrays)
*
*
* List<List<X>>
*
List<X[]>
*
List[]<X>
*
*
*
*
*
* Enum
*
*
* String
*
*
*
*
*
* Bean
*
*
* Map
*
*
*
*
*
* String
*
*
* Anything
*
*
* Arrays are converted to JSON arrays
*
*
*
*
* Anything with one of the following methods:
*
public static T fromString(String)
*
public static T valueOf(String)
*
public T(String)
*
*
* String
*
*
*
*
*
*
*
* See Also:
*
*/
public final class ConverterUtils {
// Session objects are usually not thread safe, but we're not using any feature
// of bean sessions that would cause thread safety issues.
private static final BeanSession session = BeanContext.DEFAULT_SESSION;
/**
* Converts the specified object to the specified type.
*
* @param The class type to convert the value to.
* @param value The value to convert.
* @param type The class type to convert the value to.
* @throws InvalidDataConversionException If the specified value cannot be converted to the specified type.
* @return The converted value.
*/
public static T toType(Object value, Class type) {
return session.convertToType(value, type);
}
/**
* Converts the specified object to the specified type.
*
* @param The class type to convert the value to.
* @param value The value to convert.
* @param type The class type to convert the value to.
* @param args The type arguments.
* @throws InvalidDataConversionException If the specified value cannot be converted to the specified type.
* @return The converted value.
*/
public static T toType(Object value, Class type, Type...args) {
return session.convertToType(value, type, args);
}
/**
* Converts an object to a Boolean.
*
* @param o The object to convert.
* @return The converted object.
*/
public static Boolean toBoolean(Object o) {
return toType(o, Boolean.class);
}
/**
* Converts an object to an Integer.
*
* @param o The object to convert.
* @return The converted object.
*/
public static Integer toInteger(Object o) {
return toType(o, Integer.class);
}
/**
* Converts an object to a Number.
*
* @param o The object to convert.
* @return The converted object.
*/
public static Number toNumber(Object o) {
if (o == null)
return null;
if (o instanceof Number)
return (Number)o;
try {
return StringUtils.parseNumber(o.toString(), null);
} catch (ParseException e) {
throw asRuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy