-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an equivalence proxy for AnnotationInstance
An equivalence proxy is like an equivalence key (that is, its `equals()` and `hashCode()` implement equivalence instead of equality), but it also holds and provides access to the original (delegate) object.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/jboss/jandex/AnnotationInstanceEquivalenceProxy.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.jboss.jandex; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Holds an {@link AnnotationInstance} and implements equality and hash code as equivalence. | ||
* <p> | ||
* When using equivalence proxies, it is usually a mistake to obtain | ||
* the {@linkplain AnnotationInstance#target() target} of the delegate annotation instance. | ||
* <p> | ||
* <b>Thread-Safety</b> | ||
* </p> | ||
* This class is immutable and can be shared between threads without safe | ||
* publication. | ||
* | ||
* @see AnnotationInstance#equivalentTo(AnnotationInstance) | ||
* @see AnnotationInstance#equivalenceHashCode() | ||
*/ | ||
public final class AnnotationInstanceEquivalenceProxy { | ||
private final AnnotationInstance annotation; | ||
|
||
AnnotationInstanceEquivalenceProxy(AnnotationInstance annotation) { | ||
this.annotation = Objects.requireNonNull(annotation); | ||
} | ||
|
||
public AnnotationInstance get() { | ||
return annotation; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
return annotation.equivalentTo(((AnnotationInstanceEquivalenceProxy) o).annotation); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return annotation.equivalenceHashCode(); | ||
} | ||
} |
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