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.solver;
21  
22  import com.google.common.collect.ImmutableList;
23  import org.grouplens.grapht.ResolutionException;
24  import org.grouplens.grapht.reflect.Desire;
25  
26  import java.util.Collection;
27  
28  /**
29   * Thrown when a BindingFunction would be required to return multiple binding
30   * results for a given desire and context. This is not thrown when multiple
31   * functions are each capable of producing a single result for a desire, since
32   * binding functions are given a priority.
33   * 
34   * @author <a href="http://grouplens.org">GroupLens Research</a>
35   */
36  public class MultipleBindingsException extends ResolutionException {
37      private static final long serialVersionUID = 1L;
38  
39      private final InjectionContext context;
40      private final DesireChain desires;
41      private final ImmutableList<?> bindings;
42      
43      public MultipleBindingsException(DesireChain desires, InjectionContext context, Collection<?> bindings) {
44          this.desires = desires;
45          this.context = context;
46          this.bindings = ImmutableList.copyOf(bindings);
47      }
48      
49      /**
50       * @return The context that produced the problematic bindings
51       */
52      public InjectionContext getContext() {
53          return context;
54      }
55      
56      /**
57       * @return The possible bindings, which depends on the BindingFunction that
58       * produced this exception
59       */
60      public Collection<?> getBindRules() {
61          return bindings;
62      }
63      
64      /**
65       * @return The desire that had too many possible binding within a
66       *         BindingFunction
67       */
68      public Desire getDesire() {
69          return desires.getCurrentDesire();
70      }
71      
72      @Override
73      public String getMessage() {
74          return new StringBuilder("Too many choices for desire: ")
75              .append(format(getDesire().getInjectionPoint()))
76              .append('\n')
77              .append(format(context, desires))
78              .toString();
79      }
80  }