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.internal;
21  
22  import org.grouplens.grapht.*;
23  import org.grouplens.grapht.reflect.*;
24  import org.grouplens.grapht.util.Preconditions;
25  import org.grouplens.grapht.util.Types;
26  
27  import javax.inject.Provider;
28  import javax.inject.Singleton;
29  import java.io.Serializable;
30  import java.lang.reflect.Type;
31  import java.util.Collections;
32  import java.util.List;
33  import java.util.Map;
34  
35  
36  /**
37   * Satisfaction implementation wrapping an existing Provider instance. It has no
38   * dependencies and it always returns the same Provider when
39   * {@link Satisfaction#makeInstantiator(Map, LifecycleManager)} is invoked.
40   * 
41   * @author <a href="http://grouplens.org">GroupLens Research</a>
42   */
43  public class ProviderInstanceSatisfaction implements Satisfaction, Serializable {
44      private static final long serialVersionUID = 1L;
45      @SuppressWarnings("squid:S1948") // serializable warning; satisfaction is serializable iff provider is
46      private final Provider<?> provider;
47  
48      /**
49       * Create a new satisfaction that wraps the given Provider instance.
50       * 
51       * @param provider The provider
52       * @throws NullPointerException if provider is null
53       */
54      public ProviderInstanceSatisfaction(Provider<?> provider) {
55          Preconditions.notNull("provider", provider);
56          this.provider = provider;
57      }
58      
59      @Override
60      public CachePolicy getDefaultCachePolicy() {
61          return (getErasedType().getAnnotation(Singleton.class) != null ? CachePolicy.MEMOIZE : CachePolicy.NO_PREFERENCE);
62      }
63      
64      /**
65       * @return The provider instance returned by {@link Satisfaction#makeInstantiator(Map, LifecycleManager)}
66       */
67      public Provider<?> getProvider() {
68          return provider;
69      }
70      
71      @Override
72      public List<Desire> getDependencies() {
73          return Collections.emptyList();
74      }
75  
76      @Override
77      public Type getType() {
78          return Types.getProvidedType(provider);
79      }
80  
81      @Override
82      public Class<?> getErasedType() {
83          return Types.erase(getType());
84      }
85  
86      @Override
87      public boolean hasInstance() {
88          return false;
89      }
90  
91      @Override
92      public <T> T visit(SatisfactionVisitor<T> visitor) {
93          return visitor.visitProviderInstance(provider);
94      }
95  
96      @Override
97      public Instantiator makeInstantiator(Map<Desire,Instantiator> dependencies,
98                                           LifecycleManager lm) {
99          return Instantiators.ofProvider(provider);
100     }
101     
102     @Override
103     public boolean equals(Object o) {
104         return (o instanceof ProviderInstanceSatisfaction)
105                && ((ProviderInstanceSatisfaction) o).provider.equals(provider);
106     }
107     
108     @Override
109     public int hashCode() {
110         return provider.hashCode();
111     }
112     
113     @Override
114     public String toString() {
115         return "ProviderInstance(" + provider + ")";
116     }
117 }