@OverrideEquality @Override public boolean equals(Object obj) { Equality<TailMap> equality = Equality.of(TailMap.class, this, obj); TailMap other = equality.other(); if (equality.other() == null) { return equality.returnValue(); } return Objects.equals(this.fromKey, other.fromKey); }
public boolean returnValue() { if (this.returnValue == 0) { throw new IllegalStateException( illegalStateToGetReturnValue(Equality.class) ); } return this.returnValue > 0; }
private static void collectOverrideEqualityClasses( Class<?> clazz, Set<Class<?>> classes) { if (clazz != Object.class) { for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(OverrideEquality.class)) { validateEqualsMethod(method); classes.add(clazz); return; } } if (!clazz.isInterface() && !clazz.isAnnotation()) { collectOverrideEqualityClasses(clazz.getSuperclass(), classes); } for (Class<?> interfacze : clazz.getInterfaces()) { collectOverrideEqualityClasses(interfacze, classes); } } }
@SuppressWarnings("unchecked") public static <T> Equality<T> of(Class<T> clazz, T thiz, Object obj) { if (Arguments.mustNotBeNull("thiz", thiz) == obj) { return new Equality<>(true); } if (obj == null) { return new Equality<>(false); } Class<? extends Object> thisClass = thiz.getClass(); Arguments.mustBeInstanceOfOther("thisObj", thiz, "clazz", clazz); Class<? extends Object> objectClass = obj.getClass(); if (thisClass == objectClass) { return new Equality<>((T)obj); } if (!clazz.isAssignableFrom(objectClass)) { return new Equality<>(false); } Class<?>[] expectedOETypes = Equality.getOETypes(thisClass); Class<?>[] actualOETypes = Equality.getOETypes(objectClass); if (expectedOETypes.length != actualOETypes.length) { return new Equality<>(false); } for (int i = expectedOETypes.length - 1; i >= 0; i--) { if (expectedOETypes[i] != actualOETypes[i]) { return new Equality<>(false); } } return new Equality<>((T)obj); }
private static void validateEqualsMethod(Method method) { if ( !Modifier.isPublic(method.getModifiers()) || Modifier.isStatic(method.getModifiers()) || !"equals".equals(method.getName()) || boolean.class != method.getReturnType() || !Arrays.equals(method.getParameterTypes(), ONE_OBJECT_TYPE_ARRAY)) { throw new IllegalProgramException( invalidOverrideEqualityMethod(method, OverrideEquality.class) ); } }
private static Class<?>[] getOETypes(Class<?> clazz) { Class<?>[] oeTypes; Lock lock; (lock = CACHE_LOCK.readLock()).lock(); try { oeTypes = CACHE.get(clazz); //1st reading } finally { lock.unlock(); } if (oeTypes == null) { //1st checking (lock = CACHE_LOCK.writeLock()).lock(); try { oeTypes = CACHE.get(clazz); //2nd reading if (oeTypes == null) { //2nd checking oeTypes = getOETypes0(clazz); CACHE.put(clazz, oeTypes); } } finally { lock.unlock(); } } return oeTypes; }
private static Class<?>[] getOETypes0(Class<?> clazz) { Set<Class<?>> classes = new HashSet<Class<?>>(16); collectOverrideEqualityClasses(clazz, classes); if (classes.isEmpty()) { return EMPTY_OE_TYPES;
@OverrideEquality @Override public boolean equals(Object obj) { Equality<TailSet> equality = Equality.of(TailSet.class, this, obj); TailSet other = equality.other(); if (equality.other() == null) { return equality.returnValue(); } return Objects.equals(this.fromElement, other.fromElement); }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<SubSet> equality = Equality.of(SubSet.class, this, obj); SubSet other = equality.other(); if (equality.other() == null) { return equality.returnValue(); } return Objects.equals(this.fromElement, other.fromElement) && Objects.equals(this.toElement, other.toElement); }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<HeadSet> equality = Equality.of(HeadSet.class, this, obj); HeadSet other = equality.other(); if (equality.other() == null) { return equality.returnValue(); } return Objects.equals(this.toElement, other.toElement); }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<SubMap> equality = Equality.of(SubMap.class, this, obj); SubMap other = equality.other(); if (other == null) { return false; } return super.equals(obj) && this.fromInclusive == other.fromInclusive && this.toInclusive == other.toInclusive; }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<HeadMap> equality = Equality.of(HeadMap.class, this, obj); HeadMap other = equality.other(); if (equality.other() == null) { return equality.returnValue(); } return Objects.equals(this.toKey, other.toKey); }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<SubSet> equality = Equality.of(SubSet.class, this, obj); SubSet other = equality.other(); if (other == null) { return false; } return super.equals(obj) && this.fromInclusive == other.fromInclusive && this.toInclusive == other.toInclusive; }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<SubMap> equality = Equality.of(SubMap.class, this, obj); SubMap other = equality.other(); if (equality.other() == null) { return equality.returnValue(); } return Objects.equals(this.fromKey, other.fromKey) && Objects.equals(this.toKey, other.toKey); }
@OverrideEquality @Override public boolean equals(Object obj) { Equality<MemberInfo> equality = Equality.of(MemberInfo.class, this, obj); MemberInfo other = equality.other(); if (other == null) { return equality.returnValue(); } return this.declaringClass.equals(other.declaringClass) && this.member.equals(other.member); }
@Override public boolean equals(Object obj) { Equality<Point2D> equality = Equality.of(Point2D.class, this, obj); Point2D other = equality.other(); if (other == null) { return equality.returnValue(); } /* * Notes: * (1) Left side of "==" can use field to optimize the performance * (2) Right side of "==" must use getter because "other" may be proxy(For example: Point2DReadOnlyProxy) */ return this.x == other.getX() && this.y == other.getY(); } }