logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Raymond DeCampo <rdeca...@twcny.rr.com>
Subject Sorted or order-preserving Properties
Date Sun, 16 Feb 2003 03:05:25 GMT
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


Mime
View raw message