OrderedProperties.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.util.AbstractMap.SimpleEntry;
  19. import java.util.Collections;
  20. import java.util.Enumeration;
  21. import java.util.Iterator;
  22. import java.util.LinkedHashSet;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import java.util.Properties;
  26. import java.util.Set;
  27. import java.util.function.BiConsumer;
  28. import java.util.function.BiFunction;
  29. import java.util.function.Function;
  30. import java.util.stream.Collectors;

  31. /**
  32.  * A drop-in replacement for {@link Properties} for ordered keys.
  33.  * <p>
  34.  * Overrides methods to keep keys in insertion order. Allows other methods in the superclass to work with ordered keys.
  35.  * </p>
  36.  *
  37.  * @see OrderedPropertiesFactory#INSTANCE
  38.  * @since 4.5.0-M1
  39.  */
  40. public class OrderedProperties extends Properties {

  41.     private static final long serialVersionUID = 1L;

  42.     /**
  43.      * Preserves the insertion order.
  44.      */
  45.     private final LinkedHashSet<Object> orderedKeys = new LinkedHashSet<>();

  46.     /**
  47.      * Constructs a new instance.
  48.      */
  49.     public OrderedProperties() {
  50.         // empty
  51.     }

  52.     @Override
  53.     public synchronized void clear() {
  54.         orderedKeys.clear();
  55.         super.clear();
  56.     }

  57.     @Override
  58.     public synchronized Object compute(final Object key, final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
  59.         final Object compute = super.compute(key, remappingFunction);
  60.         if (compute != null) {
  61.             orderedKeys.add(key);
  62.         }
  63.         return compute;
  64.     }

  65.     @Override
  66.     public synchronized Object computeIfAbsent(final Object key, final Function<? super Object, ? extends Object> mappingFunction) {
  67.         final Object computeIfAbsent = super.computeIfAbsent(key, mappingFunction);
  68.         if (computeIfAbsent != null) {
  69.             orderedKeys.add(key);
  70.         }
  71.         return computeIfAbsent;
  72.     }

  73.     @Override
  74.     public Set<Map.Entry<Object, Object>> entrySet() {
  75.         return orderedKeys.stream().map(k -> new SimpleEntry<>(k, get(k))).collect(Collectors.toCollection(LinkedHashSet::new));
  76.     }

  77.     @Override
  78.     public synchronized void forEach(final BiConsumer<? super Object, ? super Object> action) {
  79.         Objects.requireNonNull(action);
  80.         orderedKeys.forEach(k -> action.accept(k, get(k)));
  81.     }

  82.     @Override
  83.     public synchronized Enumeration<Object> keys() {
  84.         return Collections.enumeration(orderedKeys);
  85.     }

  86.     @Override
  87.     public Set<Object> keySet() {
  88.         return orderedKeys;
  89.     }

  90.     @Override
  91.     public synchronized Object merge(final Object key, final Object value,
  92.             final BiFunction<? super Object, ? super Object, ? extends Object> remappingFunction) {
  93.         orderedKeys.add(key);
  94.         return super.merge(key, value, remappingFunction);
  95.     }

  96.     @Override
  97.     public Enumeration<?> propertyNames() {
  98.         return Collections.enumeration(orderedKeys);
  99.     }

  100.     @Override
  101.     public synchronized Object put(final Object key, final Object value) {
  102.         final Object put = super.put(key, value);
  103.         if (put == null) {
  104.             orderedKeys.add(key);
  105.         }
  106.         return put;
  107.     }

  108.     @Override
  109.     public synchronized void putAll(final Map<? extends Object, ? extends Object> t) {
  110.         orderedKeys.addAll(t.keySet());
  111.         super.putAll(t);
  112.     }

  113.     @Override
  114.     public synchronized Object putIfAbsent(final Object key, final Object value) {
  115.         final Object putIfAbsent = super.putIfAbsent(key, value);
  116.         if (putIfAbsent == null) {
  117.             orderedKeys.add(key);
  118.         }
  119.         return putIfAbsent;
  120.     }

  121.     @Override
  122.     public synchronized Object remove(final Object key) {
  123.         final Object remove = super.remove(key);
  124.         if (remove != null) {
  125.             orderedKeys.remove(key);
  126.         }
  127.         return remove;
  128.     }

  129.     @Override
  130.     public synchronized boolean remove(final Object key, final Object value) {
  131.         final boolean remove = super.remove(key, value);
  132.         if (remove) {
  133.             orderedKeys.remove(key);
  134.         }
  135.         return remove;
  136.     }

  137.     @Override
  138.     public synchronized String toString() {
  139.         // Must override for Java 17 to maintain order since the implementation is based on a map
  140.         final int max = size() - 1;
  141.         if (max == -1) {
  142.             return "{}";
  143.         }
  144.         final StringBuilder sb = new StringBuilder();
  145.         final Iterator<Map.Entry<Object, Object>> it = entrySet().iterator();
  146.         sb.append('{');
  147.         for (int i = 0;; i++) {
  148.             final Map.Entry<Object, Object> e = it.next();
  149.             final Object key = e.getKey();
  150.             final Object value = e.getValue();
  151.             sb.append(key == this ? "(this Map)" : key.toString());
  152.             sb.append('=');
  153.             sb.append(value == this ? "(this Map)" : value.toString());
  154.             if (i == max) {
  155.                 return sb.append('}').toString();
  156.             }
  157.             sb.append(", ");
  158.         }
  159.     }
  160. }