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      private long lastModified = -1;
62  
63      private DefinitionProviderBuilder() {
64      }
65  
66      public DefinitionProviderBuilder<T> metadata(DefinitionMetadataBuilder metadata) {
67          this.metadata = metadata;
68          return this;
69      }
70  
71      public DefinitionProviderBuilder<T> rawView(DefinitionRawView rawView) {
72          this.rawView = rawView;
73          return this;
74      }
75  
76      public DefinitionProviderBuilder<T> definition(T definition) {
77          this.definition = definition;
78          return this;
79      }
80  
81      public DefinitionProviderBuilder<T> addErrorMessage(String errorMessage) {
82          // Let's be lazy here ...
83          if (this.errorMessages == null) {
84              this.errorMessages = new ArrayList<>();
85          }
86          this.errorMessages.add(errorMessage);
87          return this;
88      }
89  
90      public DefinitionProviderBuilder<T> withLastModifed(long lastModified) {
91          this.lastModified = lastModified;
92          return this;
93      }
94  
95      public DefinitionProvider<T> build() {
96          return new DefinitionProviderImpl<>(
97                  this.metadata.build(),
98                  this.rawView,
99                  this.definition,
100                 this.errorMessages != null ? this.errorMessages : NO_ERRORS,
101                 this.lastModified > 0 ? lastModified : System.currentTimeMillis()
102         );
103     }
104 
105     /**
106      * Implementation of DefinitionProviderBuilder.
107      */
108     static class DefinitionProviderImpl<T> implements DefinitionProvider<T> {
109         private final DefinitionMetadata metadata;
110         private final DefinitionRawView rawView;
111         private final T definition;
112         private final List<String> errorMessages;
113         private final boolean valid;
114         private final long lastModified;
115 
116         protected DefinitionProviderImpl(DefinitionMetadata metadata, DefinitionRawView rawView, T definition, List<String> errorMessages) {
117             this(metadata, rawView, definition, errorMessages, System.currentTimeMillis());
118         }
119 
120         protected DefinitionProviderImpl(DefinitionMetadata metadata, DefinitionRawView rawView, T definition, List<String> errorMessages, long lastModified) {
121             this.metadata = metadata;
122             this.rawView = rawView;
123             this.definition = definition;
124             this.errorMessages = errorMessages;
125             this.lastModified = lastModified;
126             this.valid = (metadata != null && rawView != null && definition != null && errorMessages.isEmpty());
127         }
128 
129         @Override
130         public List<DefinitionDecorator<T>> getDecorators() {
131             return Collections.emptyList();
132         }
133 
134         @Override
135         public DefinitionMetadata getMetadata() {
136             return metadata;
137         }
138 
139         @Override
140         public DefinitionRawView getRaw() {
141             return rawView;
142         }
143 
144         @Override
145         public T get() {
146             if (!isValid()) {
147                 throw new Registry.InvalidDefinitionException(metadata.getReferenceId());
148             }
149             return definition;
150         }
151 
152         @Override
153         public boolean isValid() {
154             return valid;
155         }
156 
157         @Override
158         public List<String> getErrorMessages() {
159             return errorMessages;
160         }
161 
162         @Override
163         public long getLastModified() {
164             return lastModified;
165         }
166     }
167 }