View Javadoc
1   /**
2    * This file Copyright (c) 2015-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 java.util.Collection;
37  import java.util.Collections;
38  import java.util.List;
39  
40  import lombok.AccessLevel;
41  import lombok.EqualsAndHashCode;
42  import lombok.Getter;
43  import lombok.RequiredArgsConstructor;
44  import lombok.ToString;
45  
46  /**
47   * This is a read-only view on the "raw" data of a configured definition. Implementations may wrap simple Maps or anything else.
48   */
49  public interface DefinitionRawView {
50  
51      /**
52       * Order is entirely dependent on the source. Should ideally reflect reality, but we don't necessarily have control over it.
53       */
54      List<Property> properties();
55  
56      /**
57       * Kinds of raw view types.
58       * Entries could be
59       * - simple (i.e just a string, or do we need to be more granular already)
60       * - complex
61       * - collection of either
62       * - map of <either,either> (although typically we never have maps with complex keys)
63       */
64      enum Kind {simple, collection, subBean /*or...*/}
65  
66  
67      /**
68       * Property definition for the raw view.
69       *
70       * Not using the @Value annotation because we want a private constructor and SEVERAL static factory methods
71       */
72      @Getter
73      @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
74      @ToString
75      @EqualsAndHashCode
76      class Property {
77          private final DefinitionRawView.Kind kind;
78          private final String name;
79          private final Object simpleRawValue;
80          private final String simpleStringValue;
81          private final Collection<Property> collection;
82          private final DefinitionRawView subRawView;
83  
84          public String getSimpleValue() {
85              return this.simpleStringValue;
86          }
87  
88          public static Property simple(String name, Object value) {
89              return new Property(Kind.simple, name, value, String.valueOf(value), null, null);
90          }
91  
92          public static Property collection(String name, Collection<Property> collection) {
93              return new Property(Kind.collection, name, null, null, collection, null);
94          }
95  
96          public static Property subBean(String name, DefinitionRawView subRawView) {
97              return new Property(Kind.subBean, name, null, null, null, subRawView);
98          }
99      }
100 
101     /**
102      * A null pattern implementation of {@link info.magnolia.config.registry.DefinitionRawView}.
103      */
104     DefinitionRawView EMPTY = new DefinitionRawView() {
105         @Override
106         public List<Property> properties() {
107             return Collections.emptyList();
108         }
109     };
110 }