org.jboss.util.Objects Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jboss-common-core Show documentation
Show all versions of jboss-common-core Show documentation
JBoss Common Core Utility classes
The newest version!
/*
* JBoss, Home of Professional Open Source
* Copyright 2015, Red Hat, Inc., and individual contributors as indicated
* by the @authors tag.
*
* 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.jboss.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Array;
import java.lang.ref.Reference;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.Serializable;
import org.jboss.util.stream.Streams;
/**
* A collection of Object
utilities.
*
* @author Jason Dillon
* @author Ales Justin
*/
@SuppressWarnings("unchecked")
public final class Objects
{
/////////////////////////////////////////////////////////////////////////
// Coercion Methods //
/////////////////////////////////////////////////////////////////////////
/**
* Get a compatible constructor for the given value type
*
* @param type Class to look for constructor in
* @param valueType Argument type for constructor
* @return Constructor or null
*/
public static Constructor getCompatibleConstructor(final Class type,
final Class valueType)
{
// first try and find a constructor with the exact argument type
try {
return type.getConstructor(new Class[] { valueType });
}
catch (Exception ignore) {
// if the above failed, then try and find a constructor with
// an compatible argument type
// get an array of compatible types
Class[] types = type.getClasses();
for (int i=0; inon-null and is an
* instance of Reference
. If the object is null
* then null is returned. If the object is not an instance of
* Reference
, then the object is returned.
*
* @param obj Object to dereference.
* @return Dereferenced object.
*/
@SuppressWarnings("unchecked")
public static Object deref(final Object obj) {
if (obj != null && obj instanceof Reference) {
Reference ref = (Reference)obj;
return ref.get();
}
return obj;
}
/**
* Dereference an object
*
* @param the expected type
* @param obj the object or reference
* @param expected the expected type
* @return the object or null
*/
public static T deref(final Object obj, Class expected)
{
Object result = deref(obj);
if (result == null)
return null;
return expected.cast(result);
}
/**
* Check if the given object is an array (primitve or native).
*
* @param obj Object to test.
* @return True of the object is an array.
*/
public static boolean isArray(final Object obj) {
if (obj != null)
return obj.getClass().isArray();
return false;
}
/**
* @return an Object array for the given object.
*
* @param obj Object to convert to an array. Converts primitive
* arrays to Object arrays consisting of their wrapper
* classes. If the object is not an array (object or primitve)
* then a new array of the given type is created and the
* object is set as the sole element.
*/
public static Object[] toArray(final Object obj) {
// if the object is an array, the cast and return it.
if (obj instanceof Object[]) {
return (Object[])obj;
}
// if the object is an array of primitives then wrap the array
Class type = obj.getClass();
Object array;
if (type.isArray()) {
int length = Array.getLength(obj);
Class componentType = type.getComponentType();
array = Array.newInstance(componentType, length);
for (int i=0; i