Skip to content
Permalink
Browse files

Update for additional methods

  • Loading branch information...
Ghost-Programmer committed May 11, 2019
1 parent 0eb65cc commit 15bae162c7bd7e51e9179d706d5a1f7123aa82f2
Showing with 115 additions and 0 deletions.
  1. +115 −0 src/name/mymiller/extensions/lang/reflect/ReflectionUtil.java
@@ -18,6 +18,11 @@
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

import name.mymiller.extensions.utils.ObjectUtils;

/**
* Collection of Utility functions around Reflection to assist with creating,
@@ -72,6 +77,42 @@ static public Object instantiateClass(String className) throws InstantiationExce
return Class.forName(className).getDeclaredConstructor().newInstance();
}

/**
* Assign a value to all objects in a list, given the setter
*
* @param <T> Type of Objects in list
* @param <R> Value Type to set on Objects
* @param value Value to set on Objects
* @param list List of objects to set value.
* @param setter Reference to the Setter
*/
static public <T, R> void reassignToValue(R value, List<T> list, BiConsumer<T, R> setter) {
if ((list != null) && (value != null)) {
list.stream().forEach(item -> setter.accept(item, value));
}
}

/**
* Assign value between two objects of the same tiem by using the getter and
* setter
*
* @param <T> Type of Ojects
* @param <R> Value type to set on the objects
* @param source Source to retreive with the Getter.
* @param target Target to set with the Setter
* @param getter Reference to the Getter
* @param setter Reference to the setter
*/
static public <T, R> void setElements(T source, T target, Function<? super T, ? extends R> getter,
BiConsumer<T, R> setter) {
final R A = getter.apply(source);
final R B = getter.apply(target);

if ((A != null) && (B == null)) {
setter.accept(target, A);
}
}

/**
* Set the value on a field of a class. The field must have an mutator.
*
@@ -88,4 +129,78 @@ static public void setField(Object object, String fieldName, Object value)
throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
ReflectionUtil.getPropertyDescriptor(object, fieldName).getWriteMethod().invoke(object, value);
}

/**
* Method to determine if two elements have equals valutes
*
* @param <T> Type of Objects
* @param <R> Value type on objects
* @param source Source
* @param targetA First element
* @param targetB Second element
* @return boolean indicating if they are equals.
*/
static public <T, R> boolean validateElements(T targetA, T targetB, Function<? super T, ? extends R> getter) {
final R A = getter.apply(targetA);
final R B = getter.apply(targetB);

if ((A == null) || (B == null)) {
return false;
}

// Return true if they are set but not equal
return !A.equals(B);
}

/**
* Performs a check between two lists to determine if the elements are equal
* with a single check.
*
* @param <T> Type of Objects
* @param <R> Value type on objects
* @param source List to check against
* @param target List to check
* @param firstCheck Value to to compare on the equals
* @param first Getter to check the value.
* @return boolean indicating value.
*/
static public <T, R> boolean validateLists(List<T> source, List<T> target, boolean firstCheck,
Function<? super T, ? extends R> first) {
return ReflectionUtil.validateLists(source, target, firstCheck, first, false, null);
}

/**
* Performs a check between two lists to determine if the elements are equal
* with a double check.
*
* @param <T> Type of Objects
* @param <R> Value type on objects
* @param source List to check against
* @param target List to check
* @param firstCheck Value to to compare on the equals
* @param first Getter to check the value.
* @param secondCHeck Value to compare on the equals for the second check.
* @param second Getter to check on the second value
* @return boolean indicating value.
*/
static public <T, R> boolean validateLists(List<T> source, List<T> target, boolean firstCheck,
Function<? super T, ? extends R> first, boolean secondCheck, Function<? super T, ? extends R> second) {
if (ObjectUtils.isNotNullOrEmpty(source, target)) {
for (final T sItem : source) {
for (final T tItem : target) {
if (first.apply(sItem).equals(first.apply(tItem)) == firstCheck) {
if (second != null) {
if (second.apply(sItem).equals(second.apply(tItem)) == secondCheck) {
return true;
}
} else {
return true;
}
}
}
}
}

return false;
}
}

0 comments on commit 15bae16

Please sign in to comment.
You can’t perform that action at this time.