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    *      https://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.commons.collections4.properties;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.Reader;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.InvalidPropertiesFormatException;
27  import java.util.Map;
28  import java.util.Map.Entry;
29  import java.util.Objects;
30  import java.util.Properties;
31  import java.util.Set;
32  import java.util.function.BiConsumer;
33  import java.util.function.BiFunction;
34  import java.util.function.Function;
35  
36  /**
37   * Creates and loads {@link Properties}.
38   *
39   * @see Properties
40   * @since 4.4
41   */
42  public class PropertiesFactory extends AbstractPropertiesFactory<Properties> {
43  
44      private static final class EmptyProperties extends Properties {
45  
46          private static final long serialVersionUID = 1L;
47  
48          @Override
49          public synchronized void clear() {
50              // Noop
51          }
52  
53          @Override
54          public synchronized Object compute(final Object key,
55              final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
56              Objects.requireNonNull(key, "key");
57              throw new UnsupportedOperationException();
58          }
59  
60          @Override
61          public synchronized Object computeIfAbsent(final Object key,
62              final Function<? super Object, ? extends Object> mappingFunction) {
63              Objects.requireNonNull(key, "key");
64              throw new UnsupportedOperationException();
65          }
66  
67          @Override
68          public synchronized Object computeIfPresent(final Object key,
69              final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
70              Objects.requireNonNull(key, "key");
71              throw new UnsupportedOperationException();
72          }
73  
74          @Override
75          public synchronized boolean contains(final Object value) {
76              return false;
77          }
78  
79          @Override
80          public synchronized boolean containsKey(final Object key) {
81              return false;
82          }
83  
84          @Override
85          public boolean containsValue(final Object value) {
86              return false;
87          }
88  
89          @Override
90          public synchronized Enumeration<Object> elements() {
91              return Collections.emptyEnumeration();
92          }
93  
94          @Override
95          public Set<Entry<Object, Object>> entrySet() {
96              return Collections.emptySet();
97          }
98  
99          @Override
100         public synchronized boolean equals(final Object o) {
101             return o instanceof Properties && ((Properties) o).isEmpty();
102         }
103 
104         @Override
105         public synchronized void forEach(final BiConsumer<? super Object, ? super Object> action) {
106             Objects.requireNonNull(action, "action");
107         }
108 
109         @Override
110         public synchronized Object get(final Object key) {
111             return null;
112         }
113 
114         @Override
115         public synchronized Object getOrDefault(final Object key, final Object defaultValue) {
116             return defaultValue;
117         }
118 
119         @Override
120         public String getProperty(final String key) {
121             return null;
122         }
123 
124         @Override
125         public String getProperty(final String key, final String defaultValue) {
126             return defaultValue;
127         }
128 
129         @Override
130         public synchronized int hashCode() {
131             return 0;
132         }
133 
134         @Override
135         public synchronized boolean isEmpty() {
136             return true;
137         }
138 
139         @Override
140         public synchronized Enumeration<Object> keys() {
141             return Collections.emptyEnumeration();
142         }
143 
144         @Override
145         public Set<Object> keySet() {
146             return Collections.emptySet();
147         }
148 
149         /**
150          * Throws {@link UnsupportedOperationException}.
151          * Caller should use try-with-resources statement.
152          */
153         @SuppressWarnings("resource")
154         @Override
155         public synchronized void load(final InputStream inputStream) throws IOException {
156             Objects.requireNonNull(inputStream, "inputStream");
157             throw new UnsupportedOperationException();
158         }
159 
160         /**
161          * Throws {@link UnsupportedOperationException}.
162          * Caller should use try-with-resources statement.
163          */
164         @SuppressWarnings("resource")
165         @Override
166         public synchronized void load(final Reader reader) throws IOException {
167             Objects.requireNonNull(reader, "reader");
168             throw new UnsupportedOperationException();
169         }
170 
171         /**
172          * Throws {@link UnsupportedOperationException}.
173          * Caller should use try-with-resources statement.
174          */
175         @SuppressWarnings("resource")
176         @Override
177         public synchronized void loadFromXML(final InputStream inputStream)
178             throws IOException, InvalidPropertiesFormatException {
179             Objects.requireNonNull(inputStream, "inputStream");
180             throw new UnsupportedOperationException();
181         }
182 
183         @Override
184         public synchronized Object merge(final Object key, final Object value,
185             final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
186             Objects.requireNonNull(key, "key");
187             Objects.requireNonNull(value, "value");
188             throw new UnsupportedOperationException();
189         }
190 
191         @Override
192         public Enumeration<?> propertyNames() {
193             return Collections.emptyEnumeration();
194         }
195 
196         @Override
197         public synchronized Object put(final Object key, final Object value) {
198             Objects.requireNonNull(key, "key");
199             Objects.requireNonNull(value, "value");
200             throw new UnsupportedOperationException();
201         }
202 
203         @Override
204         public synchronized void putAll(final Map<? extends Object, ? extends Object> map) {
205             Objects.requireNonNull(map, "map");
206             throw new UnsupportedOperationException();
207         }
208 
209         @Override
210         public synchronized Object putIfAbsent(final Object key, final Object value) {
211             Objects.requireNonNull(key, "key");
212             Objects.requireNonNull(value, "value");
213             throw new UnsupportedOperationException();
214         }
215 
216         @Override
217         protected void rehash() {
218             // Noop
219         }
220 
221         @Override
222         public synchronized Object remove(final Object key) {
223             Objects.requireNonNull(key, "key");
224             throw new UnsupportedOperationException();
225         }
226 
227         @Override
228         public synchronized boolean remove(final Object key, final Object value) {
229             Objects.requireNonNull(key, "key");
230             Objects.requireNonNull(value, "value");
231             throw new UnsupportedOperationException();
232         }
233 
234         @Override
235         public synchronized Object replace(final Object key, final Object value) {
236             Objects.requireNonNull(key, "key");
237             Objects.requireNonNull(value, "value");
238             throw new UnsupportedOperationException();
239         }
240 
241         @Override
242         public synchronized boolean replace(final Object key, final Object oldValue, final Object newValue) {
243             Objects.requireNonNull(key, "key");
244             Objects.requireNonNull(oldValue, "oldValue");
245             Objects.requireNonNull(newValue, "newValue");
246             throw new UnsupportedOperationException();
247         }
248 
249         @Override
250         public synchronized void replaceAll(
251             final BiFunction<? super Object, ? super Object, ? extends Object> function) {
252             Objects.requireNonNull(function, "function");
253             throw new UnsupportedOperationException();
254         }
255 
256         @Override
257         public synchronized Object setProperty(final String key, final String value) {
258             Objects.requireNonNull(key, "key");
259             Objects.requireNonNull(value, "value");
260             throw new UnsupportedOperationException();
261         }
262 
263         @Override
264         public synchronized int size() {
265             return 0;
266         }
267 
268         @Override
269         public Set<String> stringPropertyNames() {
270             return Collections.emptySet();
271         }
272 
273         @Override
274         public Collection<Object> values() {
275             return Collections.emptyList();
276         }
277 
278     }
279 
280     /**
281      * The empty map (immutable). This map is serializable.
282      *
283      * @since 4.5.0-M1
284      */
285     public static final Properties EMPTY_PROPERTIES = new EmptyProperties();
286 
287     /**
288      * The singleton instance.
289      */
290     public static final PropertiesFactory INSTANCE = new PropertiesFactory();
291 
292     /**
293      * Constructs an instance.
294      */
295     private PropertiesFactory() {
296         // There is only one instance.
297     }
298 
299     /**
300      * Subclasses override to provide customized properties instances.
301      *
302      * @return A new Properties instance.
303      */
304     @Override
305     protected Properties createProperties() {
306         return new Properties();
307     }
308 
309 }