1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.io.PrintStream;
26  import java.io.PrintWriter;
27  
28  import java.util.Collections;
29  import java.util.Enumeration;
30  import java.util.Properties;
31  import java.util.Set;
32  import java.util.TreeSet;
33  
34  /**
35   * <a href="SortedProperties.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class SortedProperties extends Properties {
40  
41      public SortedProperties() {
42          super();
43  
44          _names = new TreeSet<String>();
45      }
46  
47      public SortedProperties(Properties properties) {
48          this();
49  
50          Enumeration<String> enu =
51              (Enumeration<String>)properties.propertyNames();
52  
53          while (enu.hasMoreElements()) {
54              String key = enu.nextElement();
55  
56              String value = properties.getProperty(key);
57  
58              setProperty(key, value);
59          }
60      }
61  
62      public void clear() {
63          super.clear();
64  
65          _names.clear();
66      }
67  
68      public void list(PrintStream out) {
69          System.out.println("-- listing properties --");
70  
71          Enumeration<String> enu = propertyNames();
72  
73          while (enu.hasMoreElements()) {
74              String name = enu.nextElement();
75  
76              out.println(name + StringPool.EQUAL + getProperty(name));
77          }
78      }
79  
80      public void list(PrintWriter out) {
81          System.out.println("-- listing properties --");
82  
83          Enumeration<String> enu = propertyNames();
84  
85          while (enu.hasMoreElements()) {
86              String name = enu.nextElement();
87  
88              out.println(name + StringPool.EQUAL + getProperty(name));
89          }
90      }
91  
92      public Enumeration<String> propertyNames() {
93          return Collections.enumeration(_names);
94      }
95  
96      public Object put(String key, String value) {
97          if (_names.contains(key)) {
98              _names.remove(key);
99          }
100 
101         _names.add(key);
102 
103         return super.put(key, value);
104     }
105 
106     public Object remove(Object key) {
107         _names.remove(key);
108 
109         return super.remove(key);
110     }
111 
112     public Object setProperty(String key, String value) {
113         return put(key, value);
114     }
115 
116     private Set<String> _names;
117 
118 }