Created
September 4, 2022 15:55
-
-
Save eliseevdry/9198e9e0b424484818c622d6b99b7e68 to your computer and use it in GitHub Desktop.
Util class for comparing Object by same fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.lang.reflect.Type; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public class FieldsComparingUtils { | |
public static boolean equalsForSameFields(Object o1, Object o2, String... exceptFieldNames) throws IllegalAccessException { | |
Class<?> o1Class = o1.getClass(); | |
Class<?> o2Class = o2.getClass(); | |
Field[] o1Fields = getAllFieldsWithInherited(o1Class); | |
Field[] o2Fields = getAllFieldsWithInherited(o2Class); | |
Set<String> fieldSet = getSameFieldsAsName(o1Fields, o2Fields, Arrays.asList(exceptFieldNames)); | |
if (fieldSet.isEmpty()) { | |
return false; | |
} | |
List<Field> o1comparingFields = Arrays.stream(o1Fields).filter(field -> fieldSet.contains(field.getName())).sorted(Comparator.comparing(Field::getName)).toList(); | |
List<Field> o2comparingFields = Arrays.stream(o2Fields).filter(field -> fieldSet.contains(field.getName())).sorted(Comparator.comparing(Field::getName)).toList(); | |
for (int i = 0; i < o1comparingFields.size(); i++) { | |
Field o1Field = o1comparingFields.get(i); | |
o1Field.setAccessible(true); | |
Field o2Field = o2comparingFields.get(i); | |
o2Field.setAccessible(true); | |
Object o1FieldValue = o1Field.get(o1); | |
Object o2FieldValue = o2Field.get(o2); | |
if (!o1FieldValue.equals(o2FieldValue)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static Set<String> getSameFieldsAsName(Field[] fields1, Field[] fields2, List<String> exceptFieldNames) { | |
Map<String, Type> fieldMap = Arrays.stream(fields1) | |
.filter(field -> !exceptFieldNames.contains(field.getName())) | |
.collect(Collectors.toMap(Field::getName, Field::getGenericType)); | |
Map<String, Type> fieldMap2 = Arrays.stream(fields2).collect(Collectors.toMap(Field::getName, Field::getGenericType)); | |
fieldMap.keySet().retainAll(fieldMap2.keySet()); | |
fieldMap.values().retainAll(fieldMap2.values()); | |
return fieldMap.keySet(); | |
} | |
public static Field[] getAllFieldsWithInherited(Class<?> clazz) { | |
Field[] fields = clazz.getDeclaredFields(); | |
Class<?> superClazz = clazz.getSuperclass(); | |
if (superClazz != null) { | |
fields = concatWithArrayCopy(fields, getAllFieldsWithInherited(superClazz)); | |
} | |
return fields; | |
} | |
public static <T> T[] concatWithArrayCopy(T[] array1, T[] array2) { | |
T[] result = Arrays.copyOf(array1, array1.length + array2.length); | |
System.arraycopy(array2, 0, result, array1.length, array2.length); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment