View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.config.registry;
35  
36  import java.util.Collection;
37  import java.util.regex.Pattern;
38  
39  /**
40   * DefinitionQuery aggregates metadata search criteria allowing for specifying
41   * it in a builder-pattern fashion.
42   *
43   * @param <T> type of the expected query result
44   */
45  public abstract class DefinitionQuery<T> {
46      private String module;
47      private String name;
48      private DefinitionType type;
49      private Pattern locationPattern;
50  
51      public DefinitionQuery<T> from(String moduleName) {
52          this.module = moduleName;
53          return this;
54      }
55  
56      public DefinitionQuery<T> named(String defName) {
57          this.name = defName;
58          return this;
59      }
60  
61      public DefinitionQuery<T> ofType(DefinitionType type) {
62          this.type = type;
63          return this;
64      }
65  
66      public DefinitionQuery<T> at(String locationRegexPattern) {
67          this.locationPattern = Pattern.compile(locationRegexPattern);
68          return this;
69      }
70  
71      public DefinitionQuery<T> at(Pattern locationRegexPattern) {
72          this.locationPattern = locationRegexPattern;
73          return this;
74      }
75  
76      public String getModule() {
77          return module;
78      }
79  
80      public String getName() {
81          return name;
82      }
83  
84      public DefinitionType getType() {
85          return type;
86      }
87  
88      public Pattern getLocationPattern() {
89          return locationPattern;
90      }
91  
92      /**
93       * Returns a single provider matching the criteria or throws an exception if not unique.
94       *
95       * @throws Registry.NoSuchDefinitionException (which is a RuntimeException) if the query doesn't match any definition.
96       */
97      public DefinitionProvider<T> findSingle() {
98          final Collection<DefinitionProvider<T>> multiple = findMultiple();
99          if (multiple.size() < 1) {
100             throw new Registry.NoSuchDefinitionException("No match found for " + this);
101         } else if (multiple.size() > 1) {
102             throw new IllegalStateException("More than one match found for " + this);
103         } else {
104             return multiple.iterator().next();
105         }
106     }
107 
108     /**
109      * Returns all providers matching the given criteria or an empty collection if there are no matches.
110      */
111     public abstract Collection<DefinitionProvider<T>> findMultiple();
112 
113     @Override
114     public String toString() {
115         final StringBuilder s = new StringBuilder();
116         s.append("DefinitionQuery(");
117         if (this.getModule() != null) {
118             s.append("[module: ").append(this.getModule()).append("]");
119         }
120         if (this.getName() != null) {
121             s.append("[name: ").append(this.getName()).append("]");
122         }
123         if (this.getType() != null) {
124             s.append("[type: ").append(this.getType()).append("]");
125         }
126         if (this.getLocationPattern() != null) {
127             s.append("[locationPattern: ").append(this.getLocationPattern()).append("]");
128         }
129         s.append(")");
130         return s.toString();
131     }
132 
133 }