From bc1de44c602192e59ff66d1a5d1f1e9cdcc4a2bb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 14 Sep 2014 23:26:43 -0400 Subject: core: Add PotentialAnnotation.from(Key) method. --- .../sangria/core/PotentialAnnotation.java | 70 +++++++++++++++++++++- .../sangria/core/PotentialAnnotationTest.java | 18 ++++-- 2 files changed, 83 insertions(+), 5 deletions(-) (limited to 'sangria-core') diff --git a/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java b/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java index 302e5e1..7c3680f 100644 --- a/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java +++ b/sangria-core/src/main/java/com/tavianator/sangria/core/PotentialAnnotation.java @@ -29,7 +29,7 @@ import com.google.inject.spi.Message; * A record of stored annotations, perfect for builders with {@code annotatedWith()} methods. * * @author Tavian Barnes (tavianator@tavianator.com) - * @version 1.1 + * @version 1.2 * @since 1.1 */ public abstract class PotentialAnnotation { @@ -75,6 +75,24 @@ public abstract class PotentialAnnotation { return NONE; } + /** + * @return A {@link PotentialAnnotation} with the annotation from a {@link Key}. + * @since 1.2 + */ + public static PotentialAnnotation from(Key key) { + Annotation instance = key.getAnnotation(); + if (instance != null) { + return none().annotatedWith(instance); + } + + Class type = key.getAnnotationType(); + if (type != null) { + return none().annotatedWith(type); + } + + return none(); + } + private PotentialAnnotation() { } @@ -139,6 +157,12 @@ public abstract class PotentialAnnotation { */ public abstract T accept(Visitor visitor); + @Override + public abstract boolean equals(Object o); + + @Override + public abstract int hashCode(); + @Override public abstract String toString(); @@ -171,6 +195,16 @@ public abstract class PotentialAnnotation { return visitor.visitNoAnnotation(); } + @Override + public boolean equals(Object o) { + return o == this || o instanceof NoAnnotation; + } + + @Override + public int hashCode() { + return NoAnnotation.class.hashCode(); + } + @Override public String toString() { return "[no annotation]"; @@ -202,6 +236,23 @@ public abstract class PotentialAnnotation { return visitor.visitAnnotationType(annotationType); } + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof AnnotationType)) { + return false; + } + + AnnotationType other = (AnnotationType)o; + return annotationType.equals(other.annotationType); + } + + @Override + public int hashCode() { + return annotationType.hashCode(); + } + @Override public String toString() { return "@" + annotationType.getCanonicalName(); @@ -233,6 +284,23 @@ public abstract class PotentialAnnotation { return visitor.visitAnnotationInstance(annotation); } + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (!(o instanceof AnnotationInstance)) { + return false; + } + + AnnotationInstance other = (AnnotationInstance)o; + return annotation.equals(other.annotation); + } + + @Override + public int hashCode() { + return annotation.hashCode(); + } + @Override public String toString() { return annotation.toString(); diff --git a/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java b/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java index c4ccc36..4c53ef4 100644 --- a/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java +++ b/sangria-core/src/test/java/com/tavianator/sangria/core/PotentialAnnotationTest.java @@ -36,7 +36,7 @@ import static org.junit.Assert.*; * Tests for {@link PotentialAnnotation}s. * * @author Tavian Barnes (tavianator@tavianator.com) - * @version 1.1 + * @version 1.2 * @since 1.1 */ public class PotentialAnnotationTest { @@ -63,8 +63,8 @@ public class PotentialAnnotationTest { @Test(expected = CreationException.class) public void testInvalidAnnotatedWithInstance() { - none.annotatedWith(Names.named("name")) - .annotatedWith(Names.named("name")); + none.annotatedWith(nameAnnotation) + .annotatedWith(nameAnnotation); } @Test @@ -77,6 +77,16 @@ public class PotentialAnnotationTest { equalTo(new Key(nameAnnotation) { })); } + @Test + public void testFromKey() { + assertThat(PotentialAnnotation.from(new Key() { }), + equalTo(none)); + assertThat(PotentialAnnotation.from(new Key(Simple.class) { }), + equalTo(none.annotatedWith(Simple.class))); + assertThat(PotentialAnnotation.from(new Key(nameAnnotation) { }), + equalTo(none.annotatedWith(nameAnnotation))); + } + @Test public void testVisitor() { PotentialAnnotation.Visitor visitor = new PotentialAnnotation.Visitor() { @@ -114,7 +124,7 @@ public class PotentialAnnotationTest { } /** - * Needed to avoid compilation error to to inferred type being anonymous class. + * Needed to avoid compilation error due to inferred type being anonymous class. */ private static Matcher> equalTo(Key key) { return Matchers.equalTo(key); -- cgit v1.2.3