|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2014 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
35 | 35 | import java.util.Set;
|
36 | 36 | import java.util.TreeSet;
|
37 | 37 |
|
| 38 | +import org.springframework.util.ObjectUtils; |
| 39 | + |
38 | 40 | import static org.springframework.beans.PropertyDescriptorUtils.*;
|
39 | 41 |
|
40 | 42 | /**
|
@@ -115,7 +117,7 @@ private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescript
|
115 | 117 | matches.add(method);
|
116 | 118 | }
|
117 | 119 | }
|
118 |
| - // sort non-void returning write methods to guard against the ill effects of |
| 120 | + // Sort non-void returning write methods to guard against the ill effects of |
119 | 121 | // non-deterministic sorting of methods returned from Class#getDeclaredMethods
|
120 | 122 | // under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
|
121 | 123 | Collections.sort(matches, new Comparator<Method>() {
|
@@ -310,8 +312,14 @@ public void setPropertyEditorClass(Class<?> propertyEditorClass) {
|
310 | 312 | }
|
311 | 313 |
|
312 | 314 | @Override
|
313 |
| - public boolean equals(Object obj) { |
314 |
| - return PropertyDescriptorUtils.equals(this, obj); |
| 315 | + public boolean equals(Object other) { |
| 316 | + return (this == other || (other instanceof PropertyDescriptor && |
| 317 | + PropertyDescriptorUtils.equals(this, (PropertyDescriptor) other))); |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public int hashCode() { |
| 322 | + return (ObjectUtils.nullSafeHashCode(getReadMethod()) * 29 + ObjectUtils.nullSafeHashCode(getWriteMethod())); |
315 | 323 | }
|
316 | 324 |
|
317 | 325 | @Override
|
@@ -437,24 +445,27 @@ public void setPropertyEditorClass(Class<?> propertyEditorClass) {
|
437 | 445 | * See java.beans.IndexedPropertyDescriptor#equals(java.lang.Object)
|
438 | 446 | */
|
439 | 447 | @Override
|
440 |
| - public boolean equals(Object obj) { |
441 |
| - if (this == obj) { |
| 448 | + public boolean equals(Object other) { |
| 449 | + if (this == other) { |
442 | 450 | return true;
|
443 | 451 | }
|
444 |
| - if (obj != null && obj instanceof IndexedPropertyDescriptor) { |
445 |
| - IndexedPropertyDescriptor other = (IndexedPropertyDescriptor) obj; |
446 |
| - if (!compareMethods(getIndexedReadMethod(), other.getIndexedReadMethod())) { |
447 |
| - return false; |
448 |
| - } |
449 |
| - if (!compareMethods(getIndexedWriteMethod(), other.getIndexedWriteMethod())) { |
450 |
| - return false; |
451 |
| - } |
452 |
| - if (getIndexedPropertyType() != other.getIndexedPropertyType()) { |
453 |
| - return false; |
454 |
| - } |
455 |
| - return PropertyDescriptorUtils.equals(this, obj); |
| 452 | + if (!(other instanceof IndexedPropertyDescriptor)) { |
| 453 | + return false; |
456 | 454 | }
|
457 |
| - return false; |
| 455 | + IndexedPropertyDescriptor otherPd = (IndexedPropertyDescriptor) other; |
| 456 | + return (ObjectUtils.nullSafeEquals(getIndexedReadMethod(), otherPd.getIndexedReadMethod()) && |
| 457 | + ObjectUtils.nullSafeEquals(getIndexedWriteMethod(), otherPd.getIndexedWriteMethod()) && |
| 458 | + ObjectUtils.nullSafeEquals(getIndexedPropertyType(), otherPd.getIndexedPropertyType()) && |
| 459 | + PropertyDescriptorUtils.equals(this, otherPd)); |
| 460 | + } |
| 461 | + |
| 462 | + @Override |
| 463 | + public int hashCode() { |
| 464 | + int hashCode = ObjectUtils.nullSafeHashCode(getReadMethod()); |
| 465 | + hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getWriteMethod()); |
| 466 | + hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getIndexedReadMethod()); |
| 467 | + hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getIndexedWriteMethod()); |
| 468 | + return hashCode; |
458 | 469 | }
|
459 | 470 |
|
460 | 471 | @Override
|
@@ -595,40 +606,12 @@ else if (params[1].isAssignableFrom(indexedPropertyType)) {
|
595 | 606 | * editor and flags are equivalent.
|
596 | 607 | * @see PropertyDescriptor#equals(Object)
|
597 | 608 | */
|
598 |
| - public static boolean equals(PropertyDescriptor pd1, Object obj) { |
599 |
| - if (pd1 == obj) { |
600 |
| - return true; |
601 |
| - } |
602 |
| - if (obj != null && obj instanceof PropertyDescriptor) { |
603 |
| - PropertyDescriptor pd2 = (PropertyDescriptor) obj; |
604 |
| - if (!compareMethods(pd1.getReadMethod(), pd2.getReadMethod())) { |
605 |
| - return false; |
606 |
| - } |
607 |
| - if (!compareMethods(pd1.getWriteMethod(), pd2.getWriteMethod())) { |
608 |
| - return false; |
609 |
| - } |
610 |
| - if (pd1.getPropertyType() == pd2.getPropertyType() && |
611 |
| - pd1.getPropertyEditorClass() == pd2.getPropertyEditorClass() && |
612 |
| - pd1.isBound() == pd2.isBound() && pd1.isConstrained() == pd2.isConstrained()) { |
613 |
| - return true; |
614 |
| - } |
615 |
| - } |
616 |
| - return false; |
617 |
| - } |
618 |
| - |
619 |
| - /* |
620 |
| - * See PropertyDescriptor#compareMethods |
621 |
| - */ |
622 |
| - public static boolean compareMethods(Method a, Method b) { |
623 |
| - if ((a == null) != (b == null)) { |
624 |
| - return false; |
625 |
| - } |
626 |
| - if (a != null) { |
627 |
| - if (!a.equals(b)) { |
628 |
| - return false; |
629 |
| - } |
630 |
| - } |
631 |
| - return true; |
| 609 | + public static boolean equals(PropertyDescriptor pd, PropertyDescriptor otherPd) { |
| 610 | + return (ObjectUtils.nullSafeEquals(pd.getReadMethod(), otherPd.getReadMethod()) && |
| 611 | + ObjectUtils.nullSafeEquals(pd.getWriteMethod(), otherPd.getWriteMethod()) && |
| 612 | + ObjectUtils.nullSafeEquals(pd.getPropertyType(), otherPd.getPropertyType()) && |
| 613 | + ObjectUtils.nullSafeEquals(pd.getPropertyEditorClass(), otherPd.getPropertyEditorClass()) && |
| 614 | + pd.isBound() == otherPd.isBound() && pd.isConstrained() == otherPd.isConstrained()); |
632 | 615 | }
|
633 | 616 | }
|
634 | 617 |
|
|
0 commit comments