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 org.apache.commons.lang3.tuple.Pair;
23  import org.grouplens.grapht.reflect.InjectionPoint;
24  import org.grouplens.grapht.reflect.QualifierMatcher;
25  import org.grouplens.grapht.reflect.Satisfaction;
26  
27  /**
28   * Utilities for context matching.
29   *
30   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
31   */
32  public final class ContextElements {
33      private ContextElements() {}
34  
35      /**
36       * Match priority constants.
37       */
38      public static enum MatchPriority {
39          TYPE,
40          INVERTED,
41          ANY
42      }
43  
44      public static ContextElementMatcher matchAny() {
45          return AnyMatcher.INSTANCE;
46      }
47  
48      public static ContextElementMatcher matchType(Class<?> type) {
49          return new TypeElementMatcher(type);
50      }
51  
52      public static ContextElementMatcher matchType(Class<?> type,  QualifierMatcher qual) {
53          return new TypeElementMatcher(type, qual);
54      }
55  
56      public static ContextElementMatcher invertMatch(ContextElementMatcher matcher) {
57          return new InvertedMatcher(matcher);
58      }
59  
60      private static enum AnyMatcher implements ContextElementMatcher {
61          INSTANCE;
62  
63          @Override
64          public MatchElement apply(Pair<Satisfaction, InjectionPoint> n) {
65              return MatchElementImpl.ANY;
66          }
67  
68  
69          @Override
70          public String toString() {
71              return "{Object}";
72          }
73      }
74  
75      private static class InvertedMatcher implements ContextElementMatcher {
76          private static final long serialVersionUID = 1L;
77  
78          private final ContextElementMatcher delegate;
79  
80          public InvertedMatcher(ContextElementMatcher base) {
81              delegate = base;
82          }
83  
84          @Override
85          public MatchElement apply(Pair<Satisfaction, InjectionPoint> elem) {
86              MatchElement result = delegate.apply(elem);
87              if (result == null) {
88                  return MatchElementImpl.INVERTED;
89              } else {
90                  return null;
91              }
92          }
93  
94          @Override
95          public String toString() {
96              return "!" + delegate.toString();
97          }
98      }
99  
100     private static enum MatchElementImpl implements MatchElement {
101         ANY(MatchPriority.ANY),
102         INVERTED(MatchPriority.INVERTED);
103 
104         private final MatchPriority priority;
105 
106         private MatchElementImpl(MatchPriority p) {
107             priority = p;
108         }
109 
110         @Override
111         public MatchPriority getPriority() {
112             return priority;
113         }
114 
115         @Override
116         public Integer getTypeDistance() {
117             return null;
118         }
119     }
120 }