diff options
author | Mark Wielaard <mark@gcc.gnu.org> | 2005-11-15 23:20:01 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2005-11-15 23:20:01 +0000 |
commit | 8f523f3a1047919d3563daf1ef47ba87336ebe89 (patch) | |
tree | a5eb7cf42a51869cc8aa1fad7ad6a90cca47fdd8 /libjava/classpath/java/lang | |
parent | 02e549bfaaec38f68307e7f34e46ea57ea1809af (diff) | |
download | gcc-8f523f3a1047919d3563daf1ef47ba87336ebe89.zip gcc-8f523f3a1047919d3563daf1ef47ba87336ebe89.tar.gz gcc-8f523f3a1047919d3563daf1ef47ba87336ebe89.tar.bz2 |
Imported GNU Classpath 0.19 + gcj-import-20051115.
* sources.am: Regenerated.
* Makefile.in: Likewise.
* scripts/makemake.tcl: Use glob -nocomplain.
From-SVN: r107049
Diffstat (limited to 'libjava/classpath/java/lang')
28 files changed, 1273 insertions, 78 deletions
diff --git a/libjava/classpath/java/lang/Boolean.java b/libjava/classpath/java/lang/Boolean.java index b691028..902c93b 100644 --- a/libjava/classpath/java/lang/Boolean.java +++ b/libjava/classpath/java/lang/Boolean.java @@ -221,4 +221,36 @@ public final class Boolean implements Serializable return false; return "true".equalsIgnoreCase(System.getProperty(name)); } + + /** + * If the String argument is "true", ignoring case, return true. + * Otherwise, return false. + * + * @param b String to parse + * @since 1.5 + */ + public static boolean parseBoolean(String b) + { + return "true".equalsIgnoreCase(b) ? true : false; + } + + /** + * Compares this Boolean to another. + * @param b the Boolean to compare this Boolean to + * @return 0 if both Booleans represent the same value, a positive number + * if this Boolean represents true and b represents false, or a negative + * number otherwise. + * @since 1.5 + */ + public int compareTo (Boolean b) + { + if (b == null) + throw new NullPointerException("argument passed to compareTo(Boolean) cannot be null"); + + if (this.value == b.value) + return 0; + if (this.value == true) + return 1; + return -1; + } } diff --git a/libjava/classpath/java/lang/Byte.java b/libjava/classpath/java/lang/Byte.java index 338e216..2560bfc 100644 --- a/libjava/classpath/java/lang/Byte.java +++ b/libjava/classpath/java/lang/Byte.java @@ -50,7 +50,7 @@ package java.lang; * @author Per Bothner * @author Eric Blake (ebb9@email.byu.edu) * @since 1.1 - * @status updated to 1.4 + * @status updated to 1.5 */ public final class Byte extends Number implements Comparable { @@ -78,6 +78,16 @@ public final class Byte extends Number implements Comparable public static final Class TYPE = VMClassLoader.getPrimitiveClass('B'); /** + * The number of bits needed to represent a <code>byte</code>. + * @since 1.5 + */ + public static final int SIZE = 8; + + // This caches Byte values, and is used by boxing conversions via + // valueOf(). We're required to cache all possible values here. + private static Byte[] byteCache = new Byte[MAX_VALUE - MIN_VALUE + 1]; + + /** * The immutable value of this Byte. * * @serial the wrapped byte @@ -192,6 +202,26 @@ public final class Byte extends Number implements Comparable } /** + * Returns a <code>Byte</code> object wrapping the value. + * In contrast to the <code>Byte</code> constructor, this method + * will cache some values. It is used by boxing conversion. + * + * @param val the value to wrap + * @return the <code>Byte</code> + * + * @since 1.5 + */ + public static Byte valueOf(byte val) + { + synchronized (byteCache) + { + if (byteCache[val - MIN_VALUE] == null) + byteCache[val - MIN_VALUE] = new Byte(val); + return byteCache[val - MIN_VALUE]; + } + } + + /** * Convert the specified <code>String</code> into a <code>Byte</code>. * The <code>String</code> may represent decimal, hexadecimal, or * octal numbers. diff --git a/libjava/classpath/java/lang/Character.java b/libjava/classpath/java/lang/Character.java index 1e4f219..78db41e 100644 --- a/libjava/classpath/java/lang/Character.java +++ b/libjava/classpath/java/lang/Character.java @@ -1,5 +1,5 @@ /* java.lang.Character -- Wrapper class for char, and Unicode subsets - Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc. + Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -1034,6 +1034,18 @@ public final class Character implements Serializable, Comparable public static final Class TYPE = VMClassLoader.getPrimitiveClass('C'); /** + * The number of bits needed to represent a <code>char</code>. + * @since 1.5 + */ + public static final int SIZE = 16; + + // This caches some Character values, and is used by boxing + // conversions via valueOf(). We must cache at least 0..127; + // this constant controls how much we actually cache. + private static final int MAX_CACHE = 127; + private static Character[] charCache = new Character[MAX_CACHE + 1]; + + /** * Lu = Letter, Uppercase (Informative). * * @since 1.1 @@ -1480,34 +1492,48 @@ public final class Character implements Serializable, Comparable /** - * Minimum high surrrogate code in UTF-16 encoding. + * Minimum high surrogate code in UTF-16 encoding. * * @since 1.5 */ public static final char MIN_HIGH_SURROGATE = '\ud800'; /** - * Maximum high surrrogate code in UTF-16 encoding. + * Maximum high surrogate code in UTF-16 encoding. * * @since 1.5 */ public static final char MAX_HIGH_SURROGATE = '\udbff'; /** - * Minimum low surrrogate code in UTF-16 encoding. + * Minimum low surrogate code in UTF-16 encoding. * * @since 1.5 */ public static final char MIN_LOW_SURROGATE = '\udc00'; /** - * Maximum low surrrogate code in UTF-16 encoding. + * Maximum low surrogate code in UTF-16 encoding. * * @since 1.5 */ public static final char MAX_LOW_SURROGATE = '\udfff'; /** + * Minimum surrogate code in UTF-16 encoding. + * + * @since 1.5 + */ + public static final char MIN_SURROGATE = MIN_HIGH_SURROGATE; + + /** + * Maximum low surrogate code in UTF-16 encoding. + * + * @since 1.5 + */ + public static final char MAX_SURROGATE = MAX_LOW_SURROGATE; + + /** * Grabs an attribute offset from the Unicode attribute database. The lower * 5 bits are the character type, the next 2 bits are flags, and the top * 9 bits are the offset into the attribute tables. @@ -2303,6 +2329,37 @@ public final class Character implements Serializable, Comparable } /** + * Returns an <code>Character</code> object wrapping the value. + * In contrast to the <code>Character</code> constructor, this method + * will cache some values. It is used by boxing conversion. + * + * @param val the value to wrap + * @return the <code>Character</code> + * + * @since 1.5 + */ + public static Character valueOf(char val) + { + if (val > MAX_CACHE) + return new Character(val); + synchronized (charCache) + { + if (charCache[val - MIN_VALUE] == null) + charCache[val - MIN_VALUE] = new Character(val); + return charCache[val - MIN_VALUE]; + } + } + + /** + * Reverse the bytes in val. + * @since 1.5 + */ + public static char reverseBytes(char val) + { + return (char) (((val >> 8) & 0xff) | ((val << 8) & 0xff00)); + } + + /** * Converts a unicode code point to a UTF-16 representation of that * code point. * @@ -2370,7 +2427,7 @@ public final class Character implements Serializable, Comparable * Return number of 16-bit characters required to represent the given * code point. * - * @param codePoint a uncode code point + * @param codePoint a unicode code point * * @return 2 if codePoint >= 0x10000, 1 otherwise. * @@ -2415,4 +2472,210 @@ public final class Character implements Serializable, Comparable { return codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT; } + + /** + * Return true if the given character is a high surrogate. + * @param ch the character + * @return true if the character is a high surrogate character + * + * @since 1.5 + */ + public static boolean isHighSurrogate(char ch) + { + return ch >= MIN_HIGH_SURROGATE && ch <= MAX_HIGH_SURROGATE; + } + + /** + * Return true if the given character is a low surrogate. + * @param ch the character + * @return true if the character is a low surrogate character + * + * @since 1.5 + */ + public static boolean isLowSurrogate(char ch) + { + return ch >= MIN_LOW_SURROGATE && ch <= MAX_LOW_SURROGATE; + } + + /** + * Return true if the given characters compose a surrogate pair. + * This is true if the first character is a high surrogate and the + * second character is a low surrogate. + * @param ch1 the first character + * @param ch2 the first character + * @return true if the characters compose a surrogate pair + * + * @since 1.5 + */ + public static boolean isSurrogatePair(char ch1, char ch2) + { + return isHighSurrogate(ch1) && isLowSurrogate(ch2); + } + + /** + * Given a valid surrogate pair, this returns the corresponding + * code point. + * @param high the high character of the pair + * @param low the low character of the pair + * @return the corresponding code point + * + * @since 1.5 + */ + public static int toCodePoint(char high, char low) + { + return ((high - MIN_HIGH_SURROGATE) << 10) + (low - MIN_LOW_SURROGATE); + } + + /** + * Get the code point at the specified index in the CharSequence. + * This is like CharSequence#charAt(int), but if the character is + * the start of a surrogate pair, and there is a following + * character, and this character completes the pair, then the + * corresponding supplementary code point is returned. Otherwise, + * the character at the index is returned. + * + * @param sequence the CharSequence + * @param index the index of the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public static int codePointAt(CharSequence sequence, int index) + { + int len = sequence.length(); + if (index < 0 || index >= len) + throw new IndexOutOfBoundsException(); + char high = sequence.charAt(index); + if (! isHighSurrogate(high) || ++index >= len) + return high; + char low = sequence.charAt(index); + if (! isLowSurrogate(low)) + return high; + return toCodePoint(high, low); + } + + /** + * Get the code point at the specified index in the CharSequence. + * If the character is the start of a surrogate pair, and there is a + * following character, and this character completes the pair, then + * the corresponding supplementary code point is returned. + * Otherwise, the character at the index is returned. + * + * @param chars the character array in which to look + * @param index the index of the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public static int codePointAt(char[] chars, int index) + { + return codePointAt(chars, index, chars.length); + } + + /** + * Get the code point at the specified index in the CharSequence. + * If the character is the start of a surrogate pair, and there is a + * following character within the specified range, and this + * character completes the pair, then the corresponding + * supplementary code point is returned. Otherwise, the character + * at the index is returned. + * + * @param chars the character array in which to look + * @param index the index of the codepoint to get, starting at 0 + * @param limit the limit past which characters should not be examined + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= + * limit, or if limit is negative or >= the length of the array + * @since 1.5 + */ + public static int codePointAt(char[] chars, int index, int limit) + { + if (index < 0 || index >= limit || limit < 0 || limit >= chars.length) + throw new IndexOutOfBoundsException(); + char high = chars[index]; + if (! isHighSurrogate(high) || ++index >= limit) + return high; + char low = chars[index]; + if (! isLowSurrogate(low)) + return high; + return toCodePoint(high, low); + } + + /** + * Get the code point before the specified index. This is like + * #codePointAt(char[], int), but checks the characters at + * <code>index-1</code> and <code>index-2</code> to see if they form + * a supplementary code point. If they do not, the character at + * <code>index-1</code> is returned. + * + * @param chars the character array + * @param index the index just past the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public static int codePointBefore(char[] chars, int index) + { + return codePointBefore(chars, index, 1); + } + + /** + * Get the code point before the specified index. This is like + * #codePointAt(char[], int), but checks the characters at + * <code>index-1</code> and <code>index-2</code> to see if they form + * a supplementary code point. If they do not, the character at + * <code>index-1</code> is returned. The start parameter is used to + * limit the range of the array which may be examined. + * + * @param chars the character array + * @param index the index just past the codepoint to get, starting at 0 + * @param start the index before which characters should not be examined + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is > start or > + * the length of the array, or if limit is negative or >= the + * length of the array + * @since 1.5 + */ + public static int codePointBefore(char[] chars, int index, int start) + { + if (index < start || index > chars.length + || start < 0 || start >= chars.length) + throw new IndexOutOfBoundsException(); + --index; + char low = chars[index]; + if (! isLowSurrogate(low) || --index < start) + return low; + char high = chars[index]; + if (! isHighSurrogate(high)) + return low; + return toCodePoint(high, low); + } + + /** + * Get the code point before the specified index. This is like + * #codePointAt(CharSequence, int), but checks the characters at + * <code>index-1</code> and <code>index-2</code> to see if they form + * a supplementary code point. If they do not, the character at + * <code>index-1</code> is returned. + * + * @param sequence the CharSequence + * @param index the index just past the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public static int codePointBefore(CharSequence sequence, int index) + { + int len = sequence.length(); + if (index < 1 || index > len) + throw new IndexOutOfBoundsException(); + --index; + char low = sequence.charAt(index); + if (! isLowSurrogate(low) || --index < 0) + return low; + char high = sequence.charAt(index); + if (! isHighSurrogate(high)) + return low; + return toCodePoint(high, low); + } } // class Character diff --git a/libjava/classpath/java/lang/Class.java b/libjava/classpath/java/lang/Class.java index 22f148e..726c794 100644 --- a/libjava/classpath/java/lang/Class.java +++ b/libjava/classpath/java/lang/Class.java @@ -41,7 +41,9 @@ package java.lang; import gnu.classpath.VMStackWalker; import java.io.InputStream; +import java.io.ObjectStreamClass; import java.io.Serializable; +import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -99,7 +101,7 @@ public final class Class implements Serializable /** The class signers. */ private Object[] signers = null; /** The class protection domain. */ - private final ProtectionDomain pd; + private final transient ProtectionDomain pd; /* We use an inner class, so that Class doesn't have a static initializer */ private static final class StaticData @@ -592,7 +594,8 @@ public final class Class implements Serializable ClassLoader cl = getClassLoader(); if (cl != null) return cl.getPackage(getPackagePortion(getName())); - return null; + else + return VMClassLoader.getPackage(getPackagePortion(getName())); } /** @@ -721,7 +724,7 @@ public final class Class implements Serializable * @param list List of methods to search * @param name Name of method * @param args Method parameter types - * @see #getMethod() + * @see #getMethod(String, Class[]) */ private static Method matchMethod(Method[] list, String name, Class[] args) { @@ -829,7 +832,7 @@ public final class Class implements Serializable * public and final, but not an interface. * * @return the modifiers of this class - * @see Modifer + * @see Modifier * @since 1.1 */ public int getModifiers() diff --git a/libjava/classpath/java/lang/ClassLoader.java b/libjava/classpath/java/lang/ClassLoader.java index 0d50a6e..9f586c4 100644 --- a/libjava/classpath/java/lang/ClassLoader.java +++ b/libjava/classpath/java/lang/ClassLoader.java @@ -49,6 +49,7 @@ import java.io.InputStream; import java.lang.reflect.Constructor; import java.net.URL; import java.net.URLClassLoader; +import java.nio.ByteBuffer; import java.security.CodeSource; import java.security.PermissionCollection; import java.security.Policy; @@ -472,6 +473,35 @@ public abstract class ClassLoader } /** + * Helper to define a class using the contents of a byte buffer. If + * the domain is null, the default of + * <code>Policy.getPolicy().getPermissions(new CodeSource(null, + * null))</code> is used. Once a class has been defined in a + * package, all further classes in that package must have the same + * set of certificates or a SecurityException is thrown. + * + * @param name the name to give the class. null if unknown + * @param buf a byte buffer containing bytes that form a class. + * @param domain the ProtectionDomain to give to the class, null for the + * default protection domain + * @return the class that was defined + * @throws ClassFormatError if data is not in proper classfile format + * @throws NoClassDefFoundError if the supplied name is not the same as + * the one specified by the byte buffer. + * @throws SecurityException if name starts with "java.", or if certificates + * do not match up + * @since 1.5 + */ + protected final Class defineClass(String name, ByteBuffer buf, + ProtectionDomain domain) + throws ClassFormatError + { + byte[] data = new byte[buf.remaining()]; + buf.get(data); + return defineClass(name, data, 0, data.length, domain); + } + + /** * Links the class, if that has not already been done. Linking basically * resolves all references to other classes made by this class. * @@ -883,7 +913,7 @@ public abstract class ClassLoader * * @param name the (system specific) name of the requested library * @return the full pathname to the requested library, or null - * @see Runtime#loadLibrary() + * @see Runtime#loadLibrary(String) * @since 1.2 */ protected String findLibrary(String name) @@ -913,7 +943,7 @@ public abstract class ClassLoader * * @param name the package (and subpackages) to affect * @param enabled true to set the default to enabled - * @see #setDefaultAssertionStatus(String, boolean) + * @see #setDefaultAssertionStatus(boolean) * @see #setClassAssertionStatus(String, boolean) * @see #clearAssertionStatus() * @since 1.4 @@ -934,7 +964,7 @@ public abstract class ClassLoader * @param name the class to affect * @param enabled true to set the default to enabled * @throws NullPointerException if name is null - * @see #setDefaultAssertionStatus(String, boolean) + * @see #setDefaultAssertionStatus(boolean) * @see #setPackageAssertionStatus(String, boolean) * @see #clearAssertionStatus() * @since 1.4 diff --git a/libjava/classpath/java/lang/Double.java b/libjava/classpath/java/lang/Double.java index 4fa47f4..26b398b 100644 --- a/libjava/classpath/java/lang/Double.java +++ b/libjava/classpath/java/lang/Double.java @@ -38,7 +38,6 @@ exception statement from your version. */ package java.lang; -import gnu.classpath.Configuration; /** * Instances of class <code>Double</code> represent primitive @@ -89,6 +88,12 @@ public final class Double extends Number implements Comparable public static final double NaN = 0.0 / 0.0; /** + * The number of bits needed to represent a <code>double</code>. + * @since 1.5 + */ + public static final int SIZE = 64; + + /** * The primitive type <code>double</code> is represented by this * <code>Class</code> object. * @since 1.1 @@ -168,6 +173,22 @@ public final class Double extends Number implements Comparable } /** + * Returns a <code>Double</code> object wrapping the value. + * In contrast to the <code>Double</code> constructor, this method + * may cache some values. It is used by boxing conversion. + * + * @param val the value to wrap + * @return the <code>Double</code> + * + * @since 1.5 + */ + public static Double valueOf(double val) + { + // We don't actually cache, but we could. + return new Double(val); + } + + /** * Create a new <code>Double</code> object using the <code>String</code>. * * @param s the <code>String</code> to convert diff --git a/libjava/classpath/java/lang/EnumConstantNotPresentException.java b/libjava/classpath/java/lang/EnumConstantNotPresentException.java new file mode 100644 index 0000000..dbec9d6 --- /dev/null +++ b/libjava/classpath/java/lang/EnumConstantNotPresentException.java @@ -0,0 +1,93 @@ +/* EnumConstantNotPresentException.java -- thrown when enum constant + not available + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package java.lang; + +/** + * An exception of this type is thrown when a symbolic reference is + * made to an enum constant which does not exist. + * + * @author Tom Tromey (tromey@redhat.com) + * @since 1.5 + */ +public class EnumConstantNotPresentException extends RuntimeException +{ + /** + * The enum's type. Note that the name is fixed by the + * serialization spec. + */ + private Class enumType; + + /** + * The name of the missing enum constant. Note that the name is + * fixed by the serialization spec. + */ + private String constantName; + + /** + * Create a new EnumConstantNotPresentException with the indicated + * enum type and enum constant name. + * @param theEnum the enum's class + * @param name the name of the missing enum constant + */ + public EnumConstantNotPresentException(Class theEnum, String name) + { + super("enum " + theEnum + " is missing the constant " + name); + enumType = theEnum; + constantName = name; + } + + /** + * Return the name of the missing constant. + * @return the name of the missing constant + */ + public String constantName() + { + return constantName; + } + + /** + * Return the enum type which is missing a constant. + * @return the enum type which is missing a constant + */ + public Class enumType() + { + return enumType; + } +} diff --git a/libjava/classpath/java/lang/Float.java b/libjava/classpath/java/lang/Float.java index e6200da..eef34a0 100644 --- a/libjava/classpath/java/lang/Float.java +++ b/libjava/classpath/java/lang/Float.java @@ -94,6 +94,12 @@ public final class Float extends Number implements Comparable public static final Class TYPE = VMClassLoader.getPrimitiveClass('F'); /** + * The number of bits needed to represent a <code>float</code>. + * @since 1.5 + */ + public static final int SIZE = 32; + + /** * The immutable value of this Float. * * @serial the wrapped float @@ -192,6 +198,22 @@ public final class Float extends Number implements Comparable } /** + * Returns a <code>Float</code> object wrapping the value. + * In contrast to the <code>Float</code> constructor, this method + * may cache some values. It is used by boxing conversion. + * + * @param val the value to wrap + * @return the <code>Float</code> + * + * @since 1.5 + */ + public static Float valueOf(float val) + { + // We don't actually cache, but we could. + return new Float(val); + } + + /** * Parse the specified <code>String</code> as a <code>float</code>. The * extended BNF grammar is as follows:<br> * <pre> diff --git a/libjava/classpath/java/lang/Integer.java b/libjava/classpath/java/lang/Integer.java index 53de9ab..f3fe85f 100644 --- a/libjava/classpath/java/lang/Integer.java +++ b/libjava/classpath/java/lang/Integer.java @@ -707,8 +707,8 @@ public final class Integer extends Number implements Comparable * @throws NullPointerException if decode is true and str if null * @see #parseInt(String, int) * @see #decode(String) - * @see Byte#parseInt(String, int) - * @see Short#parseInt(String, int) + * @see Byte#parseByte(String, int) + * @see Short#parseShort(String, int) */ static int parseInt(String str, int radix, boolean decode) { diff --git a/libjava/classpath/java/lang/Long.java b/libjava/classpath/java/lang/Long.java index 703eab8..74e2a52 100644 --- a/libjava/classpath/java/lang/Long.java +++ b/libjava/classpath/java/lang/Long.java @@ -50,7 +50,7 @@ package java.lang; * @author Warren Levy * @author Eric Blake (ebb9@email.byu.edu) * @since 1.0 - * @status updated to 1.4 + * @status updated to 1.5 */ public final class Long extends Number implements Comparable { @@ -79,6 +79,12 @@ public final class Long extends Number implements Comparable public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J'); /** + * The number of bits needed to represent a <code>long</code>. + * @since 1.5 + */ + public static final int SIZE = 64; + + /** * The immutable value of this Long. * * @serial the wrapped long @@ -282,6 +288,21 @@ public final class Long extends Number implements Comparable } /** + * Returns a <code>Long</code> object wrapping the value. + * + * @param val the value to wrap + * @return the <code>Long</code> + * + * @since 1.5 + */ + public static synchronized Long valueOf(long val) + { + // We aren't required to cache here. We could, though perhaps we + // ought to consider that as an empirical question. + return new Long(val); + } + + /** * Convert the specified <code>String</code> into a <code>Long</code>. * The <code>String</code> may represent decimal, hexadecimal, or * octal numbers. @@ -512,6 +533,136 @@ public final class Long extends Number implements Comparable } /** + * Return the number of bits set in x. + * @param x value to examine + * @since 1.5 + */ + public static int bitCount(long x) + { + // Successively collapse alternating bit groups into a sum. + x = ((x >> 1) & 0x5555555555555555L) + (x & 0x5555555555555555L); + x = ((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L); + int v = (int) ((x >>> 32) + x); + v = ((v >> 4) & 0x0f0f0f0f) + (v & 0x0f0f0f0f); + v = ((v >> 8) & 0x00ff00ff) + (v & 0x00ff00ff); + return ((v >> 16) & 0x0000ffff) + (v & 0x0000ffff); + } + + /** + * Rotate x to the left by distance bits. + * @param x the value to rotate + * @param distance the number of bits by which to rotate + * @since 1.5 + */ + public static long rotateLeft(long x, int distance) + { + // This trick works because the shift operators implicitly mask + // the shift count. + return (x << distance) | (x >>> - distance); + } + + /** + * Rotate x to the right by distance bits. + * @param x the value to rotate + * @param distance the number of bits by which to rotate + * @since 1.5 + */ + public static long rotateRight(long x, int distance) + { + // This trick works because the shift operators implicitly mask + // the shift count. + return (x << - distance) | (x >>> distance); + } + + /** + * Find the highest set bit in value, and return a new value + * with only that bit set. + * @param value the value to examine + * @since 1.5 + */ + public static long highestOneBit(long value) + { + value |= value >>> 1; + value |= value >>> 2; + value |= value >>> 4; + value |= value >>> 8; + value |= value >>> 16; + value |= value >>> 32; + return value ^ (value >>> 1); + } + + /** + * Return the number of leading zeros in value. + * @param value the value to examine + * @since 1.5 + */ + public static int numberOfLeadingZeros(long value) + { + value |= value >>> 1; + value |= value >>> 2; + value |= value >>> 4; + value |= value >>> 8; + value |= value >>> 16; + value |= value >>> 32; + return bitCount(~value); + } + + /** + * Find the lowest set bit in value, and return a new value + * with only that bit set. + * @param value the value to examine + * @since 1.5 + */ + public static long lowestOneBit(long value) + { + // Classic assembly trick. + return value & - value; + } + + /** + * Find the number of trailing zeros in value. + * @param value the value to examine + * @since 1.5 + */ + public static int numberOfTrailingZeros(long value) + { + return bitCount((value & -value) - 1); + } + + /** + * Return 1 if x is positive, -1 if it is negative, and 0 if it is + * zero. + * @param x the value to examine + * @since 1.5 + */ + public static int signum(long x) + { + return x < 0 ? -1 : (x > 0 ? 1 : 0); + } + + /** + * Reverse the bytes in val. + * @since 1.5 + */ + public static long reverseBytes(long val) + { + int hi = Integer.reverseBytes((int) val); + int lo = Integer.reverseBytes((int) (val >>> 32)); + return (((long) hi) << 32) | lo; + } + + /** + * Reverse the bits in val. + * @since 1.5 + */ + public static long reverse(long val) + { + long hi = Integer.reverse((int) val) & 0xffffffffL; + long lo = Integer.reverse((int) (val >>> 32)) & 0xffffffffL; + return (hi << 32) | lo; + } + + /** * Helper for converting unsigned numbers to String. * * @param num the number diff --git a/libjava/classpath/java/lang/Object.java b/libjava/classpath/java/lang/Object.java index f8c389a..6212d7d 100644 --- a/libjava/classpath/java/lang/Object.java +++ b/libjava/classpath/java/lang/Object.java @@ -343,7 +343,7 @@ public class Object * * <p>This thread still holds a lock on the object, so it is * typical to release the lock by exiting the synchronized - * code, calling wait(), or calling {@link Thread#sleep()}, so + * code, calling wait(), or calling {@link Thread#sleep(long)}, so * that the newly awakened thread can actually resume. The * awakened thread will most likely be awakened with an * {@link InterruptedException}, but that is not guaranteed. @@ -372,7 +372,7 @@ public class Object * * <p>This thread still holds a lock on the object, so it is * typical to release the lock by exiting the synchronized - * code, calling wait(), or calling {@link Thread#sleep()}, so + * code, calling wait(), or calling {@link Thread#sleep(long)}, so * that one of the newly awakened threads can actually resume. * The resuming thread will most likely be awakened with an * {@link InterruptedException}, but that is not guaranteed. diff --git a/libjava/classpath/java/lang/Process.java b/libjava/classpath/java/lang/Process.java index b6e18ca..ccaa3f1 100644 --- a/libjava/classpath/java/lang/Process.java +++ b/libjava/classpath/java/lang/Process.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.lang; +import java.io.File; import java.io.InputStream; import java.io.OutputStream; diff --git a/libjava/classpath/java/lang/Readable.java b/libjava/classpath/java/lang/Readable.java index efc1985..d896765 100644 --- a/libjava/classpath/java/lang/Readable.java +++ b/libjava/classpath/java/lang/Readable.java @@ -39,6 +39,7 @@ package java.lang; import java.io.IOException; import java.nio.CharBuffer; +import java.nio.ReadOnlyBufferException; /** * A <code>Readable</code> object is simply a source for Unicode character diff --git a/libjava/classpath/java/lang/RuntimePermission.java b/libjava/classpath/java/lang/RuntimePermission.java index ca33307..2f80b91 100644 --- a/libjava/classpath/java/lang/RuntimePermission.java +++ b/libjava/classpath/java/lang/RuntimePermission.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.lang; import java.security.BasicPermission; +import java.security.Permission; /** * A <code>RuntimePermission</code> contains a permission name, but no diff --git a/libjava/classpath/java/lang/SecurityManager.java b/libjava/classpath/java/lang/SecurityManager.java index ef9e7597..26d56a6 100644 --- a/libjava/classpath/java/lang/SecurityManager.java +++ b/libjava/classpath/java/lang/SecurityManager.java @@ -41,19 +41,35 @@ package java.lang; import gnu.classpath.VMStackWalker; import java.awt.AWTPermission; +import java.awt.Frame; +import java.awt.Toolkit; +import java.awt.Window; import java.io.File; import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.FilePermission; +import java.io.RandomAccessFile; import java.lang.reflect.Member; import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketImplFactory; import java.net.SocketPermission; +import java.net.URL; +import java.net.URLStreamHandlerFactory; import java.security.AccessControlContext; +import java.security.AccessControlException; import java.security.AccessController; import java.security.AllPermission; +import java.security.BasicPermission; import java.security.Permission; +import java.security.Policy; import java.security.PrivilegedAction; +import java.security.ProtectionDomain; import java.security.Security; import java.security.SecurityPermission; +import java.util.Properties; import java.util.PropertyPermission; import java.util.StringTokenizer; @@ -196,7 +212,7 @@ public class SecurityManager * <ul> * <li>All methods on the stack are from system classes</li> * <li>All methods on the stack up to the first "privileged" caller, as - * created by {@link AccessController.doPrivileged(PrivilegedAction)}, + * created by {@link AccessController#doPrivileged(PrivilegedAction)}, * are from system classes</li> * <li>A check of <code>java.security.AllPermission</code> succeeds.</li> * </ul> @@ -219,7 +235,7 @@ public class SecurityManager * <ul> * <li>All methods on the stack are from system classes</li> * <li>All methods on the stack up to the first "privileged" caller, as - * created by {@link AccessController.doPrivileged(PrivilegedAction)}, + * created by {@link AccessController#doPrivileged(PrivilegedAction)}, * are from system classes</li> * <li>A check of <code>java.security.AllPermission</code> succeeds.</li> * </ul> @@ -258,7 +274,7 @@ public class SecurityManager * <ul> * <li>All methods on the stack are from system classes</li> * <li>All methods on the stack up to the first "privileged" caller, as - * created by {@link AccessController.doPrivileged(PrivilegedAction)}, + * created by {@link AccessController#doPrivileged(PrivilegedAction)}, * are from system classes</li> * <li>A check of <code>java.security.AllPermission</code> succeeds.</li> * </ul> @@ -431,7 +447,7 @@ public class SecurityManager * @throws SecurityException if permission is denied * @throws NullPointerException if g is null * @see Thread#Thread() - * @see ThreadGroup#ThreadGroup() + * @see ThreadGroup#ThreadGroup(String) * @see ThreadGroup#stop() * @see ThreadGroup#suspend() * @see ThreadGroup#resume() @@ -537,7 +553,7 @@ public class SecurityManager * @throws NullPointerException if filename is null * @see File * @see FileInputStream#FileInputStream(String) - * @see RandomAccessFile#RandomAccessFile(String) + * @see RandomAccessFile#RandomAccessFile(String, String) */ public void checkRead(String filename) { @@ -602,9 +618,9 @@ public class SecurityManager * @see File * @see File#canWrite() * @see File#mkdir() - * @see File#renameTo() + * @see File#renameTo(File) * @see FileOutputStream#FileOutputStream(String) - * @see RandomAccessFile#RandomAccessFile(String) + * @see RandomAccessFile#RandomAccessFile(String, String) */ public void checkWrite(String filename) { diff --git a/libjava/classpath/java/lang/Short.java b/libjava/classpath/java/lang/Short.java index fbeea91..eb40cd9 100644 --- a/libjava/classpath/java/lang/Short.java +++ b/libjava/classpath/java/lang/Short.java @@ -77,6 +77,19 @@ public final class Short extends Number implements Comparable public static final Class TYPE = VMClassLoader.getPrimitiveClass('S'); /** + * The number of bits needed to represent a <code>short</code>. + * @since 1.5 + */ + public static final int SIZE = 16; + + // This caches some Short values, and is used by boxing conversions + // via valueOf(). We must cache at least -128..127; these constants + // control how much we actually cache. + private static final int MIN_CACHE = -128; + private static final int MAX_CACHE = 127; + private static Short[] shortCache = new Short[MAX_CACHE - MIN_CACHE + 1]; + + /** * The immutable value of this Short. * * @serial the wrapped short @@ -189,6 +202,28 @@ public final class Short extends Number implements Comparable } /** + * Returns a <code>Short</code> object wrapping the value. + * In contrast to the <code>Short</code> constructor, this method + * will cache some values. It is used by boxing conversion. + * + * @param val the value to wrap + * @return the <code>Short</code> + * + * @since 1.5 + */ + public static Short valueOf(short val) + { + if (val < MIN_CACHE || val > MAX_CACHE) + return new Short(val); + synchronized (shortCache) + { + if (shortCache[val - MIN_CACHE] == null) + shortCache[val - MIN_CACHE] = new Short(val); + return shortCache[val - MIN_CACHE]; + } + } + + /** * Convert the specified <code>String</code> into a <code>Short</code>. * The <code>String</code> may represent decimal, hexadecimal, or * octal numbers. @@ -350,4 +385,13 @@ public final class Short extends Number implements Comparable { return compareTo((Short)o); } + + /** + * Reverse the bytes in val. + * @since 1.5 + */ + public static short reverseBytes(short val) + { + return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00)); + } } diff --git a/libjava/classpath/java/lang/StrictMath.java b/libjava/classpath/java/lang/StrictMath.java index 32bd354..2079cc1 100644 --- a/libjava/classpath/java/lang/StrictMath.java +++ b/libjava/classpath/java/lang/StrictMath.java @@ -1254,7 +1254,7 @@ public final strictfp class StrictMath /** * Super precision for 2/pi in 24-bit chunks, for use in - * {@link #remPiOver2()}. + * {@link #remPiOver2(double, double[])}. */ private static final int TWO_OVER_PI[] = { 0xa2f983, 0x6e4e44, 0x1529fc, 0x2757d1, 0xf534dd, 0xc0db62, @@ -1272,7 +1272,7 @@ public final strictfp class StrictMath /** * Super precision for pi/2 in 24-bit chunks, for use in - * {@link #remPiOver2()}. + * {@link #remPiOver2(double, double[])}. */ private static final double PI_OVER_TWO[] = { 1.570796251296997, // Long bits 0x3ff921fb40000000L. @@ -1286,8 +1286,8 @@ public final strictfp class StrictMath }; /** - * More constants related to pi, used in {@link #remPiOver2()} and - * elsewhere. + * More constants related to pi, used in + * {@link #remPiOver2(double, double[])} and elsewhere. */ private static final double PI_L = 1.2246467991473532e-16, // Long bits 0x3ca1a62633145c07L. @@ -1301,7 +1301,7 @@ public final strictfp class StrictMath /** * Natural log and square root constants, for calculation of * {@link #exp(double)}, {@link #log(double)} and - * {@link #power(double, double)}. CP is 2/(3*ln(2)). + * {@link #pow(double, double)}. CP is 2/(3*ln(2)). */ private static final double SQRT_1_5 = 1.224744871391589, // Long bits 0x3ff3988e1409212eL. diff --git a/libjava/classpath/java/lang/String.java b/libjava/classpath/java/lang/String.java index b4db850..369d808 100644 --- a/libjava/classpath/java/lang/String.java +++ b/libjava/classpath/java/lang/String.java @@ -98,7 +98,7 @@ public final class String implements Serializable, Comparable, CharSequence /** * Stores unicode multi-character uppercase expansion table. - * @see #toUpperCase(char) + * @see #toUpperCase(Locale) * @see CharData#UPPER_EXPAND */ private static final char[] upperExpand @@ -139,7 +139,7 @@ public final class String implements Serializable, Comparable, CharSequence final int offset; /** - * An implementation for {@link CASE_INSENSITIVE_ORDER}. + * An implementation for {@link #CASE_INSENSITIVE_ORDER}. * This must be {@link Serializable}. The class name is dictated by * compatibility with Sun's JDK. */ @@ -233,6 +233,7 @@ public final class String implements Serializable, Comparable, CharSequence * @param count the number of characters from data to copy * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 + * || offset + count < 0 (overflow) * || offset + count > data.length) * (while unspecified, this is a StringIndexOutOfBoundsException) */ @@ -256,6 +257,7 @@ public final class String implements Serializable, Comparable, CharSequence * @param count the number of characters from ascii to copy * @throws NullPointerException if ascii is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 + * || offset + count < 0 (overflow) * || offset + count > ascii.length) * (while unspecified, this is a StringIndexOutOfBoundsException) * @see #String(byte[]) @@ -267,8 +269,13 @@ public final class String implements Serializable, Comparable, CharSequence */ public String(byte[] ascii, int hibyte, int offset, int count) { - if (offset < 0 || count < 0 || offset + count > ascii.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > ascii.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); value = new char[count]; this.offset = 0; this.count = count; @@ -327,8 +334,13 @@ public final class String implements Serializable, Comparable, CharSequence public String(byte[] data, int offset, int count, String encoding) throws UnsupportedEncodingException { - if (offset < 0 || count < 0 || offset + count > data.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > data.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); try { CharsetDecoder csd = Charset.forName(encoding).newDecoder(); @@ -402,8 +414,13 @@ public final class String implements Serializable, Comparable, CharSequence */ public String(byte[] data, int offset, int count) { - if (offset < 0 || count < 0 || offset + count > data.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > data.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); int o, c; char[] v; String encoding; @@ -512,8 +529,13 @@ public final class String implements Serializable, Comparable, CharSequence */ String(char[] data, int offset, int count, boolean dont_copy) { - if (offset < 0 || count < 0 || offset + count > data.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > data.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); if (dont_copy) { value = data; @@ -554,6 +576,40 @@ public final class String implements Serializable, Comparable, CharSequence } /** + * Get the code point at the specified index. This is like #charAt(int), + * but if the character is the start of a surrogate pair, and the + * following character completes the pair, then the corresponding + * supplementary code point is returned. + * @param index the index of the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public synchronized int codePointAt(int index) + { + // Use the CharSequence overload as we get better range checking + // this way. + return Character.codePointAt(this, index); + } + + /** + * Get the code point before the specified index. This is like + * #codePointAt(int), but checks the characters at <code>index-1</code> and + * <code>index-2</code> to see if they form a supplementary code point. + * @param index the index just past the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * (while unspecified, this is a StringIndexOutOfBoundsException) + * @since 1.5 + */ + public synchronized int codePointBefore(int index) + { + // Use the CharSequence overload as we get better range checking + // this way. + return Character.codePointBefore(this, index); + } + + /** * Copies characters from this String starting at a specified start index, * ending at a specified stop index, to a character array starting at * a specified destination begin index. @@ -628,21 +684,26 @@ public final class String implements Serializable, Comparable, CharSequence ByteBuffer bbuf = cse.encode(CharBuffer.wrap(value, offset, count)); if(bbuf.hasArray()) return bbuf.array(); - + // Doubt this will happen. But just in case. byte[] bytes = new byte[bbuf.remaining()]; bbuf.get(bytes); return bytes; - - } catch(IllegalCharsetNameException e){ - throw new UnsupportedEncodingException("Encoding: "+enc+ - " not found."); - } catch(UnsupportedCharsetException e){ - throw new UnsupportedEncodingException("Encoding: "+enc+ - " not found."); - } catch(CharacterCodingException e){ - // XXX - Ignore coding exceptions? They shouldn't really happen. - return null; + } + catch(IllegalCharsetNameException e) + { + throw new UnsupportedEncodingException("Encoding: " + enc + + " not found."); + } + catch(UnsupportedCharsetException e) + { + throw new UnsupportedEncodingException("Encoding: " + enc + + " not found."); + } + catch(CharacterCodingException e) + { + // This shouldn't ever happen. + throw (InternalError) new InternalError().initCause(e); } } @@ -726,6 +787,26 @@ public final class String implements Serializable, Comparable, CharSequence } /** + * Compares the given CharSequence to this String. This is true if + * the CharSequence has the same content as this String at this + * moment. + * + * @param seq the CharSequence to compare to + * @return true if CharSequence has the same character sequence + * @throws NullPointerException if the given CharSequence is null + * @since 1.5 + */ + public boolean contentEquals(CharSequence seq) + { + if (seq.length() != count) + return false; + for (int i = 0; i < count; ++i) + if (value[offset + i] != seq.charAt(i)) + return false; + return true; + } + + /** * Compares a String to this String, ignoring case. This does not handle * multi-character capitalization exceptions; instead the comparison is * made on a character-by-character basis, and is true if:<br><ul> @@ -1546,6 +1627,7 @@ public final class String implements Serializable, Comparable, CharSequence * @return String containing the chars from data[offset..offset+count] * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 + * || offset + count < 0 (overflow) * || offset + count > data.length) * (while unspecified, this is a StringIndexOutOfBoundsException) * @see #String(char[], int, int) @@ -1566,6 +1648,7 @@ public final class String implements Serializable, Comparable, CharSequence * @return String containing the chars from data[offset..offset+count] * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 + * || offset + count < 0 (overflow) * || offset + count > data.length) * (while unspecified, this is a StringIndexOutOfBoundsException) * @see #String(char[], int, int) @@ -1677,6 +1760,49 @@ public final class String implements Serializable, Comparable, CharSequence } /** + * Return the number of code points between two indices in the + * <code>StringBuffer</code>. An unpaired surrogate counts as a + * code point for this purpose. Characters outside the indicated + * range are not examined, even if the range ends in the middle of a + * surrogate pair. + * + * @param start the starting index + * @param end one past the ending index + * @return the number of code points + * @since 1.5 + */ + public synchronized int codePointCount(int start, int end) + { + if (start < 0 || end >= count || start > end) + throw new StringIndexOutOfBoundsException(); + + start += offset; + end += offset; + int count = 0; + while (start < end) + { + char base = value[start]; + if (base < Character.MIN_HIGH_SURROGATE + || base > Character.MAX_HIGH_SURROGATE + || start == end + || start == count + || value[start + 1] < Character.MIN_LOW_SURROGATE + || value[start + 1] > Character.MAX_LOW_SURROGATE) + { + // Nothing. + } + else + { + // Surrogate pair. + ++start; + } + ++start; + ++count; + } + return count; + } + + /** * Helper function used to detect which characters have a multi-character * uppercase expansion. Note that this is only used in locations which * track one-to-many capitalization (java.lang.Character does not do this). @@ -1747,4 +1873,43 @@ public final class String implements Serializable, Comparable, CharSequence return value; } + + /** + * Returns true iff this String contains the sequence of Characters + * described in s. + * @param s the CharSequence + * @return true iff this String contains s + */ + public boolean contains (CharSequence s) + { + return this.indexOf(s.toString()) != -1; + } + + /** + * Returns a string that is this string with all instances of the sequence + * represented by <code>target</code> replaced by the sequence in + * <code>replacement</code>. + * @param target the sequence to be replaced + * @param replacement the sequence used as the replacement + * @return the string constructed as above + */ + public String replace (CharSequence target, CharSequence replacement) + { + String targetString = target.toString(); + String replaceString = replacement.toString(); + int targetLength = target.length(); + int replaceLength = replacement.length(); + + int startPos = this.indexOf(targetString); + StringBuilder result = new StringBuilder(this); + while (startPos != -1) + { + // Replace the target with the replacement + result.replace(startPos, startPos + targetLength, replaceString); + + // Search for a new occurrence of the target + startPos = result.indexOf(targetString, startPos + replaceLength); + } + return result.toString(); + } } diff --git a/libjava/classpath/java/lang/StringBuffer.java b/libjava/classpath/java/lang/StringBuffer.java index 94dec48..caffd6e 100644 --- a/libjava/classpath/java/lang/StringBuffer.java +++ b/libjava/classpath/java/lang/StringBuffer.java @@ -148,6 +148,24 @@ public final class StringBuffer implements Serializable, CharSequence } /** + * Create a new <code>StringBuffer</code> with the characters from the + * specified <code>CharSequence</code>. Initial capacity will be the + * size of the CharSequence plus 16. + * + * @param sequence the <code>String</code> to convert + * @throws NullPointerException if str is null + * + * @since 1.5 + */ + public StringBuffer(CharSequence sequence) + { + count = Math.max(0, sequence.length()); + value = new char[count + DEFAULT_CAPACITY]; + for (int i = 0; i < count; ++i) + value[i] = sequence.charAt(i); + } + + /** * Get the length of the <code>String</code> this <code>StringBuffer</code> * would create. Not to be confused with the <em>capacity</em> of the * <code>StringBuffer</code>. @@ -234,7 +252,6 @@ public final class StringBuffer implements Serializable, CharSequence * @param index the index of the character to get, starting at 0 * @return the character at the specified index * @throws IndexOutOfBoundsException if index is negative or >= length() - * (while unspecified, this is a StringIndexOutOfBoundsException) */ public synchronized char charAt(int index) { @@ -244,6 +261,39 @@ public final class StringBuffer implements Serializable, CharSequence } /** + * Get the code point at the specified index. This is like #charAt(int), + * but if the character is the start of a surrogate pair, and the + * following character completes the pair, then the corresponding + * supplementary code point is returned. + * @param index the index of the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public synchronized int codePointAt(int index) + { + return Character.codePointAt(value, index, count); + } + + /** + * Get the code point before the specified index. This is like + * #codePointAt(int), but checks the characters at <code>index-1</code> and + * <code>index-2</code> to see if they form a supplementary code point. + * @param index the index just past the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public synchronized int codePointBefore(int index) + { + // Character.codePointBefore() doesn't perform this check. We + // could use the CharSequence overload, but this is just as easy. + if (index >= count) + throw new IndexOutOfBoundsException(); + return Character.codePointBefore(value, index, 1); + } + + /** * Get the specified array of characters. <code>srcOffset - srcEnd</code> * characters will be copied into the array you pass in. * @@ -341,6 +391,46 @@ public final class StringBuffer implements Serializable, CharSequence } /** + * Append the <code>CharSequence</code> value of the argument to this + * <code>StringBuffer</code>. + * + * @param sequence the <code>CharSequence</code> to append + * @return this <code>StringBuffer</code> + * @see #append(Object) + * @since 1.5 + */ + public synchronized StringBuffer append(CharSequence sequence) + { + if (sequence == null) + sequence = "null"; + return append(sequence, 0, sequence.length()); + } + + /** + * Append the specified subsequence of the <code>CharSequence</code> + * argument to this <code>StringBuffer</code>. + * + * @param sequence the <code>CharSequence</code> to append + * @param start the starting index + * @param end one past the ending index + * @return this <code>StringBuffer</code> + * @see #append(Object) + * @since 1.5 + */ + public synchronized StringBuffer append(CharSequence sequence, + int start, int end) + { + if (sequence == null) + sequence = "null"; + if (start < 0 || end < 0 || start > end || end > sequence.length()) + throw new IndexOutOfBoundsException(); + ensureCapacity_unsynchronized(this.count + end - start); + for (int i = start; i < end; ++i) + value[count++] = sequence.charAt(i); + return this; + } + + /** * Append the <code>char</code> array to this <code>StringBuffer</code>. * This is similar (but more efficient) than * <code>append(new String(data))</code>, except in the case of null. @@ -407,6 +497,25 @@ public final class StringBuffer implements Serializable, CharSequence } /** + * Append the code point to this <code>StringBuffer</code>. + * This is like #append(char), but will append two characters + * if a supplementary code point is given. + * + * @param code the code point to append + * @return this <code>StringBuffer</code> + * @see Character#toChars(int, char[], int) + * @since 1.5 + */ + public synchronized StringBuffer appendCodePoint(int code) + { + int len = Character.charCount(code); + ensureCapacity_unsynchronized(count + len); + Character.toChars(code, value, count); + count += len; + return this; + } + + /** * Append the <code>String</code> value of the argument to this * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. @@ -660,6 +769,54 @@ public final class StringBuffer implements Serializable, CharSequence } /** + * Insert the <code>CharSequence</code> argument into this + * <code>StringBuffer</code>. If the sequence is null, the String + * "null" is used instead. + * + * @param offset the place to insert in this buffer + * @param sequence the <code>CharSequence</code> to insert + * @return this <code>StringBuffer</code> + * @throws IndexOutOfBoundsException if offset is out of bounds + * @since 1.5 + */ + public synchronized StringBuffer insert(int offset, CharSequence sequence) + { + if (sequence == null) + sequence = "null"; + return insert(offset, sequence, 0, sequence.length()); + } + + /** + * Insert a subsequence of the <code>CharSequence</code> argument into this + * <code>StringBuffer</code>. If the sequence is null, the String + * "null" is used instead. + * + * @param offset the place to insert in this buffer + * @param sequence the <code>CharSequence</code> to insert + * @param start the starting index of the subsequence + * @param end one past the ending index of the subsequence + * @return this <code>StringBuffer</code> + * @throws IndexOutOfBoundsException if offset, start, + * or end are out of bounds + * @since 1.5 + */ + public synchronized StringBuffer insert(int offset, CharSequence sequence, + int start, int end) + { + if (sequence == null) + sequence = "null"; + if (start < 0 || end < 0 || start > end || end > sequence.length()) + throw new IndexOutOfBoundsException(); + int len = end - start; + ensureCapacity_unsynchronized(count + len); + VMSystem.arraycopy(value, offset, value, offset + len, count - offset); + for (int i = start; i < end; ++i) + value[offset++] = sequence.charAt(i); + count += len; + return this; + } + + /** * Insert the <code>char[]</code> argument into this * <code>StringBuffer</code>. * @@ -880,6 +1037,106 @@ public final class StringBuffer implements Serializable, CharSequence } /** + * This may reduce the amount of memory used by the StringBuffer, + * by resizing the internal array to remove unused space. However, + * this method is not required to resize, so this behavior cannot + * be relied upon. + * @since 1.5 + */ + public synchronized void trimToSize() + { + int wouldSave = value.length - count; + // Some random heuristics: if we save less than 20 characters, who + // cares. + if (wouldSave < 20) + return; + // If we save more than 200 characters, shrink. + // If we save more than 1/4 of the buffer, shrink. + if (wouldSave > 200 || wouldSave * 4 > value.length) + { + char[] newValue = new char[count]; + VMSystem.arraycopy(value, 0, newValue, 0, count); + value = newValue; + } + } + + /** + * Return the number of code points between two indices in the + * <code>StringBuffer</code>. An unpaired surrogate counts as a + * code point for this purpose. Characters outside the indicated + * range are not examined, even if the range ends in the middle of a + * surrogate pair. + * + * @param start the starting index + * @param end one past the ending index + * @return the number of code points + * @since 1.5 + */ + public synchronized int codePointCount(int start, int end) + { + if (start < 0 || end >= count || start > end) + throw new StringIndexOutOfBoundsException(); + + int count = 0; + while (start < end) + { + char base = value[start]; + if (base < Character.MIN_HIGH_SURROGATE + || base > Character.MAX_HIGH_SURROGATE + || start == end + || start == count + || value[start + 1] < Character.MIN_LOW_SURROGATE + || value[start + 1] > Character.MAX_LOW_SURROGATE) + { + // Nothing. + } + else + { + // Surrogate pair. + ++start; + } + ++start; + ++count; + } + return count; + } + + /** + * Starting at the given index, this counts forward by the indicated + * number of code points, and then returns the resulting index. An + * unpaired surrogate counts as a single code point for this + * purpose. + * + * @param start the starting index + * @param codePoints the number of code points + * @return the resulting index + * @since 1.5 + */ + public synchronized int offsetByCodePoints(int start, int codePoints) + { + while (codePoints > 0) + { + char base = value[start]; + if (base < Character.MIN_HIGH_SURROGATE + || base > Character.MAX_HIGH_SURROGATE + || start == count + || value[start + 1] < Character.MIN_LOW_SURROGATE + || value[start + 1] > Character.MAX_LOW_SURROGATE) + { + // Nothing. + } + else + { + // Surrogate pair. + ++start; + } + ++start; + --codePoints; + } + return start; + } + + /** * An unsynchronized version of ensureCapacity, used internally to avoid * the cost of a second lock on the same object. This also has the side * effect of duplicating the array, if it was shared (to form copy-on-write diff --git a/libjava/classpath/java/lang/StringBuilder.java b/libjava/classpath/java/lang/StringBuilder.java index b54c8ef..470f1ba 100644 --- a/libjava/classpath/java/lang/StringBuilder.java +++ b/libjava/classpath/java/lang/StringBuilder.java @@ -464,6 +464,25 @@ public final class StringBuilder } /** + * Append the code point to this <code>StringBuilder</code>. + * This is like #append(char), but will append two characters + * if a supplementary code point is given. + * + * @param code the code point to append + * @return this <code>StringBuilder</code> + * @see Character#toChars(int, char[], int) + * @since 1.5 + */ + public synchronized StringBuilder appendCodePoint(int code) + { + int len = Character.charCount(code); + ensureCapacity(count + len); + Character.toChars(code, value, count); + count += len; + return this; + } + + /** * Append the <code>String</code> value of the argument to this * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert * to <code>String</code>. @@ -705,6 +724,52 @@ public final class StringBuilder } /** + * Insert the <code>CharSequence</code> argument into this + * <code>StringBuilder</code>. If the sequence is null, the String + * "null" is used instead. + * + * @param offset the place to insert in this buffer + * @param sequence the <code>CharSequence</code> to insert + * @return this <code>StringBuilder</code> + * @throws IndexOutOfBoundsException if offset is out of bounds + */ + public synchronized StringBuilder insert(int offset, CharSequence sequence) + { + if (sequence == null) + sequence = "null"; + return insert(offset, sequence, 0, sequence.length()); + } + + /** + * Insert a subsequence of the <code>CharSequence</code> argument into this + * <code>StringBuilder</code>. If the sequence is null, the String + * "null" is used instead. + * + * @param offset the place to insert in this buffer + * @param sequence the <code>CharSequence</code> to insert + * @param start the starting index of the subsequence + * @param end one past the ending index of the subsequence + * @return this <code>StringBuilder</code> + * @throws IndexOutOfBoundsException if offset, start, + * or end are out of bounds + */ + public synchronized StringBuilder insert(int offset, CharSequence sequence, + int start, int end) + { + if (sequence == null) + sequence = "null"; + if (start < 0 || end < 0 || start > end || end > sequence.length()) + throw new IndexOutOfBoundsException(); + int len = end - start; + ensureCapacity(count + len); + VMSystem.arraycopy(value, offset, value, offset + len, count - offset); + for (int i = start; i < end; ++i) + value[offset++] = sequence.charAt(i); + count += len; + return this; + } + + /** * Insert the <code>char[]</code> argument into this * <code>StringBuilder</code>. * diff --git a/libjava/classpath/java/lang/System.java b/libjava/classpath/java/lang/System.java index e466d3b..ea84dba 100644 --- a/libjava/classpath/java/lang/System.java +++ b/libjava/classpath/java/lang/System.java @@ -464,7 +464,7 @@ public final class System * * @param finalizeOnExit whether to run finalizers on exit * @throws SecurityException if permission is denied - * @see Runtime#runFinalizersOnExit() + * @see Runtime#runFinalizersOnExit(boolean) * @since 1.1 * @deprecated never rely on finalizers to do a clean, thread-safe, * mop-up from your code diff --git a/libjava/classpath/java/lang/Thread.java b/libjava/classpath/java/lang/Thread.java index 37ca630..763228c 100644 --- a/libjava/classpath/java/lang/Thread.java +++ b/libjava/classpath/java/lang/Thread.java @@ -38,6 +38,7 @@ exception statement from your version. */ package java.lang; +import java.security.Permission; import java.util.Map; import java.util.WeakHashMap; @@ -704,7 +705,7 @@ public class Thread implements Runnable * * @return the context class loader * @throws SecurityException when permission is denied - * @see setContextClassLoader(ClassLoader) + * @see #setContextClassLoader(ClassLoader) * @since 1.2 */ public synchronized ClassLoader getContextClassLoader() @@ -726,7 +727,7 @@ public class Thread implements Runnable * * @param classloader the new context class loader * @throws SecurityException when permission is denied - * @see getContextClassLoader() + * @see #getContextClassLoader() * @since 1.2 */ public synchronized void setContextClassLoader(ClassLoader classloader) @@ -812,8 +813,11 @@ public class Thread implements Runnable { // Check parameters - if (ms < 0 || ns < 0 || ns > 999999) - throw new IllegalArgumentException(); + if (ms < 0 ) + throw new IllegalArgumentException("Negative milliseconds: " + ms); + + if (ns < 0 || ns > 999999) + throw new IllegalArgumentException("Nanoseconds ouf of range: " + ns); // Really sleep VMThread.sleep(ms, ns); diff --git a/libjava/classpath/java/lang/ThreadLocal.java b/libjava/classpath/java/lang/ThreadLocal.java index 0b2b608..bc83904 100644 --- a/libjava/classpath/java/lang/ThreadLocal.java +++ b/libjava/classpath/java/lang/ThreadLocal.java @@ -38,7 +38,6 @@ exception statement from your version. */ package java.lang; import java.util.Map; -import java.util.WeakHashMap; /** diff --git a/libjava/classpath/java/lang/ref/Reference.java b/libjava/classpath/java/lang/ref/Reference.java index 1ec7243..4b6a3ad 100644 --- a/libjava/classpath/java/lang/ref/Reference.java +++ b/libjava/classpath/java/lang/ref/Reference.java @@ -68,7 +68,7 @@ package java.lang.ref; * work. It is useful to keep track, when an object is finalized. * * @author Jochen Hoenicke - * @see java.util.WeakHashtable + * @see java.util.WeakHashMap */ public abstract class Reference { @@ -104,7 +104,7 @@ public abstract class Reference * Creates a new reference that is not registered to any queue. * Since it is package private, it is not possible to overload this * class in a different package. - * @param referent the object we refer to. + * @param ref the object we refer to. */ Reference(Object ref) { @@ -115,7 +115,7 @@ public abstract class Reference * Creates a reference that is registered to a queue. Since this is * package private, it is not possible to overload this class in a * different package. - * @param referent the object we refer to. + * @param ref the object we refer to. * @param q the reference queue to register on. * @exception NullPointerException if q is null. */ diff --git a/libjava/classpath/java/lang/ref/WeakReference.java b/libjava/classpath/java/lang/ref/WeakReference.java index 9f758ca..b4018fb 100644 --- a/libjava/classpath/java/lang/ref/WeakReference.java +++ b/libjava/classpath/java/lang/ref/WeakReference.java @@ -52,7 +52,7 @@ package java.lang.ref; * automatically cleared, and you may remove it from the set. <br> * * @author Jochen Hoenicke - * @see java.util.WeakHashtable + * @see java.util.WeakHashMap */ public class WeakReference extends Reference diff --git a/libjava/classpath/java/lang/reflect/Member.java b/libjava/classpath/java/lang/reflect/Member.java index 9983b27..c39731f 100644 --- a/libjava/classpath/java/lang/reflect/Member.java +++ b/libjava/classpath/java/lang/reflect/Member.java @@ -60,7 +60,7 @@ public interface Member * package-protected, but only which are declared in this class. * Used in SecurityManager.checkMemberAccess() to determine the * type of members to access. - * @see SecurityManager#checkMemberAccess() + * @see SecurityManager#checkMemberAccess(Class, int) */ int DECLARED = 1; @@ -68,7 +68,7 @@ public interface Member * Represents public members only, but includes all inherited members. * Used in SecurityManager.checkMemberAccess() to determine the type of * members to access. - * @see SecurityManager#checkMemberAccess() + * @see SecurityManager#checkMemberAccess(Class, int) */ int PUBLIC = 0; diff --git a/libjava/classpath/java/lang/reflect/Proxy.java b/libjava/classpath/java/lang/reflect/Proxy.java index 7a5fd30..137cb5a 100644 --- a/libjava/classpath/java/lang/reflect/Proxy.java +++ b/libjava/classpath/java/lang/reflect/Proxy.java @@ -100,7 +100,7 @@ import java.util.Set; * belongs to the classloader you designated.</li> * <li>Reflection works as expected: {@link Class#getInterfaces()} and * {@link Class#getMethods()} work as they do on normal classes.</li> - * <li>The method {@link #isProxyClass()} will distinguish between + * <li>The method {@link #isProxyClass(Class)} will distinguish between * true proxy classes and user extensions of this class. It only * returns true for classes created by {@link #getProxyClass}.</li> * <li>The {@link ProtectionDomain} of a proxy class is the same as for @@ -111,8 +111,8 @@ import java.util.Set; * the only way to create an instance of the proxy class.</li> * <li>The proxy class contains a single constructor, which takes as * its only argument an {@link InvocationHandler}. The method - * {@link #newInstance} is shorthand to do the necessary - * reflection.</li> + * {@link #newProxyInstance(ClassLoader, Class[], InvocationHandler)} + * is shorthand to do the necessary reflection.</li> * </ul> * * <h3>Proxy Instances</h3> @@ -126,7 +126,7 @@ import java.util.Set; * a {@link ClassCastException}.</li> * <li>Each proxy instance has an invocation handler, which can be * accessed by {@link #getInvocationHandler(Object)}. Any call - * to an interface method, including {@link Object#hashcode()}, + * to an interface method, including {@link Object#hashCode()}, * {@link Object#equals(Object)}, or {@link Object#toString()}, * but excluding the public final methods of Object, will be * encoded and passed to the {@link InvocationHandler#invoke} @@ -413,8 +413,6 @@ public class Proxy implements Serializable */ ProxyType(ClassLoader loader, Class[] interfaces) { - if (loader == null) - loader = ClassLoader.getSystemClassLoader(); this.loader = loader; this.interfaces = interfaces; } @@ -426,8 +424,7 @@ public class Proxy implements Serializable */ public int hashCode() { - //loader is always not null - int hash = loader.hashCode(); + int hash = loader == null ? 0 : loader.hashCode(); for (int i = 0; i < interfaces.length; i++) hash = hash * 31 + interfaces[i].hashCode(); return hash; @@ -436,7 +433,7 @@ public class Proxy implements Serializable /** * Calculates equality. * - * @param the object to compare to + * @param other object to compare to * @return true if it is a ProxyType with same data */ public boolean equals(Object other) @@ -586,7 +583,7 @@ public class Proxy implements Serializable /** * Calculates equality. * - * @param the object to compare to + * @param other object to compare to * @return true if it is a ProxySignature with same data */ public boolean equals(Object other) @@ -617,7 +614,7 @@ public class Proxy implements Serializable * The package this class is in <b>including the trailing dot</b> * or an empty string for the unnamed (aka default) package. */ - String pack; + String pack = ""; /** * The interfaces this class implements. Non-null, but possibly empty. diff --git a/libjava/classpath/java/lang/reflect/UndeclaredThrowableException.java b/libjava/classpath/java/lang/reflect/UndeclaredThrowableException.java index 6d5a800..ea574ad 100644 --- a/libjava/classpath/java/lang/reflect/UndeclaredThrowableException.java +++ b/libjava/classpath/java/lang/reflect/UndeclaredThrowableException.java @@ -65,7 +65,7 @@ public class UndeclaredThrowableException extends RuntimeException /** * The immutable exception that this wraps. This field is redundant - * with {@link Throwable#cause}, but is necessary for serial compatibility. + * with {@link Throwable#getCause()}, but is necessary for serial compatibility. * * @serial the chained exception */ |