All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.i18n.SortedProperties Maven / Gradle / Ivy

package org.nuiton.i18n;

/*-
 * #%L
 * I18n :: Api
 * %%
 * Copyright (C) 2004 - 2017 Code Lutin, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import java.util.stream.Collectors;

/**
 * Permet d'avoir les propriétés triées.
 *
 * @author Julien Ruchaud - [email protected]
 * @author Tony Chemit - [email protected]
 */
public class SortedProperties extends Properties {

    private static final long serialVersionUID = -1147150444452577558L;


    public SortedProperties() {
    }

    public SortedProperties(Properties defaults) {
        super(defaults);
    }

    @Override
    public synchronized Enumeration keys() {
        List objects = Collections.list(super.keys());
        Vector result;
        try {
            List list = objects.stream().map(Object::toString).collect(Collectors.toList());
            Collections.sort(list);
            result = new Vector<>(list);
        } catch (IllegalArgumentException e) {
            // keys are not string !!!
            // can not sort keys
            result = new Vector<>(objects);
        }
        return result.elements();
    }
}