com.codepoetics.fluvius.mutation.MutableState Maven / Gradle / Ivy
package com.codepoetics.fluvius.mutation;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* Records the mutable state of an object, so that it can be compared before and after an operation.
*/
public final class MutableState {
private MutableState() {
}
/**
* Obtain an equality-testable representation of the mutable state of the supplied object.
* @param object The object to extract the mutable state from.
* @return An equality-testable representation of the mutable state of the supplied object.
*/
public static Object of(Object object) {
if (isScalar(object)) {
return object;
}
if (object instanceof Collection) {
return ofCollection((Collection>) object);
}
if (object instanceof Map) {
return ofMap((Map, ?>) object);
}
if (object.getClass().isArray()) {
return ofArray((Object[]) object);
}
if (definesEquals(object)) {
return object;
}
if (hasGettableProperties(object)) {
return ofBeanLike(object);
}
return object;
}
private static boolean isScalar(Object object) {
return object == null
|| object instanceof String
|| object instanceof Character
|| object instanceof Byte
|| object instanceof Integer
|| object instanceof Short
|| object instanceof Long
|| object instanceof Float
|| object instanceof Double;
}
private static Object ofCollection(Collection> collection) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy