1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| import lombok.SneakyThrows;
import java.beans.PropertyDescriptor; import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.util.function.BiFunction; import java.util.stream.Collectors;
public interface Equable extends Serializable { long serialVersionUID = 1L;
default boolean onlyEqualsTo(Equable other) { final List<EqualGetter<Equable>> getterList = listOnlyEqualsToGetter();
if (getterList == null || getterList.isEmpty()) { return Objects.equals(other, this); }
for (EqualGetter<Equable> equalGetter : getterList) { Object o1 = equalGetter.apply(this); Object o2 = equalGetter.apply(other);
if (o1 instanceof Equable && o2 instanceof Equable) return ((Equable) o1).onlyEqualsTo((Equable) o2);
if (o1 instanceof Collection && o2 instanceof Collection) { if (notEqualsCollection((Collection<?>) o1, (Collection<?>) o2, Equable::onlyEqualsTo)) return false;
continue; }
if (o1 instanceof Map && o2 instanceof Map) { if (notEqualsMap((Map<?, ?>) o1, (Map<?, ?>) o2, Equable::onlyEqualsTo)) return false; continue; }
if (!Objects.equals(o1, o2)) { return false; } } return true; }
default boolean notEqualsMap(Map<?, ?> o1, Map<?, ?> o2, BiFunction<Equable, Equable, Boolean> biFunc) { final Collection<?> set1 = o1.keySet(); final Collection<?> set2 = o2.keySet(); if (notEqualsCollection(set1, set2, biFunc)) return true;
final Collection<?> values1 = o1.values(); final Collection<?> values2 = o2.values(); return notEqualsCollection(values1, values2, biFunc); }
default boolean notEqualsCollection(Collection<?> o1, Collection<?> o2, BiFunction<Equable, Equable, Boolean> biFunc) { if (Objects.isNull(o1) && Objects.isNull(o2)) { return false; }
if (Objects.isNull(o1) || Objects.isNull(o2)) { return true; }
if (o1.isEmpty() && o2.isEmpty()) { return false; }
if (o1.size() != o2.size()) { return true; }
o1 = o1.stream().sorted(Comparator.comparingInt(Object::hashCode)).collect(Collectors.toList()); o2 = o2.stream().sorted(Comparator.comparingInt(Object::hashCode)).collect(Collectors.toList());
final Iterator<?> iterator1 = o1.iterator(); final Iterator<?> iterator2 = o2.iterator(); while (iterator1.hasNext() && iterator2.hasNext()) { final Object next1 = iterator1.next(); final Object next2 = iterator2.next(); if (next1 instanceof Equable && next2 instanceof Equable) { if (!biFunc.apply((Equable) next1, (Equable) next2)) { return true; } } else { return !Objects.equals(next1, next2); } } return false; }
default <T extends Equable> List<EqualGetter<T>> listOnlyEqualsToGetter() { return Collections.emptyList(); }
@FunctionalInterface interface EqualGetter<T extends Equable> extends Serializable { Object apply(T source); }
@SneakyThrows default boolean ignoreEqualsTo(Equable other) { if (Objects.equals(this, other)) { return true; }
final Class<? extends Equable> aClass1 = this.getClass(); final Class<? extends Equable> aClass2 = other.getClass(); if (!Objects.equals(aClass1, aClass2)) { return false; }
final Field[] declaredFields = aClass1.getDeclaredFields(); for (Field declaredField : declaredFields) {
final EqualIgnored isIgnored = declaredField.getAnnotation(EqualIgnored.class); if (Objects.nonNull(isIgnored)) { continue; }
final PropertyDescriptor propertyDescriptor = new PropertyDescriptor(declaredField.getName(), aClass1); final Method readMethod = propertyDescriptor.getReadMethod();
final Object o1 = readMethod.invoke(this); final Object o2 = readMethod.invoke(other);
if (o1 instanceof Equable && o2 instanceof Equable) return ((Equable) o1).ignoreEqualsTo((Equable) o2);
if (o1 instanceof Collection && o2 instanceof Collection) { if (notEqualsCollection((Collection<?>) o1, (Collection<?>) o2, Equable::ignoreEqualsTo)) return false;
continue; }
if (o1 instanceof Map && o2 instanceof Map) {
if (notEqualsMap((Map<?, ?>) o1, (Map<?, ?>) o2, Equable::ignoreEqualsTo)) return false;
continue; }
if (!Objects.equals(o1, o2)) { return false; } }
return true; }
}
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface EqualIgnored { }
|