Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Properties.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Properties.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Properties.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Properties.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,461 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.security.AccessController;
+
+import com.ibm.oti.util.PriviAction;
+
+/**
+ * Properties is a Hashtable where the keys and values must be Strings. Each
+ * Properties can have a default Properties which specifies the default values
+ * which are used if the key is not in this Properties.
+ *
+ * @see Hashtable
+ * @see java.lang.System#getProperties
+ */
+public class Properties extends Hashtable {
+
+ static final long serialVersionUID = 4112578634029874840L;
+
+ /**
+ * The default values for this Properties.
+ */
+ protected Properties defaults;
+
+ private static final int NONE = 0, SLASH = 1, UNICODE = 2, CONTINUE = 3,
+ KEY_DONE = 4, IGNORE = 5;
+
+ /**
+ * Constructs a new Properties object.
+ */
+ public Properties() {
+ super();
+ }
+
+ /**
+ * Constructs a new Properties object using the specified default
+ * properties.
+ *
+ * @param properties
+ * the default properties
+ */
+ public Properties(Properties properties) {
+ defaults = properties;
+ }
+
+ private void dumpString(StringBuffer buffer, String string, boolean key) {
+ int i = 0;
+ if (!key && i < string.length() && string.charAt(i) == ' ') {
+ buffer.append("\\ "); //$NON-NLS-1$
+ i++;
+ }
+
+ for (; i < string.length(); i++) {
+ char ch = string.charAt(i);
+ switch (ch) {
+ case '\t':
+ buffer.append("\\t"); //$NON-NLS-1$
+ break;
+ case '\n':
+ buffer.append("\\n"); //$NON-NLS-1$
+ break;
+ case '\f':
+ buffer.append("\\f"); //$NON-NLS-1$
+ break;
+ case '\r':
+ buffer.append("\\r"); //$NON-NLS-1$
+ break;
+ default:
+ if ("\\#!=:".indexOf(ch) >= 0 || (key && ch == ' ')) //$NON-NLS-1$
+ buffer.append('\\');
+ if (ch >= ' ' && ch <= '~') {
+ buffer.append(ch);
+ } else {
+ String hex = Integer.toHexString(ch);
+ buffer.append("\\u"); //$NON-NLS-1$
+ for (int j = 0; j < 4 - hex.length(); j++)
+ buffer.append("0"); //$NON-NLS-1$
+ buffer.append(hex);
+ }
+ }
+ }
+ }
+
+ /**
+ * Searches for the property with the specified name. If the property is not
+ * found, look in the default properties. If the property is not found in
+ * the default properties, answer null.
+ *
+ * @param name
+ * the name of the property to find
+ * @return the named property value
+ */
+ public String getProperty(String name) {
+ Object result = get(name);
+ String property = result instanceof String ? (String) result : null;
+ if (property == null && defaults != null) {
+ property = defaults.getProperty(name);
+ }
+ return property;
+ }
+
+ /**
+ * Searches for the property with the specified name. If the property is not
+ * found, look in the default properties. If the property is not found in
+ * the default properties, answer the specified default.
+ *
+ * @param name
+ * the name of the property to find
+ * @param defaultValue
+ * the default value
+ * @return the named property value
+ */
+ public String getProperty(String name, String defaultValue) {
+ Object result = get(name);
+ String property = result instanceof String ? (String) result : null;
+ if (property == null && defaults != null) {
+ property = defaults.getProperty(name);
+ }
+ if (property == null)
+ return defaultValue;
+ return property;
+ }
+
+ /**
+ * Lists the mappings in this Properties to the specified PrintStream in a
+ * human readable form.
+ *
+ * @param out
+ * the PrintStream
+ */
+ public void list(PrintStream out) {
+ if (out == null)
+ throw new NullPointerException();
+ StringBuffer buffer = new StringBuffer(80);
+ Enumeration keys = propertyNames();
+ while (keys.hasMoreElements()) {
+ String key = (String) keys.nextElement();
+ buffer.append(key);
+ buffer.append('=');
+ String property = (String) get(key);
+ Properties def = defaults;
+ while (property == null) {
+ property = (String) def.get(key);
+ def = def.defaults;
+ }
+ if (property.length() > 40) {
+ buffer.append(property.substring(0, 37));
+ buffer.append("..."); //$NON-NLS-1$
+ } else
+ buffer.append(property);
+ out.println(buffer.toString());
+ buffer.setLength(0);
+ }
+ }
+
+ /**
+ * Lists the mappings in this Properties to the specified PrintWriter in a
+ * human readable form.
+ *
+ * @param writer
+ * the PrintWriter
+ */
+ public void list(PrintWriter writer) {
+ if (writer == null)
+ throw new NullPointerException();
+ StringBuffer buffer = new StringBuffer(80);
+ Enumeration keys = propertyNames();
+ while (keys.hasMoreElements()) {
+ String key = (String) keys.nextElement();
+ buffer.append(key);
+ buffer.append('=');
+ String property = (String) get(key);
+ Properties def = defaults;
+ while (property == null) {
+ property = (String) def.get(key);
+ def = def.defaults;
+ }
+ if (property.length() > 40) {
+ buffer.append(property.substring(0, 37));
+ buffer.append("..."); //$NON-NLS-1$
+ } else
+ buffer.append(property);
+ writer.println(buffer.toString());
+ buffer.setLength(0);
+ }
+ }
+
+ /**
+ * Loads properties from the specified InputStream. The properties are of
+ * the form <code>key=value</code>, one property per line.
+ *
+ * @param in
+ * the input stream
+ * @throws IOException
+ */
+ public synchronized void load(InputStream in) throws IOException {
+ int mode = NONE, unicode = 0, count = 0;
+ char nextChar, buf[] = new char[40];
+ int offset = 0, keyLength = -1;
+ boolean firstChar = true;
+ byte[] inbuf = new byte[256];
+ int inbufCount = 0, inbufPos = 0;
+
+ while (true) {
+ if (inbufPos == inbufCount) {
+ if ((inbufCount = in.read(inbuf)) == -1)
+ break;
+ inbufPos = 0;
+ }
+ nextChar = (char) (inbuf[inbufPos++] & 0xff);
+
+ if (offset == buf.length) {
+ char[] newBuf = new char[buf.length * 2];
+ System.arraycopy(buf, 0, newBuf, 0, offset);
+ buf = newBuf;
+ }
+ if (mode == UNICODE) {
+ int digit = Character.digit(nextChar, 16);
+ if (digit >= 0) {
+ unicode = (unicode << 4) + digit;
+ if (++count < 4)
+ continue;
+ }
+ mode = NONE;
+ buf[offset++] = (char) unicode;
+ if (nextChar != '\n')
+ continue;
+ }
+ if (mode == SLASH) {
+ mode = NONE;
+ switch (nextChar) {
+ case '\r':
+ mode = CONTINUE; // Look for a following \n
+ continue;
+ case '\n':
+ mode = IGNORE; // Ignore whitespace on the next line
+ continue;
+ case 'b':
+ nextChar = '\b';
+ break;
+ case 'f':
+ nextChar = '\f';
+ break;
+ case 'n':
+ nextChar = '\n';
+ break;
+ case 'r':
+ nextChar = '\r';
+ break;
+ case 't':
+ nextChar = '\t';
+ break;
+ case 'u':
+ mode = UNICODE;
+ unicode = count = 0;
+ continue;
+ }
+ } else {
+ switch (nextChar) {
+ case '#':
+ case '!':
+ if (firstChar) {
+ while (true) {
+ if (inbufPos == inbufCount) {
+ if ((inbufCount = in.read(inbuf)) == -1) {
+ inbufPos = -1;
+ break;
+ }
+ inbufPos = 0;
+ }
+ nextChar = (char) inbuf[inbufPos++]; // & 0xff
+ // not
+ // required
+ if (nextChar == '\r' || nextChar == '\n')
+ break;
+ }
+ continue;
+ }
+ break;
+ case '\n':
+ if (mode == CONTINUE) { // Part of a \r\n sequence
+ mode = IGNORE; // Ignore whitespace on the next line
+ continue;
+ }
+ // fall into the next case
+ case '\r':
+ mode = NONE;
+ firstChar = true;
+ if (offset > 0) {
+ if (keyLength == -1) {
+ keyLength = offset;
+ }
+ String temp = new String(buf, 0, offset);
+ put(temp.substring(0, keyLength), temp
+ .substring(keyLength));
+ }
+ keyLength = -1;
+ offset = 0;
+ continue;
+ case '\\':
+ if (mode == KEY_DONE) {
+ keyLength = offset;
+ }
+ mode = SLASH;
+ continue;
+ case ':':
+ case '=':
+ if (keyLength == -1) { // if parsing the key
+ mode = NONE;
+ keyLength = offset;
+ continue;
+ }
+ break;
+ }
+ if (Character.isWhitespace(nextChar)) {
+ if (mode == CONTINUE)
+ mode = IGNORE;
+ // if key length == 0 or value length == 0
+ if (offset == 0 || offset == keyLength || mode == IGNORE)
+ continue;
+ if (keyLength == -1) { // if parsing the key
+ mode = KEY_DONE;
+ continue;
+ }
+ }
+ if (mode == IGNORE || mode == CONTINUE)
+ mode = NONE;
+ }
+ firstChar = false;
+ if (mode == KEY_DONE) {
+ keyLength = offset;
+ mode = NONE;
+ }
+ buf[offset++] = nextChar;
+ }
+ if (keyLength >= 0) {
+ String temp = new String(buf, 0, offset);
+ put(temp.substring(0, keyLength), temp.substring(keyLength));
+ }
+ }
+
+ /**
+ * Answers all of the property names that this Properties contains.
+ *
+ * @return an Enumeration containing the names of all properties
+ */
+ public Enumeration propertyNames() {
+ if (defaults == null)
+ return keys();
+
+ Hashtable set = new Hashtable(defaults.size() + size());
+ Enumeration keys = defaults.propertyNames();
+ while (keys.hasMoreElements()) {
+ set.put(keys.nextElement(), set);
+ }
+ keys = keys();
+ while (keys.hasMoreElements()) {
+ set.put(keys.nextElement(), set);
+ }
+ return set.keys();
+ }
+
+ /**
+ * Saves the mappings in this Properties to the specified OutputStream,
+ * putting the specified comment at the beginning. The output from this
+ * method is suitable for being read by the load() method.
+ *
+ * @param out
+ * the OutputStream
+ * @param comment
+ * the comment
+ *
+ * @exception ClassCastException
+ * when the key or value of a mapping is not a String
+ *
+ * @deprecated Does not throw an IOException, use store()
+ */
+ public void save(OutputStream out, String comment) {
+ try {
+ store(out, comment);
+ } catch (IOException e) {
+ }
+ }
+
+ /**
+ * Maps the specified key to the specified value. If the key already exists,
+ * the old value is replaced. The key and value cannot be null.
+ *
+ * @param name
+ * the key
+ * @param value
+ * the value
+ * @return the old value mapped to the key, or null
+ */
+ public synchronized Object setProperty(String name, String value) {
+ return put(name, value);
+ }
+
+ private static String lineSeparator;
+
+ /**
+ * Stores the mappings in this Properties to the specified OutputStream,
+ * putting the specified comment at the beginning. The output from this
+ * method is suitable for being read by the load() method.
+ *
+ * @param out
+ * the OutputStream
+ * @param comment
+ * the comment
+ * @throws IOException
+ *
+ * @exception ClassCastException
+ * when the key or value of a mapping is not a String
+ */
+ public synchronized void store(OutputStream out, String comment)
+ throws IOException {
+ if (lineSeparator == null)
+ lineSeparator = (String) AccessController
+ .doPrivileged(new PriviAction("line.separator")); //$NON-NLS-1$
+
+ StringBuffer buffer = new StringBuffer(200);
+ OutputStreamWriter writer = new OutputStreamWriter(out, "ISO8859_1"); //$NON-NLS-1$
+ if (comment != null)
+ writer.write("#" + comment + lineSeparator); //$NON-NLS-1$
+ writer.write("#" + new Date() + lineSeparator); //$NON-NLS-1$
+ Iterator entryItr = entrySet().iterator();
+ while (entryItr.hasNext()) {
+ MapEntry entry = (MapEntry) entryItr.next();
+ String key = (String) entry.getKey();
+ dumpString(buffer, key, true);
+ buffer.append('=');
+ dumpString(buffer, (String) entry.getValue(), false);
+ buffer.append(lineSeparator);
+ writer.write(buffer.toString());
+ buffer.setLength(0);
+ }
+ writer.flush();
+ }
+
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermission.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermission.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermission.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermission.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,151 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
+import java.security.BasicPermission;
+import java.security.Permission;
+import java.security.PermissionCollection;
+
+/**
+ * PropertyPermission objects represent permission to access system properties.
+ */
+public final class PropertyPermission extends BasicPermission {
+ static final long serialVersionUID = 885438825399942851L;
+
+ transient private boolean read, write;
+
+ /**
+ * Constructs a new instance of this class.
+ *
+ * @param name
+ * java.lang.String the (possibly wildcarded) name of the
+ * property.
+ * @param actions
+ * java.lang.String the actions which are applicable to it.
+ */
+ public PropertyPermission(String name, String actions) {
+ super(name);
+ decodeActions(actions);
+ }
+
+ private void decodeActions(String actions) {
+ StringTokenizer tokenizer = new StringTokenizer(actions.toLowerCase(),
+ " \t\n\r,"); //$NON-NLS-1$
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken();
+ if (token.equals("read")) //$NON-NLS-1$
+ read = true;
+ else if (token.equals("write")) //$NON-NLS-1$
+ write = true;
+ else
+ throw new IllegalArgumentException();
+ }
+ if (!read && !write)
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Compares the argument to the receiver, and answers true if they represent
+ * the <em>same</em> object using a class specific comparison. In this
+ * case, the receiver must be for the same property as the argument, and
+ * must have the same actions.
+ *
+ * @param o
+ * the object to compare with this object
+ * @return <code>true</code> if the object is the same as this object
+ * <code>false</code> if it is different from this object
+ * @see #hashCode
+ */
+ public boolean equals(Object o) {
+ if (super.equals(o)) {
+ PropertyPermission pp = (PropertyPermission) o;
+ return read == pp.read && write == pp.write;
+ }
+ return false;
+ }
+
+ /**
+ * Answers the actions associated with the receiver. The result will be
+ * either "read", "write", or "read,write".
+ *
+ * @return String the actions associated with the receiver.
+ */
+ public String getActions() {
+ return read ? (write ? "read,write" : "read") : "write"; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
+ }
+
+ /**
+ * Answers an integer hash code for the receiver. Any two objects which
+ * answer <code>true</code> when passed to <code>equals</code> must
+ * answer the same value for this method.
+ *
+ * @return the receiver's hash
+ *
+ * @see #equals
+ */
+ public int hashCode() {
+ return super.hashCode();
+ }
+
+ /**
+ * Indicates whether the argument permission is implied by the receiver.
+ *
+ * @return boolean <code>true</code> if the argument permission is implied
+ * by the receiver, and <code>false</code> if it is not.
+ * @param permission
+ * java.security.Permission the permission to check
+ */
+ public boolean implies(Permission permission) {
+ if (super.implies(permission)) {
+ PropertyPermission pp = (PropertyPermission) permission;
+ return (read || !pp.read) && (write || !pp.write);
+ }
+ return false;
+ }
+
+ /**
+ * Answers a new PermissionCollection for holding permissions of this class.
+ * Answer null if any permission collection can be used.
+ *
+ * @return a new PermissionCollection or null
+ *
+ * see java.security.BasicPermissionCollection
+ */
+ public PermissionCollection newPermissionCollection() {
+ return new PropertyPermissionCollection();
+ }
+
+ private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
+ "actions", String.class) }; //$NON-NLS-1$
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ ObjectOutputStream.PutField fields = stream.putFields();
+ fields.put("actions", getActions()); //$NON-NLS-1$
+ stream.writeFields();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream.GetField fields = stream.readFields();
+ String actions = (String) fields.get("actions", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ decodeActions(actions);
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermissionCollection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermissionCollection.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermissionCollection.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyPermissionCollection.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,84 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
+import java.security.Permission;
+import java.security.PermissionCollection;
+
+/**
+ * A PermissionCollection for holding PropertyPermissions.
+ */
+class PropertyPermissionCollection extends PermissionCollection {
+
+ static final long serialVersionUID = 7015263904581634791L;
+
+ Hashtable permissions = new Hashtable(30);
+
+ public void add(Permission perm) {
+ if (!isReadOnly()) {
+ Permission previous = (Permission) permissions.put(perm.getName(),
+ perm);
+ // if the permission already existed but with only "read" or "write"
+ // set, then replace with both set
+ if (previous != null
+ && !previous.getActions().equals(perm.getActions()))
+ permissions.put(perm.getName(), new PropertyPermission(perm
+ .getName(), "read,write")); //$NON-NLS-1$
+ } else
+ throw new IllegalStateException();
+ }
+
+ public Enumeration elements() {
+ return permissions.elements();
+ }
+
+ public boolean implies(Permission perm) {
+ Enumeration elemEnum = elements();
+ while (elemEnum.hasMoreElements())
+ if (((Permission) elemEnum.nextElement()).implies(perm))
+ return true;
+ // At this point, the only way it can succeed is if both read and write
+ // are set,
+ // and these are separately granted by two different permissions with
+ // one
+ // representing a parent directory.
+ return perm.getActions().equals("read,write") //$NON-NLS-1$
+ && implies(new PropertyPermission(perm.getName(), "read")) //$NON-NLS-1$
+ && implies(new PropertyPermission(perm.getName(), "write")); //$NON-NLS-1$
+ }
+
+ private static final ObjectStreamField[] serialPersistentFields = {
+ new ObjectStreamField("permissions", Hashtable.class), //$NON-NLS-1$
+ new ObjectStreamField("all_allowed", Boolean.TYPE) }; //$NON-NLS-1$
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ ObjectOutputStream.PutField fields = stream.putFields();
+ fields.put("permissions", permissions); //$NON-NLS-1$
+ fields.put("all_allowed", false); //$NON-NLS-1$
+ stream.writeFields();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream.GetField fields = stream.readFields();
+ permissions = (Hashtable) fields.get("permissions", null); //$NON-NLS-1$
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyResourceBundle.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyResourceBundle.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyResourceBundle.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/PropertyResourceBundle.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,107 @@
+/* Copyright 1998, 2002 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * PropertyResourceBundle loads resources from an InputStream. All resources are
+ * Strings. The resources must be of the form <code>key=value</code>, one
+ * resource per line.
+ *
+ * @see ResourceBundle
+ * @see Properties
+ */
+public class PropertyResourceBundle extends ResourceBundle {
+
+ Properties resources;
+
+ /**
+ * Constructs a new instance of PropertyResourceBundle and loads the
+ * properties file from the specified input stream.
+ *
+ * @param stream
+ * the input stream
+ * @throws IOException
+ */
+ public PropertyResourceBundle(InputStream stream) throws IOException {
+ resources = new Properties();
+ resources.load(stream);
+ }
+
+ /**
+ * Answers the names of the resources contained in this
+ * PropertyResourceBundle.
+ *
+ * @return an Enumeration of the resource names
+ */
+ public Enumeration getKeys() {
+ if (parent == null)
+ return resources.keys();
+ return new Enumeration() {
+ Enumeration local = resources.keys();
+
+ Enumeration pEnum = parent.getKeys();
+
+ Object nextElement = null;
+
+ private boolean findNext() {
+ if (nextElement != null)
+ return true;
+ while (pEnum.hasMoreElements()) {
+ String next = (String) pEnum.nextElement();
+ if (!resources.containsKey(next)) {
+ nextElement = next;
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public boolean hasMoreElements() {
+ if (local.hasMoreElements())
+ return true;
+ return findNext();
+ }
+
+ public Object nextElement() {
+ if (local.hasMoreElements())
+ return local.nextElement();
+ if (findNext()) {
+ Object result = nextElement;
+ nextElement = null;
+ return result;
+ }
+ // Cause an exception
+ return pEnum.nextElement();
+ }
+ };
+ }
+
+ /**
+ * Answers the named resource from this PropertyResourceBundle, or null if
+ * the resource is not found.
+ *
+ * @param key
+ * the name of the resource
+ * @return the resource object
+ */
+ public Object handleGetObject(String key) {
+ return resources.get(key);
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Random.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Random.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Random.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Random.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,261 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.Serializable;
+
+/**
+ * This class provides methods that generates pseudorandom number of different
+ * types, such as int, long, double and float using either
+ *
+ * @see Properties
+ * @see PropertyResourceBundle
+ */
+public class Random implements Serializable {
+
+ static final long serialVersionUID = 3905348978240129619L;
+
+ static final long multiplier = 0x5deece66dL;
+
+ /**
+ * The boolean value indicating if the second Gaussian number is available.
+ *
+ * @serial
+ */
+ boolean haveNextNextGaussian = false;
+
+ /**
+ * @serial It is associated with the internal state of this generator.
+ */
+ long seed;
+
+ /**
+ * The second Gaussian generated number.
+ *
+ * @serial
+ */
+ double nextNextGaussian = 0;
+
+ /**
+ * Construct a random generator with the curent time of day in milliseconds
+ * as the initial state.
+ *
+ * @see #setSeed
+ */
+ public Random() {
+ setSeed(System.currentTimeMillis());
+ }
+
+ /**
+ * Construct a random generator with the given <code>seed</code> as the
+ * initial state.
+ *
+ * @param seed
+ * the seed that will determine the initial state of this random
+ * number generator
+ *
+ * @see #setSeed
+ */
+ public Random(long seed) {
+ setSeed(seed);
+ }
+
+ /**
+ * Answers a pseudorandom uniformly distributed <code>int</code> value of
+ * the number of bits specified by the argument <code>bits</code> as
+ * described by Donald E. Knuth in <i>The Art of Computer Programming,
+ * Volume 2: Seminumerical Algorithms</i>, section 3.2.1.
+ *
+ * @return int a pseudorandom generated int number
+ * @param bits
+ * number of bits of the returned value
+ *
+ * @see #nextBytes
+ * @see #nextDouble
+ * @see #nextFloat
+ * @see #nextInt()
+ * @see #nextInt(int)
+ * @see #nextGaussian
+ * @see #nextLong
+ */
+ protected synchronized int next(int bits) {
+ seed = (seed * multiplier + 0xbL) & ((1L << 48) - 1);
+ return (int) (seed >>> (48 - bits));
+ }
+
+ /**
+ * Answers the next pseudorandom, uniformly distributed boolean value
+ * generated by this generator.
+ *
+ * @return boolean a pseudorandom, uniformly distributed boolean value
+ */
+ public boolean nextBoolean() {
+ return next(1) != 0;
+ }
+
+ /**
+ * Modifies the byte array by a random sequence of bytes generated by this
+ * random number generator.
+ *
+ * @param buf
+ * non-null array to contain the new random bytes
+ *
+ * @see #next
+ */
+ public void nextBytes(byte[] buf) {
+ // for (int i = 0; i < buf.length; i++)
+ // buf[i] = (byte) next(8);
+ int rand = 0, count = 0, loop = 0;
+ while (count < buf.length) {
+ if (loop == 0) {
+ rand = nextInt();
+ loop = 3;
+ } else
+ loop--;
+ buf[count++] = (byte) rand;
+ rand >>= 8;
+ }
+ }
+
+ /**
+ * Generates a normally distributed random double number between 0.0
+ * inclusively and 1.0 exclusively.
+ *
+ * @return double
+ *
+ * @see #nextFloat
+ */
+ public double nextDouble() {
+ return ((((long) next(26) << 27) + next(27)) / (double) (1L << 53));
+ }
+
+ /**
+ * Generates a normally distributed random float number between 0.0
+ * inclusively and 1.0 exclusively.
+ *
+ * @return float a random float number between 0.0 and 1.0
+ *
+ * @see #nextDouble
+ */
+ public float nextFloat() {
+ return (next(24) / 16777216f);
+ }
+
+ /**
+ * Pseudorandomly generates (approximately) a normally distributed
+ * <code>double</code> value with mean 0.0 and a standard deviation value
+ * of <code>1.0</code> using the <i>polar method<i> of G. E. P. Box, M.
+ * E. Muller, and G. Marsaglia, as described by Donald E. Knuth in <i>The
+ * Art of Computer Programming, Volume 2: Seminumerical Algorithms</i>,
+ * section 3.4.1, subsection C, algorithm P
+ *
+ * @return double
+ *
+ * @see #nextDouble
+ */
+ public synchronized double nextGaussian() {
+ if (haveNextNextGaussian) { // if X1 has been returned, return the
+ // second Gaussian
+ haveNextNextGaussian = false;
+ return nextNextGaussian;
+ }
+
+ double v1, v2, s;
+ do {
+ v1 = 2 * nextDouble() - 1; // Generates two independent random
+ // variables U1, U2
+ v2 = 2 * nextDouble() - 1;
+ s = v1 * v1 + v2 * v2;
+ } while (s >= 1);
+ double norm = Math.sqrt(-2 * Math.log(s) / s);
+ nextNextGaussian = v2 * norm; // should that not be norm instead
+ // of multiplier ?
+ haveNextNextGaussian = true;
+ return v1 * norm; // should that not be norm instead of multiplier
+ // ?
+ }
+
+ /**
+ * Generates a uniformly distributed 32-bit <code>int</code> value from
+ * the this random number sequence.
+ *
+ * @return int uniformly distributed <code>int</code> value
+ *
+ * @see java.lang.Integer#MAX_VALUE
+ * @see java.lang.Integer#MIN_VALUE
+ * @see #next
+ * @see #nextLong
+ */
+ public int nextInt() {
+ return next(32);
+ }
+
+ /**
+ * Returns to the caller a new pseudorandom integer value which is uniformly
+ * distributed between 0 (inclusively) and the value of <cod>n</code>
+ * (exclusively).
+ *
+ * @return int
+ * @param n
+ * int
+ */
+ public int nextInt(int n) {
+ if (n > 0) {
+ if ((n & -n) == n)
+ return (int) ((n * (long) next(31)) >> 31);
+ int bits, val;
+ do {
+ bits = next(31);
+ val = bits % n;
+ } while (bits - val + (n - 1) < 0);
+ return val;
+ } else
+ throw new IllegalArgumentException();
+ }
+
+ /**
+ * Generates a uniformly distributed 64-bit <code>int</code> value from
+ * the this random number sequence.
+ *
+ * @return 64-bit <code>int</code> random number
+ *
+ * @see java.lang.Integer#MAX_VALUE
+ * @see java.lang.Integer#MIN_VALUE
+ * @see #next
+ * @see #nextInt()
+ * @see #nextInt(int)
+ */
+ public long nextLong() {
+ return ((long) next(32) << 32) + next(32);
+ }
+
+ /**
+ * Modifies the seed using linear congruential formula presented in <i>The
+ * Art of Computer Programming, Volume 2</i>, Section 3.2.1.
+ *
+ * @param seed
+ * the seed that alters the state of the random number generator
+ *
+ * @see #next
+ * @see #Random()
+ * @see #Random(long)
+ */
+ public synchronized void setSeed(long seed) {
+ this.seed = (seed ^ multiplier) & ((1L << 48) - 1);
+ haveNextNextGaussian = false;
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/RandomAccess.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/RandomAccess.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/RandomAccess.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/RandomAccess.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,25 @@
+/* Copyright 2004, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+/**
+ * RandomAccess is implemented by <code>List</code> implementations that support
+ * fast (usually constant time) random access.
+ */
+public interface RandomAccess {
+ /*empty*/
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/ResourceBundle.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/ResourceBundle.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/ResourceBundle.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/ResourceBundle.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,351 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+import com.ibm.oti.vm.VM;
+
+/**
+ * ResourceBundle is an abstract class which is the superclass of classes which
+ * provide locale specific resources. A bundle contains a number of named
+ * resources, where the names are Strings. A bundle may have a parent bundle,
+ * when a resource is not found in a bundle, the parent bundle is searched for
+ * the resource.
+ *
+ * @see Properties
+ * @see PropertyResourceBundle
+ */
+abstract public class ResourceBundle {
+
+ /**
+ * The parent of this ResourceBundle.
+ */
+ protected ResourceBundle parent;
+
+ private Locale locale;
+
+ static class MissingBundle extends ResourceBundle {
+ public Enumeration getKeys() {
+ return null;
+ }
+
+ public Object handleGetObject(String name) {
+ return null;
+ }
+ }
+
+ private static ResourceBundle MISSING = new MissingBundle();
+
+ private static ResourceBundle MISSINGBASE = new MissingBundle();
+
+ private static WeakHashMap cache = new WeakHashMap();
+
+ /**
+ * Constructs a new instance of this class.
+ *
+ */
+ public ResourceBundle() {
+ /*empty*/
+ }
+
+ /**
+ * Finds the named resource bundle for the default locale.
+ *
+ * @param bundleName
+ * the name of the resource bundle
+ * @return ResourceBundle
+ *
+ * @exception MissingResourceException
+ * when the resource bundle cannot be found
+ */
+ public static final ResourceBundle getBundle(String bundleName)
+ throws MissingResourceException {
+ return getBundleImpl(bundleName, Locale.getDefault(), VM
+ .callerClassLoader());
+ }
+
+ /**
+ * Finds the named resource bundle for the specified locale.
+ *
+ * @param bundleName
+ * the name of the resource bundle
+ * @param locale
+ * the locale
+ * @return ResourceBundle
+ *
+ * @exception MissingResourceException
+ * when the resource bundle cannot be found
+ */
+ public static final ResourceBundle getBundle(String bundleName,
+ Locale locale) {
+ return getBundleImpl(bundleName, locale, VM.callerClassLoader());
+ }
+
+ /**
+ * Finds the named resource bundle for the specified locale.
+ *
+ * @param bundleName
+ * the name of the resource bundle
+ * @param locale
+ * the locale
+ * @param loader
+ * the ClassLoader to use
+ * @return ResourceBundle
+ *
+ * @exception MissingResourceException
+ * when the resource bundle cannot be found
+ */
+ public static ResourceBundle getBundle(String bundleName, Locale locale,
+ ClassLoader loader) throws MissingResourceException {
+ if (loader == null)
+ throw new NullPointerException();
+ if (bundleName != null) {
+ ResourceBundle bundle;
+ if (!locale.equals(Locale.getDefault()))
+ if ((bundle = handleGetBundle(bundleName, "_" + locale, false, //$NON-NLS-1$
+ loader)) != null)
+ return bundle;
+ if ((bundle = handleGetBundle(bundleName,
+ "_" + Locale.getDefault(), true, loader)) != null) //$NON-NLS-1$
+ return bundle;
+ throw new MissingResourceException(null, bundleName, ""); //$NON-NLS-1$
+ } else
+ throw new NullPointerException();
+ }
+
+ private static ResourceBundle getBundleImpl(String bundleName,
+ Locale locale, ClassLoader loader) throws MissingResourceException {
+ if (bundleName != null) {
+ ResourceBundle bundle;
+ if (!locale.equals(Locale.getDefault())) {
+ String localeName = locale.toString();
+ if (localeName.length() > 0)
+ localeName = "_" + localeName; //$NON-NLS-1$
+ if ((bundle = handleGetBundle(bundleName, localeName, false,
+ loader)) != null)
+ return bundle;
+ }
+ String localeName = Locale.getDefault().toString();
+ if (localeName.length() > 0)
+ localeName = "_" + localeName; //$NON-NLS-1$
+ if ((bundle = handleGetBundle(bundleName, localeName, true, loader)) != null)
+ return bundle;
+ throw new MissingResourceException(null, bundleName, ""); //$NON-NLS-1$
+ } else
+ throw new NullPointerException();
+ }
+
+ /**
+ * Answers the names of the resources contained in this ResourceBundle.
+ *
+ * @return an Enumeration of the resource names
+ */
+ public abstract Enumeration getKeys();
+
+ /**
+ * Gets the Locale of this ResourceBundle.
+ *
+ * @return the Locale of this ResourceBundle
+ */
+ public Locale getLocale() {
+ return locale;
+ }
+
+ /**
+ * Answers the named resource from this ResourceBundle.
+ *
+ * @param key
+ * the name of the resource
+ * @return the resource object
+ *
+ * @exception MissingResourceException
+ * when the resource is not found
+ */
+ public final Object getObject(String key) {
+ ResourceBundle last, theParent = this;
+ do {
+ Object result = theParent.handleGetObject(key);
+ if (result != null)
+ return result;
+ last = theParent;
+ theParent = theParent.parent;
+ } while (theParent != null);
+ throw new MissingResourceException(null, last.getClass().getName(), key);
+ }
+
+ /**
+ * Answers the named resource from this ResourceBundle.
+ *
+ * @param key
+ * the name of the resource
+ * @return the resource string
+ *
+ * @exception MissingResourceException
+ * when the resource is not found
+ */
+ public final String getString(String key) {
+ return (String) getObject(key);
+ }
+
+ /**
+ * Answers the named resource from this ResourceBundle.
+ *
+ * @param key
+ * the name of the resource
+ * @return the resource string array
+ *
+ * @exception MissingResourceException
+ * when the resource is not found
+ */
+ public final String[] getStringArray(String key) {
+ return (String[]) getObject(key);
+ }
+
+ private static ResourceBundle handleGetBundle(String base, String locale,
+ boolean loadBase, final ClassLoader loader) {
+ ResourceBundle bundle = null;
+ String bundleName = base + locale;
+ Object cacheKey = loader != null ? (Object) loader : (Object) "null"; //$NON-NLS-1$
+ Hashtable loaderCache;
+ synchronized (cache) {
+ loaderCache = (Hashtable) cache.get(cacheKey);
+ if (loaderCache == null) {
+ loaderCache = new Hashtable(13);
+ cache.put(cacheKey, loaderCache);
+ }
+ }
+ ResourceBundle result = (ResourceBundle) loaderCache.get(bundleName);
+ if (result != null) {
+ if (result == MISSINGBASE)
+ return null;
+ if (result == MISSING) {
+ if (!loadBase)
+ return null;
+ String extension = strip(locale);
+ if (extension == null)
+ return null;
+ return handleGetBundle(base, extension, loadBase, loader);
+ }
+ return result;
+ }
+
+ try {
+ Class bundleClass = Class.forName(bundleName, true, loader);
+ bundle = (ResourceBundle) bundleClass.newInstance();
+ bundle.setLocale(locale);
+ } catch (Exception e) {
+ } catch (LinkageError e) {
+ }
+
+ if (bundle == null) {
+ final String fileName = bundleName.replace('.', '/');
+ InputStream stream = null;
+ stream = (InputStream) AccessController
+ .doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ return loader == null ? ClassLoader
+ .getSystemResourceAsStream(fileName
+ + ".properties") : loader //$NON-NLS-1$
+ .getResourceAsStream(fileName
+ + ".properties"); //$NON-NLS-1$
+ }
+ });
+ if (stream != null) {
+ try {
+ bundle = new PropertyResourceBundle(stream);
+ bundle.setLocale(locale);
+ stream.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ String extension = strip(locale);
+ if (bundle != null) {
+ if (extension != null) {
+ ResourceBundle parent = handleGetBundle(base, extension, true,
+ loader);
+ if (parent != null)
+ bundle.setParent(parent);
+ }
+ loaderCache.put(bundleName, bundle);
+ return bundle;
+ }
+
+ if (extension != null && (loadBase || extension.length() > 0)) {
+ bundle = handleGetBundle(base, extension, loadBase, loader);
+ if (bundle != null) {
+ loaderCache.put(bundleName, bundle);
+ return bundle;
+ }
+ }
+ loaderCache.put(bundleName, loadBase ? MISSINGBASE : MISSING);
+ return null;
+ }
+
+ /**
+ * Answers the named resource from this ResourceBundle, or null if the
+ * resource is not found.
+ *
+ * @param key
+ * the name of the resource
+ * @return the resource object
+ */
+ protected abstract Object handleGetObject(String key);
+
+ /**
+ * Sets the parent resource bundle of this ResourceBundle. The parent is
+ * searched for resources which are not found in this resource bundle.
+ *
+ * @param bundle
+ * the parent resource bundle
+ */
+ protected void setParent(ResourceBundle bundle) {
+ parent = bundle;
+ }
+
+ private static String strip(String name) {
+ int index = name.lastIndexOf('_');
+ if (index != -1)
+ return name.substring(0, index);
+ return null;
+ }
+
+ private void setLocale(String name) {
+ String language = "", country = "", variant = ""; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+ if (name.length() > 1) {
+ int nextIndex = name.indexOf('_', 1);
+ if (nextIndex == -1)
+ nextIndex = name.length();
+ language = name.substring(1, nextIndex);
+ if (nextIndex + 1 < name.length()) {
+ int index = nextIndex;
+ nextIndex = name.indexOf('_', nextIndex + 1);
+ if (nextIndex == -1)
+ nextIndex = name.length();
+ country = name.substring(index + 1, nextIndex);
+ if (nextIndex + 1 < name.length())
+ variant = name.substring(nextIndex + 1, name.length());
+ }
+ }
+ locale = new Locale(language, country, variant);
+ }
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Set.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Set.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Set.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/Set.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,195 @@
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+/**
+ * Set is a collection which does not allow duplicate elements.
+ */
+public interface Set extends Collection {
+
+ /**
+ * Adds the specified object to this Set. The Set is not modified if it
+ * already contains the object.
+ *
+ * @param object
+ * the object to add
+ * @return true if this Set is modified, false otherwise
+ *
+ * @exception UnsupportedOperationException
+ * when adding to this Set is not supported
+ * @exception ClassCastException
+ * when the class of the object is inappropriate for this Set
+ * @exception IllegalArgumentException
+ * when the object cannot be added to this Set
+ */
+ public boolean add(Object object);
+
+ /**
+ * Adds the objects in the specified Collection which do not exist in this
+ * Set.
+ *
+ * @param collection
+ * the Collection of objects
+ * @return true if this Set is modified, false otherwise
+ *
+ * @exception UnsupportedOperationException
+ * when adding to this Set is not supported
+ * @exception ClassCastException
+ * when the class of an object is inappropriate for this Set
+ * @exception IllegalArgumentException
+ * when an object cannot be added to this Set
+ */
+ public boolean addAll(Collection collection);
+
+ /**
+ * Removes all elements from this Set, leaving it empty.
+ *
+ * @exception UnsupportedOperationException
+ * when removing from this Set is not supported
+ *
+ * @see #isEmpty
+ * @see #size
+ */
+ public void clear();
+
+ /**
+ * Searches this Set for the specified object.
+ *
+ * @param object
+ * the object to search for
+ * @return true if object is an element of this Set, false otherwise
+ */
+ public boolean contains(Object object);
+
+ /**
+ * Searches this Set for all objects in the specified Collection.
+ *
+ * @param collection
+ * the Collection of objects
+ * @return true if all objects in the specified Collection are elements of
+ * this Set, false otherwise
+ */
+ public boolean containsAll(Collection collection);
+
+ /**
+ * Compares the argument to the receiver, and answers true if they represent
+ * the <em>same</em> object using a class specific comparison.
+ *
+ * @param object
+ * Object the object to compare with this object.
+ * @return boolean <code>true</code> if the object is the same as this
+ * object <code>false</code> if it is different from this object.
+ * @see #hashCode
+ */
+ public boolean equals(Object object);
+
+ /**
+ * Answers an integer hash code for the receiver. Objects which are equal
+ * answer the same value for this method.
+ *
+ * @return the receiver's hash
+ *
+ * @see #equals
+ */
+ public int hashCode();
+
+ /**
+ * Answers if this Set has no elements, a size of zero.
+ *
+ * @return true if this Set has no elements, false otherwise
+ *
+ * @see #size
+ */
+ public boolean isEmpty();
+
+ /**
+ * Answers an Iterator on the elements of this Set.
+ *
+ * @return an Iterator on the elements of this Set
+ *
+ * @see Iterator
+ */
+ public Iterator iterator();
+
+ /**
+ * Removes any occurrence of the specified object from this Set.
+ *
+ * @param object
+ * the object to remove
+ * @return true if this Set is modified, false otherwise
+ *
+ * @exception UnsupportedOperationException
+ * when removing from this Set is not supported
+ */
+ public boolean remove(Object object);
+
+ /**
+ * Removes all objects in the specified Collection from this Set.
+ *
+ * @param collection
+ * the Collection of objects to remove
+ * @return true if this Set is modified, false otherwise
+ *
+ * @exception UnsupportedOperationException
+ * when removing from this Set is not supported
+ */
+ public boolean removeAll(Collection collection);
+
+ /**
+ * Removes all objects from this Set that are not contained in the specified
+ * Collection.
+ *
+ * @param collection
+ * the Collection of objects to retain
+ * @return true if this Set is modified, false otherwise
+ *
+ * @exception UnsupportedOperationException
+ * when removing from this Set is not supported
+ */
+ public boolean retainAll(Collection collection);
+
+ /**
+ * Answers the number of elements in this Set.
+ *
+ * @return the number of elements in this Set
+ */
+ public int size();
+
+ /**
+ * Answers an array containing all elements contained in this Set.
+ *
+ * @return an array of the elements from this Set
+ */
+ public Object[] toArray();
+
+ /**
+ * Answers an array containing all elements contained in this Set. If the
+ * specified array is large enough to hold the elements, the specified array
+ * is used, otherwise an array of the same type is created. If the specified
+ * array is used and is larger than this Set, the array element following
+ * the collection elements is set to null.
+ *
+ * @param array
+ * the array
+ * @return an array of the elements from this Set
+ *
+ * @exception ArrayStoreException
+ * when the type of an element in this Set cannot be stored
+ * in the type of the specified array
+ */
+ public Object[] toArray(Object[] array);
+}
Added: incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/SimpleTimeZone.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/SimpleTimeZone.java?rev=350181&view=auto
==============================================================================
--- incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/SimpleTimeZone.java (added)
+++ incubator/harmony/enhanced/trunk/sandbox/contribs/ibm_core/java-src/luni/src/java/util/SimpleTimeZone.java Wed Nov 30 21:29:27 2005
@@ -0,0 +1,914 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
+
+/**
+ * SimpleTimeZone represents a local time zone and its daylight savings time
+ * rules for the gregorian calendar.
+ *
+ * @see Calendar
+ * @see TimeZone
+ */
+public class SimpleTimeZone extends TimeZone {
+
+ static final long serialVersionUID = -403250971215465050L;
+
+ private int rawOffset;
+
+ private int startYear, startMonth, startDay, startDayOfWeek, startTime;
+
+ private int endMonth, endDay, endDayOfWeek, endTime;
+
+ private int startMode, endMode;
+
+ private static final int DOM_MODE = 1, DOW_IN_MONTH_MODE = 2,
+ DOW_GE_DOM_MODE = 3, DOW_LE_DOM_MODE = 4;
+
+ /* Constant for representing start or end time in GMT time mode. */
+ public static final int UTC_TIME = 2;
+
+ /*
+ * Constant for representing start or end time in standard local time mode,
+ * based on timezone's raw offset from GMT, does not include Daylight
+ * savings.
+ */
+ public static final int STANDARD_TIME = 1;
+
+ /*
+ * Constant for representing start or end time in local wall clock time
+ * mode, based on timezone's adjusted offset from GMT, it does include
+ * Daylight savings.
+ */
+ public static final int WALL_TIME = 0;
+
+ private boolean useDaylight;
+
+ private GregorianCalendar daylightSavings;
+
+ private int dstSavings = 3600000;
+
+ /**
+ * Constructs a new SimpleTimeZone using the specified offset for standard
+ * time from GMT and the specified time zone ID.
+ *
+ * @param offset
+ * the offset from GMT of standard time in milliseconds
+ * @param name
+ * the time zone ID
+ */
+ public SimpleTimeZone(int offset, String name) {
+ setID(name);
+ rawOffset = offset;
+ }
+
+ /**
+ * Constructs a new SimpleTimeZone using the specified offset for standard
+ * time from GMT, the specified time zone ID and the rules for daylight
+ * savings time.
+ *
+ * @param offset
+ * the offset from GMT of standard time in milliseconds
+ * @param name
+ * the time zone ID
+ * @param startMonth
+ * the Calendar month in which daylight savings time starts
+ * @param startDay
+ * the occurrence of the day of the week on which daylight
+ * savings time starts
+ * @param startDayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * starts
+ * @param startTime
+ * the time of day in milliseconds on which daylight savings time
+ * starts
+ * @param endMonth
+ * the Calendar month in which daylight savings time ends
+ * @param endDay
+ * the occurrence of the day of the week on which daylight
+ * savings time ends
+ * @param endDayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * ends
+ * @param endTime
+ * the time of day in milliseconds standard time on which
+ * daylight savings time ends
+ */
+ public SimpleTimeZone(int offset, String name, int startMonth,
+ int startDay, int startDayOfWeek, int startTime, int endMonth,
+ int endDay, int endDayOfWeek, int endTime) {
+ this(offset, name, startMonth, startDay, startDayOfWeek, startTime,
+ endMonth, endDay, endDayOfWeek, endTime, 3600000);
+ }
+
+ /**
+ * Constructs a new SimpleTimeZone using the specified offset for standard
+ * time from GMT, the specified time zone ID and the rules for daylight
+ * savings time.
+ *
+ * @param offset
+ * the offset from GMT of standard time in milliseconds
+ * @param name
+ * the time zone ID
+ * @param startMonth
+ * the Calendar month in which daylight savings time starts
+ * @param startDay
+ * the occurrence of the day of the week on which daylight
+ * savings time starts
+ * @param startDayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * starts
+ * @param startTime
+ * the time of day in milliseconds on which daylight savings time
+ * starts
+ * @param endMonth
+ * the Calendar month in which daylight savings time ends
+ * @param endDay
+ * the occurrence of the day of the week on which daylight
+ * savings time ends
+ * @param endDayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * ends
+ * @param endTime
+ * the time of day in milliseconds standard time on which
+ * daylight savings time ends
+ * @param daylightSavings
+ * the daylight savings time difference in milliseconds
+ */
+ public SimpleTimeZone(int offset, String name, int startMonth,
+ int startDay, int startDayOfWeek, int startTime, int endMonth,
+ int endDay, int endDayOfWeek, int endTime, int daylightSavings) {
+ this(offset, name);
+ if (daylightSavings <= 0)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e9", daylightSavings)); //$NON-NLS-1$
+ dstSavings = daylightSavings;
+
+ setStartRule(startMonth, startDay, startDayOfWeek, startTime);
+ setEndRule(endMonth, endDay, endDayOfWeek, endTime);
+ }
+
+ /**
+ * Constructs a new SimpleTimeZone using the specified offset for standard
+ * time from GMT, the specified time zone ID, the rules for daylight savings
+ * time, and the modes indicating UTC, standard, or wall time.
+ *
+ * @param offset
+ * the offset from GMT of standard time in milliseconds
+ * @param name
+ * the time zone ID
+ * @param startMonth
+ * the Calendar month in which daylight savings time starts
+ * @param startDay
+ * the occurrence of the day of the week on which daylight
+ * savings time starts
+ * @param startDayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * starts
+ * @param startTime
+ * the time of day in milliseconds on which daylight savings time
+ * starts
+ * @param startTimeMode
+ * the mode (UTC, standard, or wall time) of the start time value
+ * @param endMonth
+ * the Calendar month in which daylight savings time ends
+ * @param endDay
+ * the occurrence of the day of the week on which daylight
+ * savings time ends
+ * @param endDayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * ends
+ * @param endTime
+ * the time of day in milliseconds standard time on which
+ * daylight savings time ends
+ * @param endTimeMode
+ * the mode (UTC, standard, or wall time) of the end time value
+ * @param daylightSavings
+ * the daylight savings time difference in milliseconds
+ */
+ public SimpleTimeZone(int offset, String name, int startMonth,
+ int startDay, int startDayOfWeek, int startTime, int startTimeMode,
+ int endMonth, int endDay, int endDayOfWeek, int endTime,
+ int endTimeMode, int daylightSavings) {
+
+ this(offset, name, startMonth, startDay, startDayOfWeek, startTime,
+ endMonth, endDay, endDayOfWeek, endTime, daylightSavings);
+
+ if (startTimeMode > 4 || startTimeMode < 1) {
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e9", startTimeMode)); //$NON-NLS-1$
+ }
+ startMode = startTimeMode;
+
+ if (endTimeMode > 4 || endTimeMode < 1) {
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e9", endTimeMode)); //$NON-NLS-1$
+ }
+ endMode = endTimeMode;
+ }
+
+ /**
+ * Answers a new SimpleTimeZone with the same ID, rawOffset and daylight
+ * savings time rules as this SimpleTimeZone.
+ *
+ * @return a shallow copy of this SimpleTimeZone
+ *
+ * @see java.lang.Cloneable
+ */
+ public Object clone() {
+ SimpleTimeZone zone = (SimpleTimeZone) super.clone();
+ if (daylightSavings != null)
+ zone.daylightSavings = (GregorianCalendar) daylightSavings.clone();
+ return zone;
+ }
+
+ /**
+ * Compares the specified object to this SimpleTimeZone and answer if they
+ * are equal. The object must be an instance of SimpleTimeZone and have the
+ * same properties.
+ *
+ * @param object
+ * the object to compare with this object
+ * @return true if the specified object is equal to this SimpleTimeZone,
+ * false otherwise
+ *
+ * @see #hashCode
+ */
+ public boolean equals(Object object) {
+ if (!(object instanceof SimpleTimeZone))
+ return false;
+ SimpleTimeZone tz = (SimpleTimeZone) object;
+ return getID().equals(tz.getID())
+ && rawOffset == tz.rawOffset
+ && useDaylight == tz.useDaylight
+ && (!useDaylight || (startYear == tz.startYear
+ && startMonth == tz.startMonth
+ && startDay == tz.startDay && startMode == tz.startMode
+ && startDayOfWeek == tz.startDayOfWeek
+ && startTime == tz.startTime && endMonth == tz.endMonth
+ && endDay == tz.endDay
+ && endDayOfWeek == tz.endDayOfWeek
+ && endTime == tz.endTime && endMode == tz.endMode && dstSavings == tz.dstSavings));
+ }
+
+ /**
+ * Gets the daylight savings offset in milliseconds for this SimpleTimeZone.
+ *
+ * If this SimpleTimezone does not observe daylight savings, returns 0.
+ *
+ * @return the daylight savings offset in milliseconds
+ */
+ public int getDSTSavings() {
+ if (!useDaylight)
+ return 0;
+ return dstSavings;
+ }
+
+ /**
+ * Gets the offset from GMT of this SimpleTimeZone for the specified date
+ * and time. The offset includes daylight savings time if the specified date
+ * and time are within the daylight savings time period.
+ *
+ * @param era
+ * the GregorianCalendar era, either GregorianCalendar.BC or
+ * GregorianCalendar.AD
+ * @param year
+ * the year
+ * @param month
+ * the Calendar month
+ * @param day
+ * the day of the month
+ * @param dayOfWeek
+ * the Calendar day of the week
+ * @param time
+ * the time of day in milliseconds
+ * @return the offset from GMT in milliseconds
+ */
+ public int getOffset(int era, int year, int month, int day, int dayOfWeek,
+ int time) {
+ if (era != GregorianCalendar.BC && era != GregorianCalendar.AD)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00ea", era)); //$NON-NLS-1$
+ checkRange(month, dayOfWeek, time);
+ if (month != Calendar.FEBRUARY || day != 29 || !isLeapYear(year))
+ checkDay(month, day);
+
+ if (!useDaylightTime() || era != GregorianCalendar.AD
+ || year < startYear)
+ return rawOffset;
+ if (endMonth < startMonth) {
+ if (month > endMonth && month < startMonth)
+ return rawOffset;
+ } else {
+ if (month < startMonth || month > endMonth)
+ return rawOffset;
+ }
+
+ int ruleDay = 0, daysInMonth, firstDayOfMonth = mod7(dayOfWeek - day);
+ if (month == startMonth) {
+ switch (startMode) {
+ case DOM_MODE:
+ ruleDay = startDay;
+ break;
+ case DOW_IN_MONTH_MODE:
+ if (startDay >= 0) {
+ ruleDay = mod7(startDayOfWeek - firstDayOfMonth) + 1
+ + (startDay - 1) * 7;
+ } else {
+ daysInMonth = GregorianCalendar.DaysInMonth[startMonth];
+ if (startMonth == Calendar.FEBRUARY && isLeapYear(year))
+ daysInMonth += 1;
+ ruleDay = daysInMonth
+ + 1
+ + mod7(startDayOfWeek
+ - (firstDayOfMonth + daysInMonth))
+ + startDay * 7;
+ }
+ break;
+ case DOW_GE_DOM_MODE:
+ ruleDay = startDay
+ + mod7(startDayOfWeek
+ - (firstDayOfMonth + startDay - 1));
+ break;
+ case DOW_LE_DOM_MODE:
+ ruleDay = startDay
+ + mod7(startDayOfWeek
+ - (firstDayOfMonth + startDay - 1));
+ if (ruleDay != startDay)
+ ruleDay -= 7;
+ break;
+ }
+ if (ruleDay > day || ruleDay == day && time < startTime)
+ return rawOffset;
+ }
+
+ int ruleTime = endTime - dstSavings;
+ int nextMonth = (month + 1) % 12;
+ if (month == endMonth || (ruleTime < 0 && nextMonth == endMonth)) {
+ switch (endMode) {
+ case DOM_MODE:
+ ruleDay = endDay;
+ break;
+ case DOW_IN_MONTH_MODE:
+ if (endDay >= 0) {
+ ruleDay = mod7(endDayOfWeek - firstDayOfMonth) + 1
+ + (endDay - 1) * 7;
+ } else {
+ daysInMonth = GregorianCalendar.DaysInMonth[endMonth];
+ if (endMonth == Calendar.FEBRUARY && isLeapYear(year))
+ daysInMonth++;
+ ruleDay = daysInMonth
+ + 1
+ + mod7(endDayOfWeek
+ - (firstDayOfMonth + daysInMonth)) + endDay
+ * 7;
+ }
+ break;
+ case DOW_GE_DOM_MODE:
+ ruleDay = endDay
+ + mod7(endDayOfWeek - (firstDayOfMonth + endDay - 1));
+ break;
+ case DOW_LE_DOM_MODE:
+ ruleDay = endDay
+ + mod7(endDayOfWeek - (firstDayOfMonth + endDay - 1));
+ if (ruleDay != endDay)
+ ruleDay -= 7;
+ break;
+ }
+
+ int ruleMonth = endMonth;
+ if (ruleTime < 0) {
+ int changeDays = 1 - (ruleTime / 86400000);
+ ruleTime = (ruleTime % 86400000) + 86400000;
+ ruleDay -= changeDays;
+ if (ruleDay <= 0) {
+ if (--ruleMonth < Calendar.JANUARY)
+ ruleMonth = Calendar.DECEMBER;
+ ruleDay += GregorianCalendar.DaysInMonth[ruleMonth];
+ if (ruleMonth == Calendar.FEBRUARY && isLeapYear(year))
+ ruleDay++;
+ }
+ }
+
+ if (month == ruleMonth) {
+ if (ruleDay < day || ruleDay == day && time >= ruleTime)
+ return rawOffset;
+ } else if (nextMonth != ruleMonth)
+ return rawOffset;
+ }
+ return rawOffset + dstSavings;
+ }
+
+ /**
+ * Gets the offset from GMT of this SimpleTimeZone for the specified date.
+ * The offset includes daylight savings time if the specified date is within
+ * the daylight savings time period.
+ *
+ * @param time
+ * the date in milliseconds since January 1, 1970 00:00:00 GMT
+ * @return the offset from GMT in milliseconds
+ */
+ public int getOffset(long time) {
+ if (!useDaylightTime())
+ return rawOffset;
+ if (daylightSavings == null)
+ daylightSavings = new GregorianCalendar(this);
+ return daylightSavings.getOffset(time + rawOffset);
+ }
+
+ /**
+ * Gets the offset for standard time from GMT for this SimpleTimeZone.
+ *
+ * @return the offset from GMT of standard time in milliseconds
+ */
+ public int getRawOffset() {
+ return rawOffset;
+ }
+
+ /**
+ * Answers an integer hash code for the receiver. Objects which are equal
+ * answer the same value for this method.
+ *
+ * @return the receiver's hash
+ *
+ * @see #equals
+ */
+ public synchronized int hashCode() {
+ int hashCode = getID().hashCode() + rawOffset;
+ if (useDaylight)
+ hashCode += startYear + startMonth + startDay + startDayOfWeek
+ + startTime + startMode + endMonth + endDay + endDayOfWeek
+ + endTime + endMode + dstSavings;
+ return hashCode;
+ }
+
+ /**
+ * Answers if the specified TimeZone has the same raw offset and daylight
+ * savings time rules as this SimpleTimeZone.
+ *
+ * @param zone
+ * a TimeZone
+ * @return true when the TimeZones have the same raw offset and daylight
+ * savings time rules, false otherwise
+ */
+ public boolean hasSameRules(TimeZone zone) {
+ if (!(zone instanceof SimpleTimeZone))
+ return false;
+ SimpleTimeZone tz = (SimpleTimeZone) zone;
+ if (useDaylight != tz.useDaylight)
+ return false;
+ if (!useDaylight)
+ return rawOffset == tz.rawOffset;
+ return rawOffset == tz.rawOffset && dstSavings == tz.dstSavings
+ && startYear == tz.startYear && startMonth == tz.startMonth
+ && startDay == tz.startDay && startMode == tz.startMode
+ && startDayOfWeek == tz.startDayOfWeek
+ && startTime == tz.startTime && endMonth == tz.endMonth
+ && endDay == tz.endDay && endDayOfWeek == tz.endDayOfWeek
+ && endTime == tz.endTime && endMode == tz.endMode;
+ }
+
+ /**
+ * Answers if the specified Date is in the daylight savings time period for
+ * this SimpleTimeZone.
+ *
+ * @param time
+ * a Date
+ * @return true when the Date is in the daylight savings time period, false
+ * otherwise
+ */
+ public boolean inDaylightTime(Date time) {
+ // check for null pointer
+ long millis = time.getTime();
+ if (!useDaylightTime())
+ return false;
+ if (daylightSavings == null)
+ daylightSavings = new GregorianCalendar(this);
+ return daylightSavings.getOffset(millis + rawOffset) != rawOffset;
+ }
+
+ private boolean isLeapYear(int year) {
+ if (year > 1582)
+ return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
+ else
+ return year % 4 == 0;
+ }
+
+ private int mod7(int num1) {
+ int rem = num1 % 7;
+ return (num1 < 0 && rem < 0) ? 7 + rem : rem;
+ }
+
+ /**
+ * Sets the daylight savings offset in milliseconds for this SimpleTimeZone.
+ *
+ * @param milliseconds
+ * the daylight savings offset in milliseconds
+ */
+ public void setDSTSavings(int milliseconds) {
+ if (milliseconds > 0)
+ dstSavings = milliseconds;
+ else
+ throw new IllegalArgumentException();
+ }
+
+ private void checkRange(int month, int dayOfWeek, int time) {
+ if (month < Calendar.JANUARY || month > Calendar.DECEMBER)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e5", month)); //$NON-NLS-1$
+ if (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e7", dayOfWeek)); //$NON-NLS-1$
+ if (time < 0 || time >= 24 * 3600000)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e8", time)); //$NON-NLS-1$
+ }
+
+ private void checkDay(int month, int day) {
+ if (day <= 0 || day > GregorianCalendar.DaysInMonth[month])
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg.getString(
+ "K00e6", day)); //$NON-NLS-1$
+ }
+
+ private void setEndMode() {
+ if (endDayOfWeek == 0)
+ endMode = DOM_MODE;
+ else if (endDayOfWeek < 0) {
+ endDayOfWeek = -endDayOfWeek;
+ if (endDay < 0) {
+ endDay = -endDay;
+ endMode = DOW_LE_DOM_MODE;
+ } else
+ endMode = DOW_GE_DOM_MODE;
+ } else
+ endMode = DOW_IN_MONTH_MODE;
+ useDaylight = startDay != 0 && endDay != 0;
+ if (endDay != 0) {
+ checkRange(endMonth, endMode == DOM_MODE ? 1 : endDayOfWeek,
+ endTime);
+ if (endMode != DOW_IN_MONTH_MODE)
+ checkDay(endMonth, endDay);
+ else {
+ if (endDay < -5 || endDay > 5)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg
+ .getString("K00f8", endDay)); //$NON-NLS-1$
+ }
+ }
+ if (endMode != DOM_MODE)
+ endDayOfWeek--;
+ }
+
+ /**
+ * Sets the rule which specifies the end of daylight savings time.
+ *
+ * @param month
+ * the Calendar month in which daylight savings time ends
+ * @param dayOfMonth
+ * the Calendar day of the month on which daylight savings time
+ * ends
+ * @param time
+ * the time of day in milliseconds standard time on which
+ * daylight savings time ends
+ */
+ public void setEndRule(int month, int dayOfMonth, int time) {
+ endMonth = month;
+ endDay = dayOfMonth;
+ endDayOfWeek = 0; // Initialize this value for hasSameRules()
+ endTime = time;
+ setEndMode();
+ }
+
+ /**
+ * Sets the rule which specifies the end of daylight savings time.
+ *
+ * @param month
+ * the Calendar month in which daylight savings time ends
+ * @param day
+ * the occurrence of the day of the week on which daylight
+ * savings time ends
+ * @param dayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * ends
+ * @param time
+ * the time of day in milliseconds standard time on which
+ * daylight savings time ends
+ */
+ public void setEndRule(int month, int day, int dayOfWeek, int time) {
+ endMonth = month;
+ endDay = day;
+ endDayOfWeek = dayOfWeek;
+ endTime = time;
+ setEndMode();
+ }
+
+ /**
+ * Sets the rule which specifies the end of daylight savings time.
+ *
+ * @param month
+ * the Calendar month in which daylight savings time ends
+ * @param day
+ * the Calendar day of the month
+ * @param dayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * ends
+ * @param time
+ * the time of day in milliseconds on which daylight savings time
+ * ends
+ * @param after
+ * selects the day after or before the day of month
+ */
+ public void setEndRule(int month, int day, int dayOfWeek, int time,
+ boolean after) {
+ endMonth = month;
+ endDay = after ? day : -day;
+ endDayOfWeek = -dayOfWeek;
+ endTime = time;
+ setEndMode();
+ }
+
+ /**
+ * Sets the offset for standard time from GMT for this SimpleTimeZone.
+ *
+ * @param offset
+ * the offset from GMT of standard time in milliseconds
+ */
+ public void setRawOffset(int offset) {
+ rawOffset = offset;
+ }
+
+ private void setStartMode() {
+ if (startDayOfWeek == 0)
+ startMode = DOM_MODE;
+ else if (startDayOfWeek < 0) {
+ startDayOfWeek = -startDayOfWeek;
+ if (startDay < 0) {
+ startDay = -startDay;
+ startMode = DOW_LE_DOM_MODE;
+ } else
+ startMode = DOW_GE_DOM_MODE;
+ } else
+ startMode = DOW_IN_MONTH_MODE;
+ useDaylight = startDay != 0 && endDay != 0;
+ if (startDay != 0) {
+ checkRange(startMonth, startMode == DOM_MODE ? 1 : startDayOfWeek,
+ startTime);
+ if (startMode != DOW_IN_MONTH_MODE)
+ checkDay(startMonth, startDay);
+ else {
+ if (startDay < -5 || startDay > 5)
+ throw new IllegalArgumentException(com.ibm.oti.util.Msg
+ .getString("K00f8", startDay)); //$NON-NLS-1$
+ }
+ }
+ if (startMode != DOM_MODE)
+ startDayOfWeek--;
+ }
+
+ /**
+ * Sets the rule which specifies the start of daylight savings time.
+ *
+ * @param month
+ * the Calendar month in which daylight savings time starts
+ * @param dayOfMonth
+ * the Calendar day of the month on which daylight savings time
+ * starts
+ * @param time
+ * the time of day in milliseconds on which daylight savings time
+ * starts
+ */
+ public void setStartRule(int month, int dayOfMonth, int time) {
+ startMonth = month;
+ startDay = dayOfMonth;
+ startDayOfWeek = 0; // Initialize this value for hasSameRules()
+ startTime = time;
+ setStartMode();
+ }
+
+ /**
+ * Sets the rule which specifies the start of daylight savings time.
+ *
+ * @param month
+ * the Calendar month in which daylight savings time starts
+ * @param day
+ * the occurrence of the day of the week on which daylight
+ * savings time starts
+ * @param dayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * starts
+ * @param time
+ * the time of day in milliseconds on which daylight savings time
+ * starts
+ */
+ public void setStartRule(int month, int day, int dayOfWeek, int time) {
+ startMonth = month;
+ startDay = day;
+ startDayOfWeek = dayOfWeek;
+ startTime = time;
+ setStartMode();
+ }
+
+ /**
+ * Sets the rule which specifies the start of daylight savings time.
+ *
+ * @param month
+ * the Calendar month in which daylight savings time starts
+ * @param day
+ * the Calendar day of the month
+ * @param dayOfWeek
+ * the Calendar day of the week on which daylight savings time
+ * starts
+ * @param time
+ * the time of day in milliseconds on which daylight savings time
+ * starts
+ * @param after
+ * selects the day after or before the day of month
+ */
+ public void setStartRule(int month, int day, int dayOfWeek, int time,
+ boolean after) {
+ startMonth = month;
+ startDay = after ? day : -day;
+ startDayOfWeek = -dayOfWeek;
+ startTime = time;
+ setStartMode();
+ }
+
+ /**
+ * Sets the starting year for daylight savings time in this SimpleTimeZone.
+ * Years before this start year will always be in standard time.
+ *
+ * @param year
+ * the starting year
+ */
+ public void setStartYear(int year) {
+ startYear = year;
+ useDaylight = true;
+ }
+
+ /**
+ * Answers the string representation of this SimpleTimeZone.
+ *
+ * @return the string representation of this SimpleTimeZone
+ */
+ public String toString() {
+ return getClass().getName()
+ + "[id=" //$NON-NLS-1$
+ + getID()
+ + ",offset=" //$NON-NLS-1$
+ + rawOffset
+ + ",dstSavings=" //$NON-NLS-1$
+ + dstSavings
+ + ",useDaylight=" //$NON-NLS-1$
+ + useDaylight
+ + ",startYear=" //$NON-NLS-1$
+ + startYear
+ + ",startMode=" //$NON-NLS-1$
+ + startMode
+ + ",startMonth=" //$NON-NLS-1$
+ + startMonth
+ + ",startDay=" //$NON-NLS-1$
+ + startDay
+ + ",startDayOfWeek=" //$NON-NLS-1$
+ + (useDaylight && (startMode != DOM_MODE) ? startDayOfWeek + 1
+ : 0) + ",startTime=" + startTime + ",endMode=" //$NON-NLS-1$ //$NON-NLS-2$
+ + endMode + ",endMonth=" + endMonth + ",endDay=" + endDay //$NON-NLS-1$ //$NON-NLS-2$
+ + ",endDayOfWeek=" //$NON-NLS-1$
+ + (useDaylight && (endMode != DOM_MODE) ? endDayOfWeek + 1 : 0)
+ + ",endTime=" + endTime + "]"; //$NON-NLS-1$//$NON-NLS-2$
+ }
+
+ /**
+ * Answers if this TimeZone has a daylight savings time period.
+ *
+ * @return true if this time zone has a daylight savings time period, false
+ * otherwise
+ */
+ public boolean useDaylightTime() {
+ return useDaylight;
+ }
+
+ private static final ObjectStreamField[] serialPersistentFields = {
+ new ObjectStreamField("dstSavings", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("endDay", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("endDayOfWeek", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("endMode", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("endMonth", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("endTime", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("monthLength", byte[].class), //$NON-NLS-1$
+ new ObjectStreamField("rawOffset", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("serialVersionOnStream", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("startDay", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("startDayOfWeek", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("startMode", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("startMonth", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("startTime", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("startYear", Integer.TYPE), //$NON-NLS-1$
+ new ObjectStreamField("useDaylight", Boolean.TYPE), }; //$NON-NLS-1$
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ int sEndDay = endDay, sEndDayOfWeek = endDayOfWeek + 1, sStartDay = startDay, sStartDayOfWeek = startDayOfWeek + 1;
+ if (useDaylight
+ && (startMode != DOW_IN_MONTH_MODE || endMode != DOW_IN_MONTH_MODE)) {
+ Calendar cal = new GregorianCalendar(this);
+ if (endMode != DOW_IN_MONTH_MODE) {
+ cal.set(Calendar.MONTH, endMonth);
+ cal.set(Calendar.DATE, endDay);
+ sEndDay = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
+ if (endMode == DOM_MODE)
+ sEndDayOfWeek = cal.getFirstDayOfWeek();
+ }
+ if (startMode != DOW_IN_MONTH_MODE) {
+ cal.set(Calendar.MONTH, startMonth);
+ cal.set(Calendar.DATE, startDay);
+ sStartDay = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
+ if (startMode == DOM_MODE)
+ sStartDayOfWeek = cal.getFirstDayOfWeek();
+ }
+ }
+ ObjectOutputStream.PutField fields = stream.putFields();
+ fields.put("dstSavings", dstSavings); //$NON-NLS-1$
+ fields.put("endDay", sEndDay); //$NON-NLS-1$
+ fields.put("endDayOfWeek", sEndDayOfWeek); //$NON-NLS-1$
+ fields.put("endMode", endMode); //$NON-NLS-1$
+ fields.put("endMonth", endMonth); //$NON-NLS-1$
+ fields.put("endTime", endTime); //$NON-NLS-1$
+ fields.put("monthLength", GregorianCalendar.DaysInMonth); //$NON-NLS-1$
+ fields.put("rawOffset", rawOffset); //$NON-NLS-1$
+ fields.put("serialVersionOnStream", 1); //$NON-NLS-1$
+ fields.put("startDay", sStartDay); //$NON-NLS-1$
+ fields.put("startDayOfWeek", sStartDayOfWeek); //$NON-NLS-1$
+ fields.put("startMode", startMode); //$NON-NLS-1$
+ fields.put("startMonth", startMonth); //$NON-NLS-1$
+ fields.put("startTime", startTime); //$NON-NLS-1$
+ fields.put("startYear", startYear); //$NON-NLS-1$
+ fields.put("useDaylight", useDaylight); //$NON-NLS-1$
+ stream.writeFields();
+ stream.writeInt(4);
+ byte[] values = new byte[4];
+ values[0] = (byte) startDay;
+ values[1] = (byte) (startMode == DOM_MODE ? 0 : startDayOfWeek + 1);
+ values[2] = (byte) endDay;
+ values[3] = (byte) (endMode == DOM_MODE ? 0 : endDayOfWeek + 1);
+ stream.write(values);
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream.GetField fields = stream.readFields();
+ rawOffset = fields.get("rawOffset", 0); //$NON-NLS-1$
+ useDaylight = fields.get("useDaylight", false); //$NON-NLS-1$
+ if (useDaylight) {
+ endMonth = fields.get("endMonth", 0); //$NON-NLS-1$
+ endTime = fields.get("endTime", 0); //$NON-NLS-1$
+ startMonth = fields.get("startMonth", 0); //$NON-NLS-1$
+ startTime = fields.get("startTime", 0); //$NON-NLS-1$
+ startYear = fields.get("startYear", 0); //$NON-NLS-1$
+ }
+ if (fields.get("serialVersionOnStream", 0) == 0) { //$NON-NLS-1$
+ if (useDaylight) {
+ startMode = endMode = DOW_IN_MONTH_MODE;
+ endDay = fields.get("endDay", 0); //$NON-NLS-1$
+ endDayOfWeek = fields.get("endDayOfWeek", 0) - 1; //$NON-NLS-1$
+ startDay = fields.get("startDay", 0); //$NON-NLS-1$
+ startDayOfWeek = fields.get("startDayOfWeek", 0) - 1; //$NON-NLS-1$
+ }
+ } else {
+ dstSavings = fields.get("dstSavings", 0); //$NON-NLS-1$
+ if (useDaylight) {
+ endMode = fields.get("endMode", 0); //$NON-NLS-1$
+ startMode = fields.get("startMode", 0); //$NON-NLS-1$
+ int length = stream.readInt();
+ byte[] values = new byte[length];
+ stream.readFully(values);
+ if (length >= 4) {
+ startDay = values[0];
+ startDayOfWeek = values[1];
+ if (startMode != DOM_MODE)
+ startDayOfWeek--;
+ endDay = values[2];
+ endDayOfWeek = values[3];
+ if (endMode != DOM_MODE)
+ endDayOfWeek--;
+ }
+ }
+ }
+ }
+
+}
|