![JAR search and dependency download from the Maven repository](/logo.png)
org.crazyyak.dev.common.BeanUtils Maven / Gradle / Ivy
/*
* Copyright 2012 Jacob D Parr
*
* 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.crazyyak.dev.common;
import java.lang.reflect.*;
import java.util.*;
import static java.lang.reflect.Modifier.*;
public class BeanUtils {
public static List getValuesFromCollection(Collection objects, String propertyName) {
try {
final Object[] args = new Object[0];
List retVal = new ArrayList();
for (Object object : objects) {
if (object == null) {
retVal.add(null);
} else {
Method method = ReflectUtils.getReadMethod(object.getClass(), propertyName);
if (method == null) {
throw new IllegalArgumentException("The accessor method for the property \""+propertyName+"\" does not exist.");
}
Object value = method.invoke(object, args);
if (value instanceof String || value instanceof Number) {
retVal.add(value.toString());
} else if (value instanceof Boolean) {
retVal.add((Boolean)value ? "1" : "0");
} else {
retVal.add(null);
}
}
}
return retVal;
} catch (Exception ex) {
throw new RuntimeException("Exception building list", ex);
}
}
static public StringBuffer getObjectAsString(Object object, boolean newline) {
Class type = object.getClass();
StringBuffer space = new StringBuffer(" ");
StringBuffer buffer = new StringBuffer();
buffer.append(type.getName());
buffer.append(":");
buffer.append(space);
HashMap map = new HashMap();
while (type != null) {
java.lang.reflect.Field fields[] = type.getDeclaredFields();
for (Field field : fields) {
try {
if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
continue;
}
if (map.get(field.getName()) == null) {
map.put(field.getName(), field.getName());
field.setAccessible(true);
Object value = field.get(object);
buffer.append("(");
if (value == null) {
buffer.append(field.getName());
buffer.append("=null");
} else if (field.getType().isArray()) {
buffer.append("[");
for (int arrayDex = 0; arrayDex < Array.getLength(value); arrayDex++) {
buffer.append(getObjectAsString(Array.get(value, arrayDex), false));
if (arrayDex < Array.getLength(value) - 1) {
buffer.append(",");
buffer.append(space);
}
}
buffer.append("]");
} else {
buffer.append(field.getName());
buffer.append("=");
buffer.append(value.toString());
}
buffer.append(")");
if (newline) {
buffer.append("\n");
} else if (field != fields[fields.length - 1]) {
buffer.append(",");
}
buffer.append(space);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
System.exit(0);
}
}
type = type.getSuperclass();
}
return buffer;
}
public static boolean objectsNotEqual(Object valueA, Object valueB) {
return objectsEqual(valueA, valueB) == false;
}
/** Helper method for testing equality of objects while handeling the issue of nulls */
public static boolean objectsEqual(T valueA, T valueB) {
if (valueA == valueB) {
return true; // Same instance or both null
} else if (valueA == null || valueB == null) {
return false; // One but not both are null
} else {
return valueA.equals(valueB);
}
}
static public boolean datesEqual(Date dateA, Date dateB) {
if (dateA == null && dateB == null) {
return true;
} else if (dateA == null || dateB == null) {
return false;
} else {
GregorianCalendar calA = new GregorianCalendar();
calA.setTime(dateA);
GregorianCalendar calB = new GregorianCalendar();
calB.setTime(dateB);
return (calA.get(Calendar.DATE) == calB.get(Calendar.DATE) &&
calA.get(Calendar.MONTH) == calB.get(Calendar.MONTH) &&
calA.get(Calendar.YEAR) == calB.get(Calendar.YEAR));
}
}
public static int compare(String objectA, String objectB) {
// noinspection StringEquality
if (objectA == objectB) return 0;
int result = compareForNull(objectA, objectB);
return (result != 0) ? result : objectA.compareTo(objectB);
}
private static int compareForNull(Object objectA, Object objectB) {
if (objectA == null && objectB != null) {
return 1;
} else if (objectA != null && objectB == null) {
return -1;
} else {
return 0;
}
}
public static T[] addToArray(T[] array, T... items) {
List list = new ArrayList(Arrays.asList(array));
Collections.addAll(list, items);
Class type = array.getClass().getComponentType();
// noinspection unchecked
return (T[]) ReflectUtils.toArray(type, list);
}
public static T[] removeFromArray(T[] array, T... items) {
List list = new ArrayList(Arrays.asList(array));
for (T item : items) {
list.remove(item);
}
Class type = array.getClass().getComponentType();
// noinspection unchecked
return (T[]) ReflectUtils.toArray(type, list);
}
public static T[] replaceInArray(T[] array, T... items) {
array = removeFromArray(array, items);
array = addToArray(array, items);
return array;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy