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.reflect;
21  
22  import java.lang.annotation.Annotation;
23  
24  /**
25   * MockDesire is a simple Desire implementation for use within certain types of
26   * tests.
27   * 
28   * @author <a href="http://grouplens.org">GroupLens Research</a>
29   */
30  public class MockDesire implements Desire {
31      private static final long serialVersionUID = 1L;
32  
33      private final Annotation qualifier;
34      private final Satisfaction satisfaction;
35      private final Class<?> desiredType;
36      
37      public MockDesire() {
38          this(null);
39      }
40      
41      public MockDesire(Satisfaction satisfaction) {
42          this(satisfaction, null);
43      }
44      
45      public MockDesire(Satisfaction satisfaction, Annotation qualifier) {
46          this((satisfaction == null ? Void.class : satisfaction.getErasedType()),
47               satisfaction, qualifier);
48      }
49      
50      public MockDesire(Class<?> desiredType, Satisfaction satisfaction, Annotation qualifier) {
51          this.desiredType = desiredType;
52          this.satisfaction = satisfaction;
53          this.qualifier = qualifier;
54      }
55      
56      @Override
57      public boolean isInstantiable() {
58          return satisfaction != null;
59      }
60  
61      @Override
62      public Satisfaction getSatisfaction() {
63          return satisfaction;
64      }
65  
66      @Override
67      public Class<?> getDesiredType() {
68          return desiredType;
69      }
70  
71      @Override
72      public InjectionPoint getInjectionPoint() {
73          return Desires.createInjectionPoint(qualifier, getDesiredType(), false);
74      }
75  
76      @Override
77      public Desire restrict(Class<?> type) {
78          return new MockDesire(type, satisfaction, qualifier);
79      }
80  
81      @Override
82      public Desire restrict(Satisfaction satisfaction) {
83          return new MockDesire(satisfaction.getErasedType(), satisfaction, qualifier);
84      }
85  
86      @Override
87      public String toString() {
88          StringBuilder sb = new StringBuilder();
89          sb.append("mock desire for ")
90            .append(desiredType);
91          if (qualifier != null) {
92              sb.append(" with qualifier ")
93                .append(qualifier);
94          }
95          if (satisfaction != null) {
96              sb.append(" (satisfied by ")
97                .append(satisfaction)
98                .append(")");
99          }
100         return sb.toString();
101     }
102 }