Return-Path: Delivered-To: apmail-jakarta-log4j-dev-archive@apache.org Received: (qmail 75159 invoked from network); 16 Feb 2003 03:04:41 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 16 Feb 2003 03:04:41 -0000 Received: (qmail 29093 invoked by uid 97); 16 Feb 2003 03:06:22 -0000 Delivered-To: qmlist-jakarta-archive-log4j-dev@nagoya.betaversion.org Received: (qmail 29086 invoked from network); 16 Feb 2003 03:06:22 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 16 Feb 2003 03:06:22 -0000 Received: (qmail 74928 invoked by uid 500); 16 Feb 2003 03:04:39 -0000 Mailing-List: contact log4j-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Log4J Developers List" Reply-To: "Log4J Developers List" Delivered-To: mailing list log4j-dev@jakarta.apache.org Received: (qmail 74915 invoked from network); 16 Feb 2003 03:04:39 -0000 Received: from ms-smtp-01.nyroc.rr.com (24.92.226.148) by daedalus.apache.org with SMTP; 16 Feb 2003 03:04:39 -0000 Received: from twcny.rr.com (syr-24-58-20-15.twcny.rr.com [24.58.20.15]) by ms-smtp-01.nyroc.rr.com (8.12.5/8.12.2) with ESMTP id h1G34lb1022252 for ; Sat, 15 Feb 2003 22:04:47 -0500 (EST) Message-ID: <3E4EFFF5.2020802@twcny.rr.com> Date: Sat, 15 Feb 2003 22:05:25 -0500 From: Raymond DeCampo User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Log4J Developers List Subject: Sorted or order-preserving Properties Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Somebody was looking for sorted or order preserving implementations of java.util.Properties. For what it's worth, here's a couple. I haven't made any attempt to optimize them (the order-preserving one in particular is a resource hog), they are not thread-safe and they are a tangle of inner classes but here they are: public class SortedProperties extends Properties { public SortedProperties() { } public java.util.Enumeration keys() { return new java.util.Enumeration() { private java.util.Iterator iterator = new java.util.TreeSet(keySet()).iterator(); public boolean hasMoreElements() { return iterator.hasNext(); } public Object nextElement() { return iterator.next(); } }; } } public class OrderedProperties extends Properties { private int next = 0; private java.util.Map order = new java.util.HashMap(); public OrderedProperties() { } public Object put(Object key, Object value) { if (!order.containsKey(key)) { order.put(key, new Integer(next++)); } return super.put(key, value); } public java.util.Enumeration keys() { return new java.util.Enumeration() { private java.util.Iterator iterator; { java.util.SortedSet sortedKeySet = new java.util.TreeSet( new java.util.Comparator() { public int compare(Object x, Object y) { Integer xInt = (Integer)order.get(x); Integer yInt = (Integer)order.get(y); if (xInt == null) { xInt = new Integer(0); } if (yInt == null) { yInt = new Integer(0); } return xInt.compareTo(yInt); } }); sortedKeySet.addAll(keySet()); iterator = sortedKeySet.iterator(); } public boolean hasMoreElements() { return iterator.hasNext(); } public Object nextElement() { return iterator.next(); } }; } } They rely on the fact that java.util.Properties uses java.util.Hashtable.keys() to get the set of keys to iterate over for writing out the file. If that internal implementation detail changes, the classes will fail to work. All-in-all they are a non-production-level hack but hey -- doesn't everything start out that way? Ray --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: log4j-dev-help@jakarta.apache.org