001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.PrintStream;
018    import java.io.PrintWriter;
019    
020    import java.util.Collections;
021    import java.util.Comparator;
022    import java.util.Enumeration;
023    import java.util.Map;
024    import java.util.Properties;
025    import java.util.Set;
026    import java.util.TreeSet;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class SortedProperties extends Properties {
032    
033            public SortedProperties() {
034                    this(null, null);
035            }
036    
037            public SortedProperties(Comparator<String> comparator) {
038                    this(comparator, null);
039            }
040    
041            public SortedProperties(
042                    Comparator<String> comparator, Properties properties) {
043    
044                    if (comparator != null) {
045                            _names = new TreeSet<String>(comparator);
046                    }
047                    else {
048                            _names = new TreeSet<String>();
049                    }
050    
051                    if (properties != null) {
052                            for (Map.Entry<Object, Object> entry : properties.entrySet()) {
053                                    String key = (String)entry.getKey();
054                                    String value = (String)entry.getValue();
055    
056                                    setProperty(key, value);
057                            }
058                    }
059            }
060    
061            public SortedProperties(Properties properties) {
062                    this(null, properties);
063            }
064    
065            @Override
066            public void clear() {
067                    super.clear();
068    
069                    _names.clear();
070            }
071    
072            @Override
073            public void list(PrintStream out) {
074                    System.out.println("-- listing properties --");
075    
076                    Enumeration<String> enu = propertyNames();
077    
078                    while (enu.hasMoreElements()) {
079                            String name = enu.nextElement();
080    
081                            out.println(name + StringPool.EQUAL + getProperty(name));
082                    }
083            }
084    
085            @Override
086            public void list(PrintWriter out) {
087                    System.out.println("-- listing properties --");
088    
089                    Enumeration<String> enu = propertyNames();
090    
091                    while (enu.hasMoreElements()) {
092                            String name = enu.nextElement();
093    
094                            out.println(name + StringPool.EQUAL + getProperty(name));
095                    }
096            }
097    
098            @Override
099            public Enumeration<String> propertyNames() {
100                    return Collections.enumeration(_names);
101            }
102    
103            public Object put(String key, String value) {
104                    if (_names.contains(key)) {
105                            _names.remove(key);
106                    }
107    
108                    _names.add(key);
109    
110                    return super.put(key, value);
111            }
112    
113            @Override
114            public Object remove(Object key) {
115                    _names.remove(key);
116    
117                    return super.remove(key);
118            }
119    
120            @Override
121            public Object setProperty(String key, String value) {
122                    return put(key, value);
123            }
124    
125            private Set<String> _names;
126    
127    }