1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.grouplens.grapht.context;
21
22 import org.apache.commons.lang3.tuple.Pair;
23 import org.grouplens.grapht.reflect.InjectionPoint;
24 import org.grouplens.grapht.reflect.QualifierMatcher;
25 import org.grouplens.grapht.reflect.Satisfaction;
26
27
28
29
30
31
32 public final class ContextElements {
33 private ContextElements() {}
34
35
36
37
38 public static enum MatchPriority {
39 TYPE,
40 INVERTED,
41 ANY
42 }
43
44 public static ContextElementMatcher matchAny() {
45 return AnyMatcher.INSTANCE;
46 }
47
48 public static ContextElementMatcher matchType(Class<?> type) {
49 return new TypeElementMatcher(type);
50 }
51
52 public static ContextElementMatcher matchType(Class<?> type, QualifierMatcher qual) {
53 return new TypeElementMatcher(type, qual);
54 }
55
56 public static ContextElementMatcher invertMatch(ContextElementMatcher matcher) {
57 return new InvertedMatcher(matcher);
58 }
59
60 private static enum AnyMatcher implements ContextElementMatcher {
61 INSTANCE;
62
63 @Override
64 public MatchElement apply(Pair<Satisfaction, InjectionPoint> n) {
65 return MatchElementImpl.ANY;
66 }
67
68
69 @Override
70 public String toString() {
71 return "{Object}";
72 }
73 }
74
75 private static class InvertedMatcher implements ContextElementMatcher {
76 private static final long serialVersionUID = 1L;
77
78 private final ContextElementMatcher delegate;
79
80 public InvertedMatcher(ContextElementMatcher base) {
81 delegate = base;
82 }
83
84 @Override
85 public MatchElement apply(Pair<Satisfaction, InjectionPoint> elem) {
86 MatchElement result = delegate.apply(elem);
87 if (result == null) {
88 return MatchElementImpl.INVERTED;
89 } else {
90 return null;
91 }
92 }
93
94 @Override
95 public String toString() {
96 return "!" + delegate.toString();
97 }
98 }
99
100 private static enum MatchElementImpl implements MatchElement {
101 ANY(MatchPriority.ANY),
102 INVERTED(MatchPriority.INVERTED);
103
104 private final MatchPriority priority;
105
106 private MatchElementImpl(MatchPriority p) {
107 priority = p;
108 }
109
110 @Override
111 public MatchPriority getPriority() {
112 return priority;
113 }
114
115 @Override
116 public Integer getTypeDistance() {
117 return null;
118 }
119 }
120 }