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.solver;
21  
22  import org.grouplens.grapht.CachePolicy;
23  import org.grouplens.grapht.reflect.Desire;
24  import org.grouplens.grapht.reflect.MockQualifierMatcher;
25  import org.grouplens.grapht.reflect.QualifierMatcher;
26  
27  import java.util.EnumSet;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * MockBindRule is a simple implementation of BindRule where the matching
33   * bindings are represented as a map from input Desires to output Desires, via
34   * {@link #addMapping(Desire, Desire)}.
35   * 
36   * @author <a href="http://grouplens.org">GroupLens Research</a>
37   */
38  public class MockBindRule implements BindRule {
39      private final Map<Desire, Desire> bindings;
40      private boolean terminate;
41      private CachePolicy policy;
42      
43      public MockBindRule() {
44          bindings = new HashMap<Desire, Desire>();
45          policy = CachePolicy.NO_PREFERENCE;
46      }
47      
48      public MockBindRule(Desire in, Desire out) {
49          this();
50          addMapping(in, out);
51      }
52      
53      public MockBindRule setCachePolicy(CachePolicy policy) {
54          this.policy = policy;
55          return this;
56      }
57      
58      public MockBindRule setTerminatesChain(boolean terminate) {
59          this.terminate = terminate;
60          return this;
61      }
62      
63      public MockBindRule addMapping(Desire in, Desire out) {
64          bindings.put(in, out);
65          return this;
66      }
67      
68      @Override
69      public CachePolicy getCachePolicy() {
70          return policy;
71      }
72      
73      @Override
74      public boolean matches(Desire desire) {
75          return bindings.containsKey(desire);
76      }
77  
78      @Override
79      public Desire apply(Desire desire) {
80          return bindings.get(desire);
81      }
82  
83      @Override
84      public EnumSet<BindingFlag> getFlags() {
85          if (terminate) {
86              return EnumSet.of(BindingFlag.TERMINAL);
87          } else {
88              return BindingFlag.emptySet();
89          }
90      }
91  
92      @Override
93      public boolean isTerminal() {
94          return terminate;
95      }
96      
97      public QualifierMatcher getQualifier() {
98          return MockQualifierMatcher.any();
99      }
100 
101     @Override
102     public BindRuleBuilder newCopyBuilder() {
103         throw new UnsupportedOperationException("cannot configure mock bind rules");
104     }
105 
106     public int compareTo(BindRule other) {
107         return getQualifier().compareTo(((MockBindRule) other).getQualifier());
108     }
109 
110     @Override
111     public String toString() {
112         return getClass() + "@" + Integer.toHexString(System.identityHashCode(this));
113     }
114 }