    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.collections4.properties;
    019
    020import java.util.AbstractMap;
    021import java.util.Collections;
    022import java.util.Enumeration;
    023import java.util.LinkedHashSet;
    024import java.util.Map;
    025import java.util.Properties;
    026import java.util.Set;
    027import java.util.TreeSet;
    028import java.util.function.BiConsumer;
    029import java.util.stream.Collectors;
    030import java.util.stream.Stream;
    031
    032/**
    033 * A drop-in replacement for {@link Properties} for sorting keys.
    034 * <p>
    035 * Overrides {@link Properties#keys()} to sort keys. Allows other methods on the superclass to work with sorted keys.
    036 * </p>
    037 *
    038 * @see SortedPropertiesFactory#INSTANCE
    039 * @since 4.2
    040 */
    041public class SortedProperties extends Properties {
    042
    043    private static final long serialVersionUID = 1L;
    044
    045    /**
    046     * Constructs a new instance.
    047     */
    048    public SortedProperties() {
    049        // empty
    050    }
    051
    052    @Override
    053    public Set<Map.Entry<Object, Object>> entrySet() {
    054        return keyStream().map(k -> new AbstractMap.SimpleEntry<>(k, get(k))).collect(Collectors.toCollection(LinkedHashSet::new));
    055    }
    056
    057    /**
    058     * Enumerates all key/value pairs in the specified TreeSet and omits the property if the key or value is not a string.
    059     *
    060     * @param result The result set to populate.
    061     * @return The given set.
    062     */
    063    private synchronized TreeSet<String> enumerateStringProperties(final TreeSet<String> result) {
    064        if (defaults != null) {
    065            result.addAll(defaults.stringPropertyNames());
    066        }
    067        for (final Enumeration<?> e = keys(); e.hasMoreElements();) {
    068            final Object k = e.nextElement();
    069            final Object v = get(k);
    070            if (k instanceof String && v instanceof String) {
    071                result.add((String) k);
    072            }
    073        }
    074        return result;
    075    }
    076
    077    @Override
    078    public synchronized void forEach(final BiConsumer<? super Object, ? super Object> action) {
    079        keyStream().forEach(k -> action.accept(k, get(k)));
    080    }
    081
    082    @Override
    083    public synchronized Enumeration<Object> keys() {
    084        return Collections.enumeration(keySet());
    085    }
    086
    087    @Override
    088    public Set<Object> keySet() {
    089        return new TreeSet<>(super.keySet());
    090    }
    091
    092    private Stream<Object> keyStream() {
    093        return keySet().stream();
    094    }
    095
    096    @Override
    097    public Enumeration<?> propertyNames() {
    098        return Collections.enumeration(stringPropertyNames());
    099    }
    100
    101    @Override
    102    public Set<String> stringPropertyNames() {
    103        return enumerateStringProperties(new TreeSet<>());
    104    }
    105}