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