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 com.google.common.base.Predicate;
23  import org.grouplens.grapht.reflect.Desire;
24  import org.grouplens.grapht.solver.DesireChain;
25  
26  import javax.annotation.Nullable;
27  import java.io.Serializable;
28  import java.util.EnumSet;
29  
30  /**
31   * Track information about a particular resolved dependency. Used as the edge of DI graph nodes
32   *
33   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
34   */
35  public class Dependency implements Serializable {
36      private static final long serialVersionUID = 1L;
37  
38      private final DesireChain desireChain;
39      private final EnumSet<Flag> flags;
40  
41      private Dependency(DesireChain chain, EnumSet<Flag> flagSet) {
42          desireChain = chain;
43          flags = flagSet.clone();
44      }
45  
46      public static Dependency create(DesireChain desires, EnumSet<Flag> flags) {
47          return new Dependency(desires, flags);
48      }
49  
50      /**
51       * Get the desire chain associated with this dependency.
52       * @return The chain of desires followed in resolving this dependency.
53       */
54      public DesireChain getDesireChain() {
55          return desireChain;
56      }
57  
58      /**
59       * Convenience method to get the initial desire that prompted this dependency.
60       * @return The initial desire that prompted this dependency.
61       */
62      public Desire getInitialDesire() {
63          return desireChain.getInitialDesire();
64      }
65  
66      /**
67       * Get the flags associated with this dependency.
68       * @return The flags associated with this dependency.
69       */
70      public EnumSet<Flag> getFlags() {
71          return flags;
72      }
73  
74      /**
75       * Query whether this dependency is immune to rewriting.
76       * @return {@code true} if this dependency cannot be rewritten during a graph rewrite.
77       */
78      public boolean isFixed() {
79          return flags.contains(Flag.FIXED);
80      }
81  
82      /**
83       * Create a predicate matching dependencies with the specified initial desire.
84       * @param d The desire.
85       * @return A predicate that accepts dependencies whose initial desire is {@code d}.
86       */
87      public static Predicate<Dependency> hasInitialDesire(final Desire d) {
88          return new Predicate<Dependency>() {
89              @Override
90              public boolean apply(@Nullable Dependency input) {
91                  return input != null && input.getInitialDesire().equals(d);
92              }
93          };
94      }
95  
96      @Override
97      public boolean equals(Object o) {
98          if (this == o) return true;
99          if (o == null || getClass() != o.getClass()) return false;
100 
101         Dependency that = (Dependency) o;
102 
103         if (!desireChain.equals(that.desireChain)) return false;
104         if (!flags.equals(that.flags)) return false;
105 
106         return true;
107     }
108 
109     @Override
110     public int hashCode() {
111         int result = desireChain.hashCode();
112         result = 31 * result + flags.hashCode();
113         return result;
114     }
115 
116     @Override
117     public String toString() {
118         return "Dependency(" + desireChain + ", " + flags + ")";
119     }
120 
121     /**
122      * Flags associated with a dependency.
123      */
124     public static enum Flag {
125         /**
126          * Indicates that a dependency is immune to rewriting.
127          */
128         FIXED;
129 
130         public static EnumSet<Flag> emptySet() {
131             return EnumSet.noneOf(Flag.class);
132         }
133     }
134 }