View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.configuration;
19  
20  import java.util.AbstractSet;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Optional;
27  import java.util.Set;
28  
29  /**
30   * This class represents an <i>immutable</i> {@link Set} of {@link Setting} objects,
31   * with the additional property that no two settings having the same {@link Setting#getIdentifier() identifier}
32   * can be simultaneously present in a {@code Settings} object.
33   *
34   * @author Hans Brende (hansbrende@apache.org)
35   */
36  public final class Settings extends AbstractSet<Setting<?>> {
37  
38      private static final Settingss.html#Settings">Settings EMPTY_SETTINGS = new Settings(Collections.emptyMap());
39  
40      private final Map<String, Setting<?>> values;
41  
42      private Settings(Map<String, Setting<?>> values) {
43          this.values = values;
44      }
45  
46      /**
47       * @param identifier the identifier of the setting to find
48       * @return the setting with the identifier supplied, if present
49       */
50      public Optional<Setting<?>> find(String identifier) {
51          return Optional.ofNullable(values.get(identifier));
52      }
53  
54      /**
55       * Returns the setting with the same setting key as the supplied setting, if present.
56       * <br><br>
57       * This method is semantically equivalent to:
58       * <br><br>
59       * <pre>
60       * {@code find(setting.getIdentifier()).flatMap(s -> s.as(setting))}
61       * </pre>
62       */
63      public <S extends Setting<?>> Optional<S> find(S setting) {
64          Setting<?> found = values.get(setting.getIdentifier());
65          return found == null ? Optional.empty() : found.as(setting);
66      }
67  
68      /**
69       * Returns the value set for {@code defaultSetting}'s key, if present.
70       * Otherwise, returns {@code defaultSetting}'s value.
71       * <br><br>
72       * This method is semantically equivalent to:
73       * <br><br>
74       * <pre>
75       * {@code find(defaultSetting).orElse(defaultSetting).getValue()}
76       * </pre>
77       */
78      public <E> E get(Setting<E> defaultSetting) {
79          return find(defaultSetting).orElse(defaultSetting).getValue();
80      }
81  
82  
83      ///////////////////////////////////////
84      // AbstractSet overrides
85      ///////////////////////////////////////
86  
87      @Override
88      public boolean contains(Object o) {
89          if (!(o instanceof Setting<?>)) {
90              return false;
91          }
92          return o.equals(values.get(((Setting<?>) o).getIdentifier()));
93      }
94  
95      @Override
96      public int size() {
97          return values.size();
98      }
99  
100     @Override
101     public Iterator<Setting<?>> iterator() {
102         return values.values().iterator();
103     }
104 
105     ///////////////////////////////////////
106     // public constructors
107     ///////////////////////////////////////
108 
109     /**
110      * Returns an empty {@link Settings} object.
111      */
112     public static Settings of() {
113         return EMPTY_SETTINGS;
114     }
115 
116     /**
117      * Returns a singleton {@link Settings} object, containing only the supplied setting.
118      */
119     public static Settings of(Setting<?> s) {
120         return new Settings(Collections.singletonMap(s.getIdentifier(), s));
121     }
122 
123     /**
124      * Returns a {@link Settings} object containing the supplied settings.
125      * For any two settings having the same key, the first will be overwritten by the second.
126      * @throws IllegalArgumentException if any two settings have the same identifier
127      */
128     public static Settings of(Setting<?>... settings) {
129         Map<String, Setting<?>> map = mapForSize(settings.length);
130         for (Setting<?> s : settings) put(map, s);
131         return ofModifiable(map);
132     }
133 
134     /**
135      * Returns a {@link Settings} object containing the supplied settings.
136      * @throws IllegalArgumentException if any two settings have the same identifier
137      */
138     public static Settings of(Collection<? extends Setting<?>> c) {
139         if (c instanceof Settings) {
140             return (Settings)c;
141         }
142         int size = c.size();
143         if (size == 0) {
144             return EMPTY_SETTINGS;
145         }
146         Map<String, Setting<?>> map = mapForSize(size);
147         for (Setting<?> s : c) put(map, s);
148         return ofModifiable(map);
149     }
150 
151     ///////////////////////////////////////
152     // Private static helpers
153     ///////////////////////////////////////
154 
155     private static Settings ofModifiable(Map<String, Setting<?>> map) {
156         return new Settings(Collections.unmodifiableMap(map));
157     }
158 
159     private static void put(Map<String, Setting<?>> map, Setting<?> setting) {
160         Setting<?> existing = map.put(setting.getIdentifier(), setting);
161         if (existing != null) {
162             throw new IllegalArgumentException(setting.getIdentifier() + " is already defined");
163         }
164     }
165 
166     private static final float loadFactor = 0.75f;
167     private static Map<String, Setting<?>> mapForSize(int size) {
168         return new HashMap<>((int)(size / loadFactor) + 1, loadFactor);
169     }
170 
171 }