View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.registry.decoration.DefinitionDecorator;
37  
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.List;
41  
42  /**
43   * A builder that allows to construct a {@link info.magnolia.config.registry.DefinitionProvider}s progressively,
44   * without nesting try/catch blocks.
45   *
46   * @see #newBuilder()
47   *
48   * @param <T> the type the built provider will provide
49   */
50  public class DefinitionProviderBuilder<T> {
51      public static <T> DefinitionProviderBuilder<T> newBuilder() {
52          return new DefinitionProviderBuilder<>();
53      }
54  
55      private static final List<String> NO_ERRORS = Collections.emptyList();
56  
57      private DefinitionMetadataBuilder metadata;
58      private DefinitionRawView rawView;
59      private T definition;
60      private List<String> errorMessages = null;
61  
62      private DefinitionProviderBuilder() {
63      }
64  
65      public DefinitionProviderBuilder<T> metadata(DefinitionMetadataBuilder metadata) {
66          this.metadata = metadata;
67          return this;
68      }
69  
70      public DefinitionProviderBuilder<T> rawView(DefinitionRawView rawView) {
71          this.rawView = rawView;
72          return this;
73      }
74  
75      public DefinitionProviderBuilder<T> definition(T definition) {
76          this.definition = definition;
77          return this;
78      }
79  
80      public DefinitionProviderBuilder<T> addErrorMessage(String errorMessage) {
81          // Let's be lazy here ...
82          if (this.errorMessages == null) {
83              this.errorMessages = new ArrayList<>();
84          }
85          this.errorMessages.add(errorMessage);
86          return this;
87      }
88  
89      public DefinitionProvider<T> build() {
90          return new DefinitionProviderImpl<>(
91                  this.metadata.build(),
92                  this.rawView,
93                  this.definition,
94                  this.errorMessages != null ? this.errorMessages : NO_ERRORS
95          );
96      }
97  
98      /**
99       * Implementation of DefinitionProviderBuilder.
100      */
101     static class DefinitionProviderImpl<T> implements DefinitionProvider<T> {
102         private final DefinitionMetadata metadata;
103         private final DefinitionRawView rawView;
104         private final T definition;
105         private final List<String> errorMessages;
106         private final boolean valid;
107 
108         protected DefinitionProviderImpl(DefinitionMetadata metadata, DefinitionRawView rawView, T definition, List<String> errorMessages) {
109             this.metadata = metadata;
110             this.rawView = rawView;
111             this.definition = definition;
112             this.errorMessages = errorMessages;
113 
114             this.valid = (metadata != null && rawView != null && definition != null && errorMessages.isEmpty());
115         }
116 
117         @Override
118         public List<DefinitionDecorator<T>> getDecorators() {
119             return Collections.emptyList();
120         }
121 
122         @Override
123         public DefinitionMetadata getMetadata() {
124             return metadata;
125         }
126 
127         @Override
128         public DefinitionRawView getRaw() {
129             return rawView;
130         }
131 
132         @Override
133         public T get() {
134             if (!isValid()) {
135                 throw new Registry.InvalidDefinitionException(metadata.getReferenceId());
136             }
137             return definition;
138         }
139 
140         @Override
141         public boolean isValid() {
142             return valid;
143         }
144 
145         @Override
146         public List<String> getErrorMessages() {
147             return errorMessages;
148         }
149 
150     }
151 }