Hi,

I just committed the following additions to java.util.Collections for new
1.3 fields and methods and two small compile patches.

    * java/util/Collections.java (EMPTY_MAP) (singletonList(Object)
    (singletonMap(Object,Object)): implemented, new in 1.3.
    * java/io/PrintWriter.java (print(String)): Don't catch IOException,
    write(String) already does.
    * java/io/ByteArrayOutputStream.java: Fix from libgcj for double assignment
    of final variable initial_buffer_size.

Note that the new EMPTY_MAP, singletonList and singletonMap are also not
Serializable just as some other fields and methods in that class. Suns
spec doesn't give enough information on how the serialized form would
look. So we cannot easily make them compatible with their version.
But we should think about how making them serializable anyway.

The ByteArrayOutputStream patch comes from libgcj. We should try to
keep in sync with them for the classes that are already merged.

Cheers,

Mark
Index: java/util/Collections.java
===================================================================
RCS file: /cvs/classpath/java/util/Collections.java,v
retrieving revision 1.12
diff -u -r1.12 Collections.java
--- java/util/Collections.java  2000/03/15 21:59:09     1.12
+++ java/util/Collections.java  2000/08/23 21:12:45
@@ -102,6 +102,18 @@
   };
 
   /**
+   * An immutable, empty Map.
+   * Note: This implementation isn't serializable, although it should be by the
+   * spec.
+   */
+  public static final Map EMPTY_MAP = new AbstractMap() {
+
+    public Set entrySet() {
+      return EMPTY_SET;
+    }
+  };
+
+  /**
    * Compare two objects with or without a Comparator. If c is null, uses the
    * natural ordering. Slightly slower than doing it inline if the JVM isn't
    * clever, but worth it for removing a duplicate of the search code.
@@ -530,6 +542,50 @@
             throw new UnsupportedOperationException();
           }
        };
+      }
+    };
+  }
+
+  /**
+   * Obtain an immutable List consisting of a single element. The return value
+   * of this method is Serializable.
+   *
+   * @param o the single element.
+   * @returns an immutable List containing only o.
+   */
+  // It's not serializable because the spec is broken.
+  public static List singletonList(final Object o) {
+
+    return new AbstractList() {
+
+      public int size() {
+        return 1;
+      }
+
+      public Object get(int index) {
+       if (index == 0) {
+          throw new IndexOutOfBoundsException();
+        } else {
+          return o;
+        }
+      }
+    };
+  }
+       
+  /**
+   * Obtain an immutable Map consisting of a single key value pair.
+   * The return value of this method is Serializable.
+   *
+   * @param key the single key.
+   * @param value the single value.
+   * @returns an immutable Map containing only the single key value pair.
+   */
+  // It's not serializable because the spec is broken.
+  public static Map singletonMap(final Object key, final Object value) {
+
+    return new AbstractMap() {
+      public Set entrySet() {
+        return singleton(new BasicMapEntry(key, value));
       }
     };
   }

Reply via email to