View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 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 info.magnolia.config.source.ConfigurationSourceType;
37  
38  import java.util.Collection;
39  import java.util.Optional;
40  import java.util.regex.Pattern;
41  
42  /**
43   * DefinitionQuery aggregates metadata search criteria allowing for specifying
44   * it in a builder-pattern fashion.
45   *
46   * @param <T> type of the expected query result
47   */
48  public abstract class DefinitionQuery<T> {
49      private String module;
50      private String name;
51      private DefinitionType type;
52      private ConfigurationSourceType configurationSourceType;
53      private Pattern locationPattern;
54  
55      protected DefinitionQuery() {
56      }
57  
58      /**
59       * Create a new query instance based on another one.
60       * All properties are initially adopted, but can still be overridden.
61       * @param query The query to wrap/fork
62       */
63      protected DefinitionQuery"../../../../info/magnolia/config/registry/DefinitionQuery.html#DefinitionQuery">DefinitionQuery(DefinitionQuery query) {
64          this.module = query.module;
65          this.name = query.name;
66          this.type = query.type;
67          this.configurationSourceType = query.configurationSourceType;
68          this.locationPattern = query.locationPattern;
69      }
70  
71      /**
72       * Start building a DefinitionQuery for a Registry.
73       */
74      public static <T> DefinitionQuery<T> build(Registry<T> registry) {
75          return new DefinitionQueryImpl<>(registry);
76      }
77  
78      /**
79       * Start building a DefinitionQuery for a Registry, copying properties of an existing query.
80       * @param baseQuery A {@link DefinitionQuery} to base the new one upon
81       */
82      public static <T> DefinitionQuery<T> build(Registry<T> registry, DefinitionQuery<T> baseQuery) {
83          return new DefinitionQueryImpl<>(registry, baseQuery);
84      }
85  
86      public DefinitionQuery<T> from(String moduleName) {
87          this.module = moduleName;
88          return this;
89      }
90  
91      public DefinitionQuery<T> named(String defName) {
92          this.name = defName;
93          return this;
94      }
95  
96      public DefinitionQuery<T> ofType(DefinitionType type) {
97          this.type = type;
98          return this;
99      }
100 
101     public DefinitionQuery<T> ofConfigurationSourceType(ConfigurationSourceType configurationSourceType) {
102         this.configurationSourceType = configurationSourceType;
103         return this;
104     }
105 
106     public DefinitionQuery<T> at(String locationRegexPattern) {
107         this.locationPattern = Pattern.compile(locationRegexPattern);
108         return this;
109     }
110 
111     public DefinitionQuery<T> at(Pattern locationRegexPattern) {
112         this.locationPattern = locationRegexPattern;
113         return this;
114     }
115 
116     public String getModule() {
117         return module;
118     }
119 
120     public String getName() {
121         return name;
122     }
123 
124     public DefinitionType getType() {
125         return type;
126     }
127 
128     public ConfigurationSourceType getConfigurationSourceType() {
129         return configurationSourceType;
130     }
131 
132     public Pattern getLocationPattern() {
133         return locationPattern;
134     }
135 
136     /**
137      * Returns a single provider matching the criteria or throws an exception if not unique.
138      *
139      * @throws Registry.NoSuchDefinitionException (which is a RuntimeException) if the query doesn't match any definition.
140      */
141     public DefinitionProvider<T> findSingle() {
142         return findAny().orElseThrow(() -> new Registry.NoSuchDefinitionException("No match found for " + this));
143     }
144 
145     /**
146      * Optionally returns a single provider matching the criteria.
147      */
148     abstract Optional<DefinitionProvider<T>> findAny();
149 
150     /**
151      * Returns all providers matching the given criteria or an empty collection if there are no matches.
152      */
153     public abstract Collection<DefinitionProvider<T>> findMultiple();
154 
155     @Override
156     public String toString() {
157         final StringBuilder s = new StringBuilder();
158         s.append("DefinitionQuery(");
159         if (this.getModule() != null) {
160             s.append("[module: ").append(this.getModule()).append("]");
161         }
162         if (this.getName() != null) {
163             s.append("[name: ").append(this.getName()).append("]");
164         }
165         if (this.getType() != null) {
166             s.append("[type: ").append(this.getType()).append("]");
167         }
168         if (this.getConfigurationSourceType() != null) {
169             s.append("[configurationSourceType: ").append(this.getConfigurationSourceType()).append("]");
170         }
171         if (this.getLocationPattern() != null) {
172             s.append("[locationPattern: ").append(this.getLocationPattern()).append("]");
173         }
174         s.append(")");
175         return s.toString();
176     }
177 }