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;
21  
22  import org.apache.commons.lang3.builder.EqualsBuilder;
23  import org.apache.commons.lang3.builder.HashCodeBuilder;
24  import org.grouplens.grapht.reflect.Satisfaction;
25  import org.grouplens.grapht.util.Preconditions;
26  
27  import java.io.Serializable;
28  
29  /**
30   * A component to be instantiated in the final dependency plan.  A component consists of a {@link
31   * Satisfaction} and related information for instantiating it (such as the {@link CachePolicy}).
32   *
33   * @author <a href="http://grouplens.org">GroupLens Research</a>
34   */
35  public class Component implements Serializable {
36      private static final long serialVersionUID = 5L;
37      
38      private final Satisfaction satisfaction;
39      private final CachePolicy cachePolicy;
40  
41      private Component(Satisfaction satisfaction, CachePolicy policy) {
42          Preconditions.notNull("satisfaction", satisfaction);
43          Preconditions.notNull("policy", policy);
44  
45          this.satisfaction = satisfaction;
46          cachePolicy = policy;
47      }
48  
49      /**
50       * Create a new Component wrapping the given satisfaction and cache policy.  The injector is
51       * responsible for using the satisfaction to implement this component consistent with its
52       * cache policy.
53       *
54       * @param satisfaction The satisfaction to wrap
55       * @param policy       The policy used with this satisfaction
56       * @throws NullPointerException the satisfaction or policy is null
57       */
58      public static Component create(Satisfaction satisfaction, CachePolicy policy) {
59          return new Component(satisfaction, policy);
60      }
61  
62      /**
63       * @return The Satisfaction stored in this pair
64       */
65      public Satisfaction getSatisfaction() {
66          return satisfaction;
67      }
68      
69      /**
70       * @return The CachePolicy that must be implemented on top of the providers
71       *         from this {@link #getSatisfaction() satisfaction}
72       */
73      public CachePolicy getCachePolicy() {
74          return cachePolicy;
75      }
76  
77      @Override
78      public boolean equals(Object o) {
79          if (!(o instanceof Component)) {
80              return false;
81          }
82              
83          Component c = (Component) o;
84          EqualsBuilder eqb = new EqualsBuilder();
85          return eqb.append(satisfaction, c.satisfaction)
86                    .append(cachePolicy, c.cachePolicy)
87                    .isEquals();
88      }
89      
90      @Override
91      public int hashCode() {
92          HashCodeBuilder hcb = new HashCodeBuilder();
93          return hcb.append(satisfaction)
94                    .append(cachePolicy)
95                    .toHashCode();
96      }
97      
98      @Override
99      public String toString() {
100         return "(" + satisfaction + ", " + cachePolicy + ")";
101     }
102 }