diff options
Diffstat (limited to 'libjava/classpath/java/util')
-rw-r--r-- | libjava/classpath/java/util/AbstractMap.java | 4 | ||||
-rw-r--r-- | libjava/classpath/java/util/ArrayList.java | 2 | ||||
-rw-r--r-- | libjava/classpath/java/util/Collections.java | 8 | ||||
-rw-r--r-- | libjava/classpath/java/util/Hashtable.java | 232 | ||||
-rw-r--r-- | libjava/classpath/java/util/Properties.java | 226 | ||||
-rw-r--r-- | libjava/classpath/java/util/StringTokenizer.java | 3 | ||||
-rw-r--r-- | libjava/classpath/java/util/WeakHashMap.java | 6 | ||||
-rw-r--r-- | libjava/classpath/java/util/logging/XMLFormatter.java | 2 | ||||
-rw-r--r-- | libjava/classpath/java/util/regex/Pattern.java | 5 |
9 files changed, 238 insertions, 250 deletions
diff --git a/libjava/classpath/java/util/AbstractMap.java b/libjava/classpath/java/util/AbstractMap.java index 7cd6436a308..b4ab882addf 100644 --- a/libjava/classpath/java/util/AbstractMap.java +++ b/libjava/classpath/java/util/AbstractMap.java @@ -594,13 +594,13 @@ public abstract class AbstractMap implements Map * * @param o1 the first object * @param o2 the second object - * @return o1 == null ? o2 == null : o1.equals(o2) + * @return o1 == o2 || (o1 != null && o1.equals(o2)) */ // Package visible for use throughout java.util. // It may be inlined since it is final. static final boolean equals(Object o1, Object o2) { - return o1 == null ? o2 == null : o1.equals(o2); + return o1 == o2 || (o1 != null && o1.equals(o2)); } /** diff --git a/libjava/classpath/java/util/ArrayList.java b/libjava/classpath/java/util/ArrayList.java index 752f9da4ee0..50b5638ede0 100644 --- a/libjava/classpath/java/util/ArrayList.java +++ b/libjava/classpath/java/util/ArrayList.java @@ -92,7 +92,7 @@ public class ArrayList extends AbstractList /** * The default capacity for new ArrayLists. */ - private static final int DEFAULT_CAPACITY = 16; + private static final int DEFAULT_CAPACITY = 10; /** * The number of elements in this list. diff --git a/libjava/classpath/java/util/Collections.java b/libjava/classpath/java/util/Collections.java index e650bf8bda9..dc37bad8a93 100644 --- a/libjava/classpath/java/util/Collections.java +++ b/libjava/classpath/java/util/Collections.java @@ -670,10 +670,10 @@ public class Collections for ( ; i != pos; i--, o = itr.previous()); forward = false; } - final int d = compare(key, o, c); + final int d = compare(o, key, c); if (d == 0) return pos; - else if (d < 0) + else if (d > 0) hi = pos - 1; else // This gets the insertion point right on the last loop @@ -685,10 +685,10 @@ public class Collections while (low <= hi) { pos = (low + hi) >> 1; - final int d = compare(key, l.get(pos), c); + final int d = compare(l.get(pos), key, c); if (d == 0) return pos; - else if (d < 0) + else if (d > 0) hi = pos - 1; else // This gets the insertion point right on the last loop diff --git a/libjava/classpath/java/util/Hashtable.java b/libjava/classpath/java/util/Hashtable.java index 011cafaa855..76b0d5c15bd 100644 --- a/libjava/classpath/java/util/Hashtable.java +++ b/libjava/classpath/java/util/Hashtable.java @@ -1,6 +1,7 @@ /* Hashtable.java -- a class providing a basic hashtable data structure, mapping Object --> Object - Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006 + Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -110,12 +111,6 @@ public class Hashtable extends Dictionary */ private static final int DEFAULT_CAPACITY = 11; - /** An "enum" of iterator types. */ - // Package visible for use by nested classes. - static final int KEYS = 0, - VALUES = 1, - ENTRIES = 2; - /** * The default load factor; this is explicitly specified by the spec. */ @@ -302,7 +297,7 @@ public class Hashtable extends Dictionary */ public Enumeration keys() { - return new Enumerator(KEYS); + return new KeyEnumerator(); } /** @@ -316,7 +311,7 @@ public class Hashtable extends Dictionary */ public Enumeration elements() { - return new Enumerator(VALUES); + return new ValueEnumerator(); } /** @@ -333,20 +328,19 @@ public class Hashtable extends Dictionary */ public synchronized boolean contains(Object value) { + if (value == null) + throw new NullPointerException(); + for (int i = buckets.length - 1; i >= 0; i--) { HashEntry e = buckets[i]; while (e != null) { - if (value.equals(e.value)) + if (e.value.equals(value)) return true; e = e.next; } } - - // Must throw on null argument even if the table is empty - if (value == null) - throw new NullPointerException(); return false; } @@ -385,7 +379,7 @@ public class Hashtable extends Dictionary HashEntry e = buckets[idx]; while (e != null) { - if (key.equals(e.key)) + if (e.key.equals(key)) return true; e = e.next; } @@ -408,7 +402,7 @@ public class Hashtable extends Dictionary HashEntry e = buckets[idx]; while (e != null) { - if (key.equals(e.key)) + if (e.key.equals(key)) return e.value; e = e.next; } @@ -438,7 +432,7 @@ public class Hashtable extends Dictionary while (e != null) { - if (key.equals(e.key)) + if (e.key.equals(key)) { // Bypass e.setValue, since we already know value is non-null. Object r = e.value; @@ -484,7 +478,7 @@ public class Hashtable extends Dictionary while (e != null) { - if (key.equals(e.key)) + if (e.key.equals(key)) { modCount++; if (last == null) @@ -581,8 +575,8 @@ public class Hashtable extends Dictionary { // Since we are already synchronized, and entrySet().iterator() // would repeatedly re-lock/release the monitor, we directly use the - // unsynchronized HashIterator instead. - Iterator entries = new HashIterator(ENTRIES); + // unsynchronized EntryIterator instead. + Iterator entries = new EntryIterator(); StringBuffer r = new StringBuffer("{"); for (int pos = size; pos > 0; pos--) { @@ -624,7 +618,7 @@ public class Hashtable extends Dictionary public Iterator iterator() { - return new HashIterator(KEYS); + return new KeyIterator(); } public void clear() @@ -682,7 +676,7 @@ public class Hashtable extends Dictionary public Iterator iterator() { - return new HashIterator(VALUES); + return new ValueIterator(); } public void clear() @@ -734,7 +728,7 @@ public class Hashtable extends Dictionary public Iterator iterator() { - return new HashIterator(ENTRIES); + return new EntryIterator(); } public void clear() @@ -798,8 +792,8 @@ public class Hashtable extends Dictionary { // Since we are already synchronized, and entrySet().iterator() // would repeatedly re-lock/release the monitor, we directly use the - // unsynchronized HashIterator instead. - Iterator itr = new HashIterator(ENTRIES); + // unsynchronized EntryIterator instead. + Iterator itr = new EntryIterator(); int hashcode = 0; for (int pos = size; pos > 0; pos--) hashcode += itr.next().hashCode(); @@ -844,7 +838,7 @@ public class Hashtable extends Dictionary HashEntry e = buckets[idx]; while (e != null) { - if (o.equals(e)) + if (e.equals(o)) return e; e = e.next; } @@ -904,8 +898,12 @@ public class Hashtable extends Dictionary if (dest != null) { - while (dest.next != null) - dest = dest.next; + HashEntry next = dest.next; + while (next != null) + { + dest = next; + next = dest.next; + } dest.next = e; } else @@ -940,8 +938,8 @@ public class Hashtable extends Dictionary s.writeInt(size); // Since we are already synchronized, and entrySet().iterator() // would repeatedly re-lock/release the monitor, we directly use the - // unsynchronized HashIterator instead. - Iterator it = new HashIterator(ENTRIES); + // unsynchronized EntryIterator instead. + Iterator it = new EntryIterator(); while (it.hasNext()) { HashEntry entry = (HashEntry) it.next(); @@ -980,22 +978,18 @@ public class Hashtable extends Dictionary /** * A class which implements the Iterator interface and is used for * iterating over Hashtables. - * This implementation is parameterized to give a sequential view of - * keys, values, or entries; it also allows the removal of elements, - * as per the Javasoft spec. Note that it is not synchronized; this is - * a performance enhancer since it is never exposed externally and is - * only used within synchronized blocks above. + * This implementation iterates entries. Subclasses are used to + * iterate key and values. It also allows the removal of elements, + * as per the Javasoft spec. Note that it is not synchronized; this + * is a performance enhancer since it is never exposed externally + * and is only used within synchronized blocks above. * * @author Jon Zeppieri + * @author Fridjof Siebert */ - private final class HashIterator implements Iterator + private class EntryIterator implements Iterator { /** - * The type of this Iterator: {@link #KEYS}, {@link #VALUES}, - * or {@link #ENTRIES}. - */ - final int type; - /** * The number of modifications to the backing Hashtable that we know about. */ int knownMod = modCount; @@ -1013,14 +1007,13 @@ public class Hashtable extends Dictionary HashEntry next; /** - * Construct a new HashIterator with the supplied type. - * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES} + * Construct a new EtryIterator */ - HashIterator(int type) + EntryIterator() { - this.type = type; } + /** * Returns true if the Iterator has more elements. * @return true if there are more elements @@ -1049,14 +1042,13 @@ public class Hashtable extends Dictionary HashEntry e = next; while (e == null) - e = buckets[--idx]; + if (idx <= 0) + return null; + else + e = buckets[--idx]; next = e.next; last = e; - if (type == VALUES) - return e.value; - if (type == KEYS) - return e.key; return e; } @@ -1077,29 +1069,70 @@ public class Hashtable extends Dictionary last = null; knownMod++; } - } // class HashIterator + } // class EntryIterator + + /** + * A class which implements the Iterator interface and is used for + * iterating over keys in Hashtables. + * + * @author Fridtjof Siebert + */ + private class KeyIterator extends EntryIterator + { + /** + * Returns the next element in the Iterator's sequential view. + * + * @return the next element + * + * @throws ConcurrentModificationException if the hashtable was modified + * @throws NoSuchElementException if there is none + */ + public Object next() + { + return ((HashEntry)super.next()).key; + } + } // class KeyIterator + /** - * Enumeration view of this Hashtable, providing sequential access to its - * elements; this implementation is parameterized to provide access either - * to the keys or to the values in the Hashtable. + * A class which implements the Iterator interface and is used for + * iterating over values in Hashtables. + * + * @author Fridtjof Siebert + */ + private class ValueIterator extends EntryIterator + { + /** + * Returns the next element in the Iterator's sequential view. + * + * @return the next element + * + * @throws ConcurrentModificationException if the hashtable was modified + * @throws NoSuchElementException if there is none + */ + public Object next() + { + return ((HashEntry)super.next()).value; + } + } // class ValueIterator + + /** + * Enumeration view of the entries in this Hashtable, providing + * sequential access to its elements. * * <b>NOTE</b>: Enumeration is not safe if new elements are put in the table * as this could cause a rehash and we'd completely lose our place. Even * without a rehash, it is undetermined if a new element added would * appear in the enumeration. The spec says nothing about this, but - * the "Java Class Libraries" book infers that modifications to the + * the "Java Class Libraries" book implies that modifications to the * hashtable during enumeration causes indeterminate results. Don't do it! * * @author Jon Zeppieri + * @author Fridjof Siebert */ - private final class Enumerator implements Enumeration + private class EntryEnumerator implements Enumeration { - /** - * The type of this Iterator: {@link #KEYS} or {@link #VALUES}. - */ - final int type; /** The number of elements remaining to be returned by next(). */ int count = size; /** Current index in the physical hash table. */ @@ -1113,11 +1146,10 @@ public class Hashtable extends Dictionary /** * Construct the enumeration. - * @param type either {@link #KEYS} or {@link #VALUES}. */ - Enumerator(int type) + EntryEnumerator() { - this.type = type; + // Nothing to do here. } /** @@ -1142,10 +1174,78 @@ public class Hashtable extends Dictionary HashEntry e = next; while (e == null) - e = buckets[--idx]; + if (idx <= 0) + return null; + else + e = buckets[--idx]; next = e.next; - return type == VALUES ? e.value : e.key; + return e; + } + } // class EntryEnumerator + + + /** + * Enumeration view of this Hashtable, providing sequential access to its + * elements. + * + * <b>NOTE</b>: Enumeration is not safe if new elements are put in the table + * as this could cause a rehash and we'd completely lose our place. Even + * without a rehash, it is undetermined if a new element added would + * appear in the enumeration. The spec says nothing about this, but + * the "Java Class Libraries" book implies that modifications to the + * hashtable during enumeration causes indeterminate results. Don't do it! + * + * @author Jon Zeppieri + * @author Fridjof Siebert + */ + private final class KeyEnumerator extends EntryEnumerator + { + /** + * Returns the next element. + * @return the next element + * @throws NoSuchElementException if there is none. + */ + public Object nextElement() + { + HashEntry entry = (HashEntry) super.nextElement(); + Object retVal = null; + if (entry != null) + retVal = entry.key; + return retVal; } - } // class Enumerator + } // class KeyEnumerator + + + /** + * Enumeration view of this Hashtable, providing sequential access to its + * values. + * + * <b>NOTE</b>: Enumeration is not safe if new elements are put in the table + * as this could cause a rehash and we'd completely lose our place. Even + * without a rehash, it is undetermined if a new element added would + * appear in the enumeration. The spec says nothing about this, but + * the "Java Class Libraries" book implies that modifications to the + * hashtable during enumeration causes indeterminate results. Don't do it! + * + * @author Jon Zeppieri + * @author Fridjof Siebert + */ + private final class ValueEnumerator extends EntryEnumerator + { + /** + * Returns the next element. + * @return the next element + * @throws NoSuchElementException if there is none. + */ + public Object nextElement() + { + HashEntry entry = (HashEntry) super.nextElement(); + Object retVal = null; + if (entry != null) + retVal = entry.value; + return retVal; + } + } // class ValueEnumerator + } // class Hashtable diff --git a/libjava/classpath/java/util/Properties.java b/libjava/classpath/java/util/Properties.java index 7c468da8b4f..eb208f5a93d 100644 --- a/libjava/classpath/java/util/Properties.java +++ b/libjava/classpath/java/util/Properties.java @@ -47,15 +47,10 @@ import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.xml.sax.Attributes; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.DefaultHandler2; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; @@ -743,173 +738,64 @@ label = Name:\\u0020</pre> throw new NullPointerException("Null input stream supplied."); try { - SAXParserFactory factory = SAXParserFactory.newInstance(); - factory.setValidating(false); /* Don't use the URI */ - XMLReader parser = factory.newSAXParser().getXMLReader(); - PropertiesHandler handler = new PropertiesHandler(); - parser.setContentHandler(handler); - parser.setProperty("http://xml.org/sax/properties/lexical-handler", - handler); - parser.parse(new InputSource(in)); + XMLInputFactory factory = XMLInputFactory.newInstance(); + // Don't resolve external entity references + factory.setProperty("javax.xml.stream.isSupportingExternalEntities", + Boolean.FALSE); + XMLStreamReader reader = factory.createXMLStreamReader(in); + String name, key = null; + StringBuffer buf = null; + while (reader.hasNext()) + { + switch (reader.next()) + { + case XMLStreamConstants.START_ELEMENT: + name = reader.getLocalName(); + if (buf == null && "entry".equals(name)) + { + key = reader.getAttributeValue(null, "key"); + if (key == null) + { + String msg = "missing 'key' attribute"; + throw new InvalidPropertiesFormatException(msg); + } + buf = new StringBuffer(); + } + else if (!"properties".equals(name) && !"comment".equals(name)) + { + String msg = "unexpected element name '" + name + "'"; + throw new InvalidPropertiesFormatException(msg); + } + break; + case XMLStreamConstants.END_ELEMENT: + name = reader.getLocalName(); + if (buf != null && "entry".equals(name)) + { + put(key, buf.toString()); + buf = null; + } + else if (!"properties".equals(name) && !"comment".equals(name)) + { + String msg = "unexpected element name '" + name + "'"; + throw new InvalidPropertiesFormatException(msg); + } + break; + case XMLStreamConstants.CHARACTERS: + case XMLStreamConstants.SPACE: + case XMLStreamConstants.CDATA: + if (buf != null) + buf.append(reader.getText()); + break; + } + } + reader.close(); } - catch (SAXException e) + catch (XMLStreamException e) { throw (InvalidPropertiesFormatException) new InvalidPropertiesFormatException("Error in parsing XML."). initCause(e); } - catch (ParserConfigurationException e) - { - throw (IOException) - new IOException("An XML parser could not be found."). - initCause(e); - } } - /** - * This class deals with the parsing of XML using - * <a href="http://java.sun.com/dtd/properties.dtd"> - * http://java.sun.com/dtd/properties.dtd</a>. - * - * @author Andrew John Hughes (gnu_andrew@member.fsf.org) - * @since 1.5 - */ - private class PropertiesHandler - extends DefaultHandler2 - { - - /** - * The current key. - */ - private String key; - - /** - * The current value. - */ - private String value; - - /** - * A flag to check whether a valid DTD declaration has been seen. - */ - private boolean dtdDeclSeen; - - /** - * Constructs a new Properties handler. - */ - public PropertiesHandler() - { - key = null; - value = null; - dtdDeclSeen = false; - } - - /** - * <p> - * Captures the start of the DTD declarations, if they exist. - * A valid properties file must declare the following doctype: - * </p> - * <p> - * <code>!DOCTYPE properties SYSTEM - * "http://java.sun.com/dtd/properties.dtd"</code> - * </p> - * - * @param name the name of the document type. - * @param publicId the public identifier that was declared, or - * null if there wasn't one. - * @param systemId the system identifier that was declared, or - * null if there wasn't one. - * @throws SAXException if some error occurs in parsing. - */ - public void startDTD(String name, String publicId, String systemId) - throws SAXException - { - if (name.equals("properties") && - publicId == null && - systemId.equals("http://java.sun.com/dtd/properties.dtd")) - { - dtdDeclSeen = true; - } - else - throw new SAXException("Invalid DTD declaration: " + name); - } - - /** - * Captures the start of an XML element. - * - * @param uri the namespace URI. - * @param localName the local name of the element inside the namespace. - * @param qName the local name qualified with the namespace URI. - * @param attributes the attributes of this element. - * @throws SAXException if some error occurs in parsing. - */ - public void startElement(String uri, String localName, - String qName, Attributes attributes) - throws SAXException - { - if (qName.equals("entry")) - { - int index = attributes.getIndex("key"); - if (index != -1) - key = attributes.getValue(index); - } - else if (qName.equals("comment") || qName.equals("properties")) - { - /* Ignore it */ - } - else - throw new SAXException("Invalid tag: " + qName); - } - - /** - * Captures characters within an XML element. - * - * @param ch the array of characters. - * @param start the start index of the characters to use. - * @param length the number of characters to use from the start index on. - * @throws SAXException if some error occurs in parsing. - */ - public void characters(char[] ch, int start, int length) - throws SAXException - { - if (key != null) - value = new String(ch,start,length); - } - - /** - * Captures the end of an XML element. - * - * @param uri the namespace URI. - * @param localName the local name of the element inside the namespace. - * @param qName the local name qualified with the namespace URI. - * @throws SAXException if some error occurs in parsing. - */ - public void endElement(String uri, String localName, - String qName) - throws SAXException - { - if (qName.equals("entry")) - { - if (value == null) - value = ""; - setProperty(key, value); - key = null; - value = null; - } - } - - /** - * Captures the end of the XML document. If a DTD declaration has - * not been seen, the document is erroneous and an exception is thrown. - * - * @throws SAXException if the correct DTD declaration didn't appear. - */ - public void endDocument() - throws SAXException - { - if (!dtdDeclSeen) - throw new SAXException("No appropriate DTD declaration was seen."); - } - - } // class PropertiesHandler - } // class Properties diff --git a/libjava/classpath/java/util/StringTokenizer.java b/libjava/classpath/java/util/StringTokenizer.java index dcc192c855a..21298c75ec7 100644 --- a/libjava/classpath/java/util/StringTokenizer.java +++ b/libjava/classpath/java/util/StringTokenizer.java @@ -132,8 +132,7 @@ public class StringTokenizer implements Enumeration { len = str.length(); this.str = str; - // The toString() hack causes the NullPointerException. - this.delim = delim.toString(); + this.delim = delim; this.retDelims = returnDelims; this.pos = 0; } diff --git a/libjava/classpath/java/util/WeakHashMap.java b/libjava/classpath/java/util/WeakHashMap.java index 514ad8cd29f..2ed982ac32d 100644 --- a/libjava/classpath/java/util/WeakHashMap.java +++ b/libjava/classpath/java/util/WeakHashMap.java @@ -475,7 +475,7 @@ public class WeakHashMap extends AbstractMap implements Map if (o instanceof Map.Entry) { Map.Entry e = (Map.Entry) o; - return key.equals(e.getKey()) + return WeakHashMap.equals(getKey(), e.getKey()) && WeakHashMap.equals(value, e.getValue()); } return false; @@ -483,7 +483,7 @@ public class WeakHashMap extends AbstractMap implements Map public String toString() { - return key + "=" + value; + return getKey() + "=" + value; } } @@ -657,7 +657,7 @@ public class WeakHashMap extends AbstractMap implements Map while (bucket != null) { WeakBucket.WeakEntry entry = bucket.getEntry(); - if (entry != null && key.equals(entry.key)) + if (entry != null && equals(key, entry.key)) return entry; bucket = bucket.next; diff --git a/libjava/classpath/java/util/logging/XMLFormatter.java b/libjava/classpath/java/util/logging/XMLFormatter.java index 8bd83ba3973..8f5769be1b9 100644 --- a/libjava/classpath/java/util/logging/XMLFormatter.java +++ b/libjava/classpath/java/util/logging/XMLFormatter.java @@ -194,7 +194,7 @@ public class XMLFormatter appendTag(buf, 1, "date", iso8601.format(new Date(millis))); - appendTag(buf, 1, "millis", record.getMillis()); + appendTag(buf, 1, "millis", millis); appendTag(buf, 1, "sequence", record.getSequenceNumber()); appendTag(buf, 1, "logger", record.getLoggerName()); diff --git a/libjava/classpath/java/util/regex/Pattern.java b/libjava/classpath/java/util/regex/Pattern.java index 6a31ef9bfd2..d39f1cfb04d 100644 --- a/libjava/classpath/java/util/regex/Pattern.java +++ b/libjava/classpath/java/util/regex/Pattern.java @@ -103,8 +103,11 @@ public final class Pattern implements Serializable } catch (REException e) { - throw new PatternSyntaxException(e.getMessage(), + PatternSyntaxException pse; + pse = new PatternSyntaxException(e.getMessage(), regex, e.getPosition()); + pse.initCause(e); + throw pse; } } |