View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.di.impl;
17  
18  import net.sf.jguiraffe.di.BeanProvider;
19  import net.sf.jguiraffe.di.BeanStore;
20  import net.sf.jguiraffe.di.Dependency;
21  import net.sf.jguiraffe.di.DependencyProvider;
22  import net.sf.jguiraffe.di.InjectionException;
23  
24  /**
25   * <p>
26   * An implementation of the <code>Dependency</code> interface that allows to
27   * define a dependency based on the name of a bean.
28   * </p>
29   *
30   * @author Oliver Heger
31   * @version $Id: NameDependency.java 205 2012-01-29 18:29:57Z oheger $
32   */
33  public final class NameDependency implements Dependency
34  {
35      /** Stores the name of the dependent bean. */
36      private String name;
37  
38      /**
39       * Creates a new instance of <code>NameDependency</code> and sets the name
40       * of the dependent bean. Clients create new instances using the static
41       * factory method.
42       *
43       * @param name the name of the dependent bean
44       */
45      private NameDependency(String name)
46      {
47          this.name = name;
48      }
49  
50      /**
51       * Returns the name of the dependent bean.
52       *
53       * @return the name of the bean this dependency refers to
54       */
55      public String getName()
56      {
57          return name;
58      }
59  
60      /**
61       * Resolves the named dependency from the specified bean store. This is done
62       * recursively for the bean store's parent if necessary.
63       *
64       * @param store the bean store
65       * @param depProvider the dependency provider (not used here)
66       * @return the found bean provider
67       * @throws InjectionException if the provider cannot be resolved
68       */
69      public BeanProvider resolve(BeanStore store, DependencyProvider depProvider)
70      {
71          if (store == null)
72          {
73              throw new InjectionException("Cannot resolve named dependency: "
74                      + getName());
75          }
76  
77          BeanProvider result = store.getBeanProvider(getName());
78          return (result != null) ? result : resolve(store.getParent(),
79                  depProvider);
80      }
81  
82      /**
83       * Tests whether this object equals another one. Two objects of this class
84       * are considered equal if and only if they refer to the same dependent
85       * bean.
86       *
87       * @param obj the object to compare to
88       * @return a flag whether the objects are equal
89       */
90      @Override
91      public boolean equals(Object obj)
92      {
93          if (obj == this)
94          {
95              return true;
96          }
97          if (!(obj instanceof NameDependency))
98          {
99              return false;
100         }
101 
102         NameDependency c = (NameDependency) obj;
103         return (getName() == null) ? c.getName() == null : getName().equals(
104                 c.getName());
105     }
106 
107     /**
108      * Returns a hash code for this object.
109      *
110      * @return a hash code
111      */
112     @Override
113     public int hashCode()
114     {
115         final int seed = 17;
116         final int factor = 23;
117         int result = seed;
118         if (getName() != null)
119         {
120             result = factor * result + getName().hashCode();
121         }
122         return result;
123     }
124 
125     /**
126      * Returns a string representation of this object. This string especially
127      * contains the name of the bean this dependency is about.
128      *
129      * @return a string for this object
130      */
131     @Override
132     public String toString()
133     {
134         StringBuilder buf = new StringBuilder(getClass().getName());
135         buf.append("[ name = ").append(getName()).append(" ]");
136         return buf.toString();
137     }
138 
139     /**
140      * Returns an instance of this class for the bean with the specified name.
141      *
142      * @param name the name of the dependent bean
143      * @return an instance referring to the specified dependent bean
144      */
145     public static NameDependency getInstance(String name)
146     {
147         return new NameDependency(name);
148     }
149 }