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                    _comparator = comparator;
045    
046                    if (comparator != null) {
047                            _names = new TreeSet<String>(comparator);
048                    }
049                    else {
050                            _names = new TreeSet<String>();
051                    }
052    
053                    if (properties != null) {
054                            for (Map.Entry<Object, Object> entry : properties.entrySet()) {
055                                    String key = (String)entry.getKey();
056                                    String value = (String)entry.getValue();
057    
058                                    setProperty(key, value);
059                            }
060                    }
061            }
062    
063            public SortedProperties(Properties properties) {
064                    this(null, properties);
065            }
066    
067            @Override
068            public void clear() {
069                    super.clear();
070    
071                    _names.clear();
072            }
073    
074            @Override
075            public Set<Map.Entry<Object, Object>> entrySet() {
076                    Set<Map.Entry<Object, Object>> set =
077                            new TreeSet<Map.Entry<Object, Object>>(
078                                    new Comparator<Map.Entry<Object, Object>>() {
079    
080                                            @Override
081                                            public int compare(
082                                                    Map.Entry<Object, Object> object1,
083                                                    Map.Entry<Object, Object> object2) {
084    
085                                                    String key1 = String.valueOf(object1.getKey());
086                                                    String key2 = String.valueOf(object2.getKey());
087    
088                                                    if (_comparator == null) {
089                                                            return key1.compareTo(key2);
090                                                    }
091    
092                                                    return _comparator.compare(key1, key2);
093                                            }
094    
095                                    });
096    
097                    set.addAll(super.entrySet());
098    
099                    return set;
100            }
101    
102            @Override
103            public void list(PrintStream out) {
104                    System.out.println("-- listing properties --");
105    
106                    Enumeration<String> enu = propertyNames();
107    
108                    while (enu.hasMoreElements()) {
109                            String name = enu.nextElement();
110    
111                            out.println(name + StringPool.EQUAL + getProperty(name));
112                    }
113            }
114    
115            @Override
116            public void list(PrintWriter out) {
117                    System.out.println("-- listing properties --");
118    
119                    Enumeration<String> enu = propertyNames();
120    
121                    while (enu.hasMoreElements()) {
122                            String name = enu.nextElement();
123    
124                            out.println(name + StringPool.EQUAL + getProperty(name));
125                    }
126            }
127    
128            @Override
129            public Enumeration<String> propertyNames() {
130                    return Collections.enumeration(_names);
131            }
132    
133            public Object put(String key, String value) {
134                    if (_names.contains(key)) {
135                            _names.remove(key);
136                    }
137    
138                    _names.add(key);
139    
140                    return super.put(key, value);
141            }
142    
143            @Override
144            public Object remove(Object key) {
145                    _names.remove(key);
146    
147                    return super.remove(key);
148            }
149    
150            @Override
151            public Object setProperty(String key, String value) {
152                    return put(key, value);
153            }
154    
155            private Comparator<String> _comparator;
156            private Set<String> _names;
157    
158    }