784 * That is, it returns {@code true} if and only if the specified object |
784 * That is, it returns {@code true} if and only if the specified object |
785 * is also a method type with exactly the same parameters and return type. |
785 * is also a method type with exactly the same parameters and return type. |
786 * @param x object to compare |
786 * @param x object to compare |
787 * @see Object#equals(Object) |
787 * @see Object#equals(Object) |
788 */ |
788 */ |
|
789 // This implementation may also return true if x is a WeakEntry containing |
|
790 // a method type that is equal to this. This is an internal implementation |
|
791 // detail to allow for faster method type lookups. |
|
792 // See ConcurrentWeakInternSet.WeakEntry#equals(Object) |
789 @Override |
793 @Override |
790 public boolean equals(Object x) { |
794 public boolean equals(Object x) { |
791 return this == x || x instanceof MethodType && equals((MethodType)x); |
795 if (this == x) { |
|
796 return true; |
|
797 } |
|
798 if (x instanceof MethodType) { |
|
799 return equals((MethodType)x); |
|
800 } |
|
801 if (x instanceof ConcurrentWeakInternSet.WeakEntry) { |
|
802 Object o = ((ConcurrentWeakInternSet.WeakEntry)x).get(); |
|
803 if (o instanceof MethodType) { |
|
804 return equals((MethodType)o); |
|
805 } |
|
806 } |
|
807 return false; |
792 } |
808 } |
793 |
809 |
794 private boolean equals(MethodType that) { |
810 private boolean equals(MethodType that) { |
795 return this.rtype == that.rtype |
811 return this.rtype == that.rtype |
796 && Arrays.equals(this.ptypes, that.ptypes); |
812 && Arrays.equals(this.ptypes, that.ptypes); |
806 * @see #equals(Object) |
822 * @see #equals(Object) |
807 * @see List#hashCode() |
823 * @see List#hashCode() |
808 */ |
824 */ |
809 @Override |
825 @Override |
810 public int hashCode() { |
826 public int hashCode() { |
811 int hashCode = 31 + rtype.hashCode(); |
827 int hashCode = 31 + rtype.hashCode(); |
812 for (Class<?> ptype : ptypes) |
828 for (Class<?> ptype : ptypes) |
813 hashCode = 31*hashCode + ptype.hashCode(); |
829 hashCode = 31 * hashCode + ptype.hashCode(); |
814 return hashCode; |
830 return hashCode; |
815 } |
831 } |
816 |
832 |
817 /** |
833 /** |
818 * Returns a string representation of the method type, |
834 * Returns a string representation of the method type, |
819 * of the form {@code "(PT0,PT1...)RT"}. |
835 * of the form {@code "(PT0,PT1...)RT"}. |
1284 */ |
1300 */ |
1285 public T get(T elem) { |
1301 public T get(T elem) { |
1286 if (elem == null) throw new NullPointerException(); |
1302 if (elem == null) throw new NullPointerException(); |
1287 expungeStaleElements(); |
1303 expungeStaleElements(); |
1288 |
1304 |
1289 WeakEntry<T> value = map.get(new WeakEntry<>(elem)); |
1305 WeakEntry<T> value = map.get(elem); |
1290 if (value != null) { |
1306 if (value != null) { |
1291 T res = value.get(); |
1307 T res = value.get(); |
1292 if (res != null) { |
1308 if (res != null) { |
1293 return res; |
1309 return res; |
1294 } |
1310 } |
1336 public WeakEntry(T key, ReferenceQueue<T> queue) { |
1352 public WeakEntry(T key, ReferenceQueue<T> queue) { |
1337 super(key, queue); |
1353 super(key, queue); |
1338 hashcode = key.hashCode(); |
1354 hashcode = key.hashCode(); |
1339 } |
1355 } |
1340 |
1356 |
1341 public WeakEntry(T key) { |
1357 /** |
1342 super(key); |
1358 * This implementation returns {@code true} if {@code obj} is another |
1343 hashcode = key.hashCode(); |
1359 * {@code WeakEntry} whose referent is equals to this referent, or |
1344 } |
1360 * if {@code obj} is equals to the referent of this. This allows |
1345 |
1361 * lookups to be made without wrapping in a {@code WeakEntry}. |
|
1362 * |
|
1363 * @param obj the object to compare |
|
1364 * @return true if {@code obj} is equals to this or the referent of this |
|
1365 * @see MethodType#equals(Object) |
|
1366 * @see Object#equals(Object) |
|
1367 */ |
1346 @Override |
1368 @Override |
1347 public boolean equals(Object obj) { |
1369 public boolean equals(Object obj) { |
|
1370 Object mine = get(); |
1348 if (obj instanceof WeakEntry) { |
1371 if (obj instanceof WeakEntry) { |
1349 Object that = ((WeakEntry) obj).get(); |
1372 Object that = ((WeakEntry) obj).get(); |
1350 Object mine = get(); |
|
1351 return (that == null || mine == null) ? (this == obj) : mine.equals(that); |
1373 return (that == null || mine == null) ? (this == obj) : mine.equals(that); |
1352 } |
1374 } |
1353 return false; |
1375 return (mine == null) ? (obj == null) : mine.equals(obj); |
1354 } |
1376 } |
1355 |
1377 |
1356 @Override |
1378 @Override |
1357 public int hashCode() { |
1379 public int hashCode() { |
1358 return hashcode; |
1380 return hashcode; |