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.Desire;
24  import org.grouplens.grapht.reflect.Satisfaction;
25  import org.grouplens.grapht.reflect.SatisfactionVisitor;
26  import org.grouplens.grapht.util.Preconditions;
27  
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   * Satisfaction implementation wrapping an instance. It has no dependencies, and
37   * the resulting providers just return the instance.
38   * 
39   * @author <a href="http://grouplens.org">GroupLens Research</a>
40   * @author <a href="http://grouplens.org">GroupLens Research</a>
41   */
42  public class InstanceSatisfaction implements Satisfaction, Serializable {
43      private static final long serialVersionUID = 1L;
44      @SuppressWarnings("squid:S1948") // serializable warning; satisfaction is serializable iff instance is
45      private final Object instance;
46  
47      /**
48       * Create a new instance node wrapping an instance.
49       * 
50       * @param obj The object to return.
51       * @throws NullPointerException if obj is null
52       */
53      public InstanceSatisfaction(Object obj) {
54          Preconditions.notNull("instance", obj);
55          instance = obj;
56      }
57      
58      /**
59       * @return The instance that satisfies this satisfaction
60       */
61      public Object getInstance() {
62          return instance;
63      }
64      
65      @Override
66      public CachePolicy getDefaultCachePolicy() {
67          return (getErasedType().getAnnotation(Singleton.class) != null ? CachePolicy.MEMOIZE : CachePolicy.NO_PREFERENCE);
68      }
69  
70      @Override
71      public List<Desire> getDependencies() {
72          return Collections.emptyList();
73      }
74  
75      @Override
76      public Type getType() {
77          return instance.getClass();
78      }
79  
80      @Override
81      public Class<?> getErasedType() {
82          return instance.getClass();
83      }
84  
85      @Override
86      public boolean hasInstance() {
87          return true;
88      }
89  
90      @Override
91      public <T> T visit(SatisfactionVisitor<T> visitor) {
92          return visitor.visitInstance(instance);
93      }
94  
95      @Override
96      @SuppressWarnings({ "rawtypes", "unchecked" })
97      public Instantiator makeInstantiator(Map<Desire,Instantiator> dependencies,
98                                           LifecycleManager injectionContainer) {
99          return Instantiators.ofInstance(instance);
100     }
101     
102     @Override
103     public boolean equals(Object o) {
104         if (!(o instanceof InstanceSatisfaction)) {
105             return false;
106         }
107         return ((InstanceSatisfaction) o).instance.equals(instance);
108     }
109     
110     @Override
111     public int hashCode() {
112         return instance.hashCode();
113     }
114     
115     @Override
116     public String toString() {
117         return "Instance(" + instance + ")";
118     }
119 }