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.context;
21  
22  import com.google.common.collect.ImmutableList;
23  import com.google.common.collect.Ordering;
24  
25  import java.util.List;
26  
27  /**
28   * Interface for context matches. A context match is the result of successfully
29   * matching a {@link ContextMatcher}.
30   *
31   * @author <a href="http://grouplens.org">GroupLens Research</a>
32   */
33  public class ContextMatch implements Comparable<ContextMatch> {
34      private final ImmutableList<MatchElement> matchElements;
35  
36      private ContextMatch(List<MatchElement> matches) {
37          matchElements = ImmutableList.copyOf(matches);
38      }
39  
40      /**
41       * Create a new context match.
42       * @param matches The list of match elements.
43       * @return The context match.
44       */
45      static ContextMatch create(List<MatchElement> matches) {
46          return new ContextMatch(matches);
47      }
48  
49      @Override
50      public int compareTo(ContextMatch o) {
51          return Ordering.from(MatchElement.Order.PRIORITY_ONLY)
52                         .lexicographical()
53                         .compound(Ordering.from(MatchElement.Order.PRIORITY_AND_DISTANCE)
54                                           .lexicographical())
55                         .compare(matchElements.reverse(), o.matchElements.reverse());
56      }
57  
58      @Override
59      public boolean equals(Object o) {
60          if (o == this) {
61              return true;
62          } else if (o instanceof ContextMatch) {
63              ContextMatch om = (ContextMatch) o;
64              return matchElements.equals(om.matchElements);
65          } else {
66              return false;
67          }
68      }
69  
70      @Override
71      public int hashCode() {
72          return matchElements.hashCode();
73      }
74  
75      @Override
76      public String toString() {
77          return "ContextMatch" + matchElements.toString();
78      }
79  }