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.Ordering;
23  
24  import javax.annotation.Nullable;
25  import java.util.Comparator;
26  
27  /**
28   * A single element in a context match.  Used in {@link ContextMatch}.
29   *
30   * @since 0.7
31   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
32   */
33  public interface MatchElement {
34      /**
35       * Get the priority of this element matcher.
36       *
37       * @return The element matcher's priority.
38       */
39      ContextElements.MatchPriority getPriority();
40  
41      /**
42       * Get the type distance of this match.
43       *
44       * @return The type distance in this match, or empty if the type distance is irrelevant
45       *         for this type of matcher.
46       */
47      @Nullable
48      Integer getTypeDistance();
49  
50      /**
51       * Orderings for the match order.
52       */
53      public static enum Order implements Comparator<MatchElement> {
54          /**
55           * Priority-only ordering, used for first-pass comparison of context matchers.
56           */
57          PRIORITY_ONLY {
58              @Override
59              public int compare(MatchElement e1, MatchElement e2) {
60                  return e1.getPriority().compareTo(e2.getPriority());
61              }
62          },
63          /**
64           * Full ordering of match elements, including type distance.  Closer compares lower (higher
65           * priority).
66           */
67          PRIORITY_AND_DISTANCE {
68              @Override
69              public int compare(MatchElement e1, MatchElement e2) {
70                  int cmp = e1.getPriority().compareTo(e2.getPriority());
71                  if (cmp == 0) {
72                      Ordering<Integer> distOrder = Ordering.<Integer>natural().nullsLast();
73                      cmp = distOrder.compare(e1.getTypeDistance(), e2.getTypeDistance());
74                  }
75                  return cmp;
76              }
77          }
78      }
79  }