View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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   * ContextImpl is the basic implementation of Context.
34   * 
35   * @author <a href="http://grouplens.org">GroupLens Research</a>
36   */
37  class ContextImpl extends AbstractContext {
38      private final BindingFunctionBuilder config;
39      private final ContextPattern pattern;
40      private final boolean anchored;
41  
42      /**
43       * Construct a new context implementation.
44       * @param config The function builder.
45       * @param context The context pattern.
46       * @param anchored Whether this is anchored.  If it is not anchored, {@code .*} will be appended
47       *                 to the context pattern for any calls other than to {@link #matching(ContextPattern)}.
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       * Get the context pattern for this builder.  It will never return {@code null}; for the root
65       * context, it will return {@link ContextPattern#any()}.
66       *
67       * @return The context pattern for this builder.
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 }