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

org.apache.juneau.internal.ObjectUtils Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * 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 java.lang.reflect.*;
import java.util.*;

import org.apache.juneau.*;
import org.apache.juneau.transform.*;

/**
 * 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 typeValid input value typesNotes
* A class that is the normal type of a registered {@link PojoSwap}. * * A value whose class matches the transformed type of that registered {@link PojoSwap}. *  
* A class that is the transformed type of a registered {@link PojoSwap}. * * A value whose class matches the normal type of that registered {@link PojoSwap}. *  
* {@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 ObjectMap}) * * {@code Map} * * If {@code Map} is not constructible, a {@code ObjectMap} is created. *
* {@code Collection} (e.g. {@code List}, {@code LinkedList}, {@code HashSet}, {@code ObjectList}) * * {@code Collection} *
{@code Object[]} *
* If {@code Collection} is not constructible, a {@code ObjectList} is created. *
* {@code X[]} (array of any type X) * * {@code List} *  
* {@code X[][]} (multi-dimensional arrays) * * {@code List>} *
{@code List} *
{@code List[]} *
 
* {@code Enum} * * {@code String} *  
* Bean * * {@code Map} *  
* {@code 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 * *
*
*/ public class ObjectUtils { // 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.createSession(); /** * 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 convertToType(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 outer * If class is a member class, this is the instance of the containing class. * Should be null if not a member class. * @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 convertToMemberType(Object outer, Object value, Class type) { return session.convertToMemberType(outer, value, type); } /** * Returns true if the specified objects are equal. * *

* Gracefully handles nulls. * * @param o1 Object #1 * @param o2 Object #2 * @return true if the objects are equal or both null. */ public static boolean equals(Object o1, Object o2) { if (o1 == null && o2 == null) return true; if (o1 == null || o2 == null) return false; return o1.equals(o2); } /** * Returns true if the specified object is empty. * *

* Return true if the value is any of the following: *

    *
  • null *
  • An empty Collection *
  • An empty Map *
  • An empty array *
  • An empty CharSequence *
  • An empty String when serialized to a string using {@link Object#toString()}. *
* * @param o The object to test. * @return true if the specified object is empty. */ @SuppressWarnings("rawtypes") public static boolean isEmpty(Object o) { if (o == null) return true; if (o instanceof Collection) return ((Collection)o).isEmpty(); if (o instanceof Map) return ((Map)o).isEmpty(); if (o.getClass().isArray()) return (Array.getLength(o) == 0); return o.toString().isEmpty(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy