1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  package org.grouplens.grapht;
21  
22  import org.grouplens.grapht.context.ContextElementMatcher;
23  import org.grouplens.grapht.context.ContextElements;
24  import org.grouplens.grapht.context.ContextPattern;
25  import org.grouplens.grapht.reflect.QualifierMatcher;
26  import org.grouplens.grapht.context.Multiplicity;
27  import org.grouplens.grapht.reflect.Qualifiers;
28  
29  import javax.annotation.Nullable;
30  import java.lang.annotation.Annotation;
31  
32  
33  
34  
35  
36  
37  class ContextImpl extends AbstractContext {
38      private final BindingFunctionBuilder config;
39      private final ContextPattern pattern;
40      private final boolean anchored;
41  
42      
43  
44  
45  
46  
47  
48  
49      private ContextImpl(BindingFunctionBuilder config, ContextPattern context, boolean anchored) {
50          this.config = config;
51          this.pattern = context;
52          this.anchored = anchored;
53      }
54  
55      public static ContextImpl root(BindingFunctionBuilder config) {
56          return new ContextImpl(config, ContextPattern.empty(), false);
57      }
58      
59      public BindingFunctionBuilder getBuilder() {
60          return config;
61      }
62      
63      
64  
65  
66  
67  
68  
69      public ContextPattern getContextPattern() {
70          if (anchored) {
71              return pattern;
72          } else {
73              return pattern.appendDotStar();
74          }
75      }
76      
77      @Override
78      public <T> Binding<T> bind(Class<T> type) {
79          return new BindingImpl<T>(this, type);
80      }
81  
82      @Override
83      public Context within(Class<?> type) {
84          return in(Qualifiers.matchDefault(), type, false);
85      }
86  
87      @Override
88      public Context within(@Nullable Class<? extends Annotation> qualifier, Class<?> type) {
89          return in(Qualifiers.match(qualifier), type, false);
90      }
91      
92      @Override
93      public Context within(@Nullable Annotation annot, Class<?> type) {
94          return in(Qualifiers.match(annot), type, false);
95      }
96  
97      @Override
98      public Context matching(ContextPattern pat) {
99          return new ContextImpl(config, pattern.append(pat), true);
100     }
101 
102     @Override
103     public Context at(Class<?> type) {
104         return in(Qualifiers.matchDefault(), type, true);
105     }
106 
107     @Override
108     public Context at(@Nullable Class<? extends Annotation> qualifier, Class<?> type) {
109         return in(Qualifiers.match(qualifier), type, true);
110     }
111 
112     @Override
113     public Context at(@Nullable Annotation annot, Class<?> type) {
114         return in(Qualifiers.match(annot), type, true);
115     }
116     
117     private Context in(QualifierMatcher q, Class<?> type, boolean anchored) {
118         ContextElementMatcher nextMatcher = ContextElements.matchType(type, q);
119         ContextPattern pat = getContextPattern().append(nextMatcher, Multiplicity.ONE);
120 
121         return new ContextImpl(config, pat, anchored);
122     }
123 }