PropertiesFactory.java

  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. package org.apache.commons.collections4.properties;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.io.Reader;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Enumeration;
  25. import java.util.InvalidPropertiesFormatException;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.Objects;
  29. import java.util.Properties;
  30. import java.util.Set;
  31. import java.util.function.BiConsumer;
  32. import java.util.function.BiFunction;
  33. import java.util.function.Function;

  34. /**
  35.  * Creates and loads {@link Properties}.
  36.  *
  37.  * @see Properties
  38.  * @since 4.4
  39.  */
  40. public class PropertiesFactory extends AbstractPropertiesFactory<Properties> {

  41.     private static final class EmptyProperties extends Properties {

  42.         private static final long serialVersionUID = 1L;

  43.         @Override
  44.         public synchronized void clear() {
  45.             // Noop
  46.         }

  47.         @Override
  48.         public synchronized Object compute(final Object key,
  49.             final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
  50.             Objects.requireNonNull(key);
  51.             throw new UnsupportedOperationException();
  52.         }

  53.         @Override
  54.         public synchronized Object computeIfAbsent(final Object key,
  55.             final Function<? super Object, ? extends Object> mappingFunction) {
  56.             Objects.requireNonNull(key);
  57.             throw new UnsupportedOperationException();
  58.         }

  59.         @Override
  60.         public synchronized Object computeIfPresent(final Object key,
  61.             final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
  62.             Objects.requireNonNull(key);
  63.             throw new UnsupportedOperationException();
  64.         }

  65.         @Override
  66.         public synchronized boolean contains(final Object value) {
  67.             return false;
  68.         }

  69.         @Override
  70.         public synchronized boolean containsKey(final Object key) {
  71.             return false;
  72.         }

  73.         @Override
  74.         public boolean containsValue(final Object value) {
  75.             return false;
  76.         }

  77.         @Override
  78.         public synchronized Enumeration<Object> elements() {
  79.             return Collections.emptyEnumeration();
  80.         }

  81.         @Override
  82.         public Set<Entry<Object, Object>> entrySet() {
  83.             return Collections.emptySet();
  84.         }

  85.         @Override
  86.         public synchronized boolean equals(final Object o) {
  87.             return o instanceof Properties && ((Properties) o).isEmpty();
  88.         }

  89.         @Override
  90.         public synchronized void forEach(final BiConsumer<? super Object, ? super Object> action) {
  91.             Objects.requireNonNull(action);
  92.         }

  93.         @Override
  94.         public synchronized Object get(final Object key) {
  95.             return null;
  96.         }

  97.         @Override
  98.         public synchronized Object getOrDefault(final Object key, final Object defaultValue) {
  99.             return defaultValue;
  100.         }

  101.         @Override
  102.         public String getProperty(final String key) {
  103.             return null;
  104.         }

  105.         @Override
  106.         public String getProperty(final String key, final String defaultValue) {
  107.             return defaultValue;
  108.         }

  109.         @Override
  110.         public synchronized int hashCode() {
  111.             return 0;
  112.         }

  113.         @Override
  114.         public synchronized boolean isEmpty() {
  115.             return true;
  116.         }

  117.         @Override
  118.         public synchronized Enumeration<Object> keys() {
  119.             return Collections.emptyEnumeration();
  120.         }

  121.         @Override
  122.         public Set<Object> keySet() {
  123.             return Collections.emptySet();
  124.         }

  125.         /**
  126.          * Throws {@link UnsupportedOperationException}.
  127.          * Caller should use try-with-resources statement.
  128.          */
  129.         @SuppressWarnings("resource")
  130.         @Override
  131.         public synchronized void load(final InputStream inStream) throws IOException {
  132.             Objects.requireNonNull(inStream);
  133.             throw new UnsupportedOperationException();
  134.         }

  135.         /**
  136.          * Throws {@link UnsupportedOperationException}.
  137.          * Caller should use try-with-resources statement.
  138.          */
  139.         @SuppressWarnings("resource")
  140.         @Override
  141.         public synchronized void load(final Reader reader) throws IOException {
  142.             Objects.requireNonNull(reader);
  143.             throw new UnsupportedOperationException();
  144.         }

  145.         /**
  146.          * Throws {@link UnsupportedOperationException}.
  147.          * Caller should use try-with-resources statement.
  148.          */
  149.         @SuppressWarnings("resource")
  150.         @Override
  151.         public synchronized void loadFromXML(final InputStream in)
  152.             throws IOException, InvalidPropertiesFormatException {
  153.             Objects.requireNonNull(in);
  154.             throw new UnsupportedOperationException();
  155.         }

  156.         @Override
  157.         public synchronized Object merge(final Object key, final Object value,
  158.             final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
  159.             Objects.requireNonNull(key);
  160.             Objects.requireNonNull(value);
  161.             throw new UnsupportedOperationException();
  162.         }

  163.         @Override
  164.         public Enumeration<?> propertyNames() {
  165.             return Collections.emptyEnumeration();
  166.         }

  167.         @Override
  168.         public synchronized Object put(final Object key, final Object value) {
  169.             Objects.requireNonNull(key);
  170.             Objects.requireNonNull(value);
  171.             throw new UnsupportedOperationException();
  172.         }

  173.         @Override
  174.         public synchronized void putAll(final Map<? extends Object, ? extends Object> t) {
  175.             Objects.requireNonNull(t);
  176.             throw new UnsupportedOperationException();
  177.         }

  178.         @Override
  179.         public synchronized Object putIfAbsent(final Object key, final Object value) {
  180.             Objects.requireNonNull(key);
  181.             Objects.requireNonNull(value);
  182.             throw new UnsupportedOperationException();
  183.         }

  184.         @Override
  185.         protected void rehash() {
  186.             // Noop
  187.         }

  188.         @Override
  189.         public synchronized Object remove(final Object key) {
  190.             Objects.requireNonNull(key);
  191.             throw new UnsupportedOperationException();
  192.         }

  193.         @Override
  194.         public synchronized boolean remove(final Object key, final Object value) {
  195.             Objects.requireNonNull(key);
  196.             Objects.requireNonNull(value);
  197.             throw new UnsupportedOperationException();
  198.         }

  199.         @Override
  200.         public synchronized Object replace(final Object key, final Object value) {
  201.             Objects.requireNonNull(key);
  202.             Objects.requireNonNull(value);
  203.             throw new UnsupportedOperationException();
  204.         }

  205.         @Override
  206.         public synchronized boolean replace(final Object key, final Object oldValue, final Object newValue) {
  207.             Objects.requireNonNull(key);
  208.             Objects.requireNonNull(oldValue);
  209.             Objects.requireNonNull(newValue);
  210.             throw new UnsupportedOperationException();
  211.         }

  212.         @Override
  213.         public synchronized void replaceAll(
  214.             final BiFunction<? super Object, ? super Object, ? extends Object> function) {
  215.             Objects.requireNonNull(function);
  216.             throw new UnsupportedOperationException();
  217.         }

  218.         @SuppressWarnings("deprecation")
  219.         @Override
  220.         public void save(final OutputStream out, final String comments) {
  221.             // Implement as super
  222.             super.save(out, comments);
  223.         }

  224.         @Override
  225.         public synchronized Object setProperty(final String key, final String value) {
  226.             Objects.requireNonNull(key);
  227.             Objects.requireNonNull(value);
  228.             throw new UnsupportedOperationException();
  229.         }

  230.         @Override
  231.         public synchronized int size() {
  232.             return 0;
  233.         }

  234.         @Override
  235.         public Set<String> stringPropertyNames() {
  236.             return Collections.emptySet();
  237.         }

  238.         @Override
  239.         public synchronized String toString() {
  240.             // Implement as super
  241.             return super.toString();
  242.         }

  243.         @Override
  244.         public Collection<Object> values() {
  245.             return Collections.emptyList();
  246.         }

  247.     }

  248.     /**
  249.      * The empty map (immutable). This map is serializable.
  250.      *
  251.      * @since 4.5.0-M1
  252.      */
  253.     public static final Properties EMPTY_PROPERTIES = new EmptyProperties();

  254.     /**
  255.      * The singleton instance.
  256.      */
  257.     public static final PropertiesFactory INSTANCE = new PropertiesFactory();

  258.     /**
  259.      * Constructs an instance.
  260.      */
  261.     private PropertiesFactory() {
  262.         // There is only one instance.
  263.     }

  264.     /**
  265.      * Subclasses override to provide customized properties instances.
  266.      *
  267.      * @return a new Properties instance.
  268.      */
  269.     @Override
  270.     protected Properties createProperties() {
  271.         return new Properties();
  272.     }

  273. }