emul/src/main/java/java/lang/Object.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 06:25:28 +0100
changeset 232 36f16c49bdef
parent 49 0a115f1c6f3c
child 233 5e8f219d60ba
child 239 8ceee38f5840
permissions -rw-r--r--
Throw exception when reaching native method without JavaScript implementation
jaroslav@49
     1
/*
jaroslav@49
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@49
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@49
     4
 *
jaroslav@49
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@49
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@49
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@49
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@49
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@49
    10
 *
jaroslav@49
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@49
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@49
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@49
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@49
    15
 * accompanied this code).
jaroslav@49
    16
 *
jaroslav@49
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@49
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@49
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@49
    20
 *
jaroslav@49
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@49
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@49
    23
 * questions.
jaroslav@49
    24
 */
jaroslav@49
    25
jaroslav@49
    26
package java.lang;
jaroslav@49
    27
jaroslav@232
    28
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@232
    29
jaroslav@49
    30
/**
jaroslav@49
    31
 * Class {@code Object} is the root of the class hierarchy.
jaroslav@49
    32
 * Every class has {@code Object} as a superclass. All objects,
jaroslav@49
    33
 * including arrays, implement the methods of this class.
jaroslav@49
    34
 *
jaroslav@49
    35
 * @author  unascribed
jaroslav@49
    36
 * @see     java.lang.Class
jaroslav@49
    37
 * @since   JDK1.0
jaroslav@49
    38
 */
jaroslav@49
    39
public class Object {
jaroslav@49
    40
jaroslav@232
    41
    @JavaScriptBody(args = {}, body = "")
jaroslav@49
    42
    private static native void registerNatives();
jaroslav@49
    43
    static {
jaroslav@49
    44
        registerNatives();
jaroslav@49
    45
    }
jaroslav@49
    46
jaroslav@49
    47
    /**
jaroslav@49
    48
     * Returns the runtime class of this {@code Object}. The returned
jaroslav@49
    49
     * {@code Class} object is the object that is locked by {@code
jaroslav@49
    50
     * static synchronized} methods of the represented class.
jaroslav@49
    51
     *
jaroslav@49
    52
     * <p><b>The actual result type is {@code Class<? extends |X|>}
jaroslav@49
    53
     * where {@code |X|} is the erasure of the static type of the
jaroslav@49
    54
     * expression on which {@code getClass} is called.</b> For
jaroslav@49
    55
     * example, no cast is required in this code fragment:</p>
jaroslav@49
    56
     *
jaroslav@49
    57
     * <p>
jaroslav@49
    58
     * {@code Number n = 0;                             }<br>
jaroslav@49
    59
     * {@code Class<? extends Number> c = n.getClass(); }
jaroslav@49
    60
     * </p>
jaroslav@49
    61
     *
jaroslav@49
    62
     * @return The {@code Class} object that represents the runtime
jaroslav@49
    63
     *         class of this object.
jaroslav@49
    64
     * @see    Class Literals, section 15.8.2 of
jaroslav@49
    65
     *         <cite>The Java&trade; Language Specification</cite>.
jaroslav@49
    66
     */
jaroslav@49
    67
    public final native Class<?> getClass();
jaroslav@49
    68
jaroslav@49
    69
    /**
jaroslav@49
    70
     * Returns a hash code value for the object. This method is
jaroslav@49
    71
     * supported for the benefit of hash tables such as those provided by
jaroslav@49
    72
     * {@link java.util.HashMap}.
jaroslav@49
    73
     * <p>
jaroslav@49
    74
     * The general contract of {@code hashCode} is:
jaroslav@49
    75
     * <ul>
jaroslav@49
    76
     * <li>Whenever it is invoked on the same object more than once during
jaroslav@49
    77
     *     an execution of a Java application, the {@code hashCode} method
jaroslav@49
    78
     *     must consistently return the same integer, provided no information
jaroslav@49
    79
     *     used in {@code equals} comparisons on the object is modified.
jaroslav@49
    80
     *     This integer need not remain consistent from one execution of an
jaroslav@49
    81
     *     application to another execution of the same application.
jaroslav@49
    82
     * <li>If two objects are equal according to the {@code equals(Object)}
jaroslav@49
    83
     *     method, then calling the {@code hashCode} method on each of
jaroslav@49
    84
     *     the two objects must produce the same integer result.
jaroslav@49
    85
     * <li>It is <em>not</em> required that if two objects are unequal
jaroslav@49
    86
     *     according to the {@link java.lang.Object#equals(java.lang.Object)}
jaroslav@49
    87
     *     method, then calling the {@code hashCode} method on each of the
jaroslav@49
    88
     *     two objects must produce distinct integer results.  However, the
jaroslav@49
    89
     *     programmer should be aware that producing distinct integer results
jaroslav@49
    90
     *     for unequal objects may improve the performance of hash tables.
jaroslav@49
    91
     * </ul>
jaroslav@49
    92
     * <p>
jaroslav@49
    93
     * As much as is reasonably practical, the hashCode method defined by
jaroslav@49
    94
     * class {@code Object} does return distinct integers for distinct
jaroslav@49
    95
     * objects. (This is typically implemented by converting the internal
jaroslav@49
    96
     * address of the object into an integer, but this implementation
jaroslav@49
    97
     * technique is not required by the
jaroslav@49
    98
     * Java<font size="-2"><sup>TM</sup></font> programming language.)
jaroslav@49
    99
     *
jaroslav@49
   100
     * @return  a hash code value for this object.
jaroslav@49
   101
     * @see     java.lang.Object#equals(java.lang.Object)
jaroslav@49
   102
     * @see     java.lang.System#identityHashCode
jaroslav@49
   103
     */
jaroslav@49
   104
    public native int hashCode();
jaroslav@49
   105
jaroslav@49
   106
    /**
jaroslav@49
   107
     * Indicates whether some other object is "equal to" this one.
jaroslav@49
   108
     * <p>
jaroslav@49
   109
     * The {@code equals} method implements an equivalence relation
jaroslav@49
   110
     * on non-null object references:
jaroslav@49
   111
     * <ul>
jaroslav@49
   112
     * <li>It is <i>reflexive</i>: for any non-null reference value
jaroslav@49
   113
     *     {@code x}, {@code x.equals(x)} should return
jaroslav@49
   114
     *     {@code true}.
jaroslav@49
   115
     * <li>It is <i>symmetric</i>: for any non-null reference values
jaroslav@49
   116
     *     {@code x} and {@code y}, {@code x.equals(y)}
jaroslav@49
   117
     *     should return {@code true} if and only if
jaroslav@49
   118
     *     {@code y.equals(x)} returns {@code true}.
jaroslav@49
   119
     * <li>It is <i>transitive</i>: for any non-null reference values
jaroslav@49
   120
     *     {@code x}, {@code y}, and {@code z}, if
jaroslav@49
   121
     *     {@code x.equals(y)} returns {@code true} and
jaroslav@49
   122
     *     {@code y.equals(z)} returns {@code true}, then
jaroslav@49
   123
     *     {@code x.equals(z)} should return {@code true}.
jaroslav@49
   124
     * <li>It is <i>consistent</i>: for any non-null reference values
jaroslav@49
   125
     *     {@code x} and {@code y}, multiple invocations of
jaroslav@49
   126
     *     {@code x.equals(y)} consistently return {@code true}
jaroslav@49
   127
     *     or consistently return {@code false}, provided no
jaroslav@49
   128
     *     information used in {@code equals} comparisons on the
jaroslav@49
   129
     *     objects is modified.
jaroslav@49
   130
     * <li>For any non-null reference value {@code x},
jaroslav@49
   131
     *     {@code x.equals(null)} should return {@code false}.
jaroslav@49
   132
     * </ul>
jaroslav@49
   133
     * <p>
jaroslav@49
   134
     * The {@code equals} method for class {@code Object} implements
jaroslav@49
   135
     * the most discriminating possible equivalence relation on objects;
jaroslav@49
   136
     * that is, for any non-null reference values {@code x} and
jaroslav@49
   137
     * {@code y}, this method returns {@code true} if and only
jaroslav@49
   138
     * if {@code x} and {@code y} refer to the same object
jaroslav@49
   139
     * ({@code x == y} has the value {@code true}).
jaroslav@49
   140
     * <p>
jaroslav@49
   141
     * Note that it is generally necessary to override the {@code hashCode}
jaroslav@49
   142
     * method whenever this method is overridden, so as to maintain the
jaroslav@49
   143
     * general contract for the {@code hashCode} method, which states
jaroslav@49
   144
     * that equal objects must have equal hash codes.
jaroslav@49
   145
     *
jaroslav@49
   146
     * @param   obj   the reference object with which to compare.
jaroslav@49
   147
     * @return  {@code true} if this object is the same as the obj
jaroslav@49
   148
     *          argument; {@code false} otherwise.
jaroslav@49
   149
     * @see     #hashCode()
jaroslav@49
   150
     * @see     java.util.HashMap
jaroslav@49
   151
     */
jaroslav@49
   152
    public boolean equals(Object obj) {
jaroslav@49
   153
        return (this == obj);
jaroslav@49
   154
    }
jaroslav@49
   155
jaroslav@49
   156
    /**
jaroslav@49
   157
     * Creates and returns a copy of this object.  The precise meaning
jaroslav@49
   158
     * of "copy" may depend on the class of the object. The general
jaroslav@49
   159
     * intent is that, for any object {@code x}, the expression:
jaroslav@49
   160
     * <blockquote>
jaroslav@49
   161
     * <pre>
jaroslav@49
   162
     * x.clone() != x</pre></blockquote>
jaroslav@49
   163
     * will be true, and that the expression:
jaroslav@49
   164
     * <blockquote>
jaroslav@49
   165
     * <pre>
jaroslav@49
   166
     * x.clone().getClass() == x.getClass()</pre></blockquote>
jaroslav@49
   167
     * will be {@code true}, but these are not absolute requirements.
jaroslav@49
   168
     * While it is typically the case that:
jaroslav@49
   169
     * <blockquote>
jaroslav@49
   170
     * <pre>
jaroslav@49
   171
     * x.clone().equals(x)</pre></blockquote>
jaroslav@49
   172
     * will be {@code true}, this is not an absolute requirement.
jaroslav@49
   173
     * <p>
jaroslav@49
   174
     * By convention, the returned object should be obtained by calling
jaroslav@49
   175
     * {@code super.clone}.  If a class and all of its superclasses (except
jaroslav@49
   176
     * {@code Object}) obey this convention, it will be the case that
jaroslav@49
   177
     * {@code x.clone().getClass() == x.getClass()}.
jaroslav@49
   178
     * <p>
jaroslav@49
   179
     * By convention, the object returned by this method should be independent
jaroslav@49
   180
     * of this object (which is being cloned).  To achieve this independence,
jaroslav@49
   181
     * it may be necessary to modify one or more fields of the object returned
jaroslav@49
   182
     * by {@code super.clone} before returning it.  Typically, this means
jaroslav@49
   183
     * copying any mutable objects that comprise the internal "deep structure"
jaroslav@49
   184
     * of the object being cloned and replacing the references to these
jaroslav@49
   185
     * objects with references to the copies.  If a class contains only
jaroslav@49
   186
     * primitive fields or references to immutable objects, then it is usually
jaroslav@49
   187
     * the case that no fields in the object returned by {@code super.clone}
jaroslav@49
   188
     * need to be modified.
jaroslav@49
   189
     * <p>
jaroslav@49
   190
     * The method {@code clone} for class {@code Object} performs a
jaroslav@49
   191
     * specific cloning operation. First, if the class of this object does
jaroslav@49
   192
     * not implement the interface {@code Cloneable}, then a
jaroslav@49
   193
     * {@code CloneNotSupportedException} is thrown. Note that all arrays
jaroslav@49
   194
     * are considered to implement the interface {@code Cloneable} and that
jaroslav@49
   195
     * the return type of the {@code clone} method of an array type {@code T[]}
jaroslav@49
   196
     * is {@code T[]} where T is any reference or primitive type.
jaroslav@49
   197
     * Otherwise, this method creates a new instance of the class of this
jaroslav@49
   198
     * object and initializes all its fields with exactly the contents of
jaroslav@49
   199
     * the corresponding fields of this object, as if by assignment; the
jaroslav@49
   200
     * contents of the fields are not themselves cloned. Thus, this method
jaroslav@49
   201
     * performs a "shallow copy" of this object, not a "deep copy" operation.
jaroslav@49
   202
     * <p>
jaroslav@49
   203
     * The class {@code Object} does not itself implement the interface
jaroslav@49
   204
     * {@code Cloneable}, so calling the {@code clone} method on an object
jaroslav@49
   205
     * whose class is {@code Object} will result in throwing an
jaroslav@49
   206
     * exception at run time.
jaroslav@49
   207
     *
jaroslav@49
   208
     * @return     a clone of this instance.
jaroslav@49
   209
     * @exception  CloneNotSupportedException  if the object's class does not
jaroslav@49
   210
     *               support the {@code Cloneable} interface. Subclasses
jaroslav@49
   211
     *               that override the {@code clone} method can also
jaroslav@49
   212
     *               throw this exception to indicate that an instance cannot
jaroslav@49
   213
     *               be cloned.
jaroslav@49
   214
     * @see java.lang.Cloneable
jaroslav@49
   215
     */
jaroslav@49
   216
    protected native Object clone() throws CloneNotSupportedException;
jaroslav@49
   217
jaroslav@49
   218
    /**
jaroslav@49
   219
     * Returns a string representation of the object. In general, the
jaroslav@49
   220
     * {@code toString} method returns a string that
jaroslav@49
   221
     * "textually represents" this object. The result should
jaroslav@49
   222
     * be a concise but informative representation that is easy for a
jaroslav@49
   223
     * person to read.
jaroslav@49
   224
     * It is recommended that all subclasses override this method.
jaroslav@49
   225
     * <p>
jaroslav@49
   226
     * The {@code toString} method for class {@code Object}
jaroslav@49
   227
     * returns a string consisting of the name of the class of which the
jaroslav@49
   228
     * object is an instance, the at-sign character `{@code @}', and
jaroslav@49
   229
     * the unsigned hexadecimal representation of the hash code of the
jaroslav@49
   230
     * object. In other words, this method returns a string equal to the
jaroslav@49
   231
     * value of:
jaroslav@49
   232
     * <blockquote>
jaroslav@49
   233
     * <pre>
jaroslav@49
   234
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
jaroslav@49
   235
     * </pre></blockquote>
jaroslav@49
   236
     *
jaroslav@49
   237
     * @return  a string representation of the object.
jaroslav@49
   238
     */
jaroslav@49
   239
    public String toString() {
jaroslav@49
   240
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
jaroslav@49
   241
    }
jaroslav@49
   242
jaroslav@49
   243
    /**
jaroslav@49
   244
     * Wakes up a single thread that is waiting on this object's
jaroslav@49
   245
     * monitor. If any threads are waiting on this object, one of them
jaroslav@49
   246
     * is chosen to be awakened. The choice is arbitrary and occurs at
jaroslav@49
   247
     * the discretion of the implementation. A thread waits on an object's
jaroslav@49
   248
     * monitor by calling one of the {@code wait} methods.
jaroslav@49
   249
     * <p>
jaroslav@49
   250
     * The awakened thread will not be able to proceed until the current
jaroslav@49
   251
     * thread relinquishes the lock on this object. The awakened thread will
jaroslav@49
   252
     * compete in the usual manner with any other threads that might be
jaroslav@49
   253
     * actively competing to synchronize on this object; for example, the
jaroslav@49
   254
     * awakened thread enjoys no reliable privilege or disadvantage in being
jaroslav@49
   255
     * the next thread to lock this object.
jaroslav@49
   256
     * <p>
jaroslav@49
   257
     * This method should only be called by a thread that is the owner
jaroslav@49
   258
     * of this object's monitor. A thread becomes the owner of the
jaroslav@49
   259
     * object's monitor in one of three ways:
jaroslav@49
   260
     * <ul>
jaroslav@49
   261
     * <li>By executing a synchronized instance method of that object.
jaroslav@49
   262
     * <li>By executing the body of a {@code synchronized} statement
jaroslav@49
   263
     *     that synchronizes on the object.
jaroslav@49
   264
     * <li>For objects of type {@code Class,} by executing a
jaroslav@49
   265
     *     synchronized static method of that class.
jaroslav@49
   266
     * </ul>
jaroslav@49
   267
     * <p>
jaroslav@49
   268
     * Only one thread at a time can own an object's monitor.
jaroslav@49
   269
     *
jaroslav@49
   270
     * @exception  IllegalMonitorStateException  if the current thread is not
jaroslav@49
   271
     *               the owner of this object's monitor.
jaroslav@49
   272
     * @see        java.lang.Object#notifyAll()
jaroslav@49
   273
     * @see        java.lang.Object#wait()
jaroslav@49
   274
     */
jaroslav@49
   275
    public final native void notify();
jaroslav@49
   276
jaroslav@49
   277
    /**
jaroslav@49
   278
     * Wakes up all threads that are waiting on this object's monitor. A
jaroslav@49
   279
     * thread waits on an object's monitor by calling one of the
jaroslav@49
   280
     * {@code wait} methods.
jaroslav@49
   281
     * <p>
jaroslav@49
   282
     * The awakened threads will not be able to proceed until the current
jaroslav@49
   283
     * thread relinquishes the lock on this object. The awakened threads
jaroslav@49
   284
     * will compete in the usual manner with any other threads that might
jaroslav@49
   285
     * be actively competing to synchronize on this object; for example,
jaroslav@49
   286
     * the awakened threads enjoy no reliable privilege or disadvantage in
jaroslav@49
   287
     * being the next thread to lock this object.
jaroslav@49
   288
     * <p>
jaroslav@49
   289
     * This method should only be called by a thread that is the owner
jaroslav@49
   290
     * of this object's monitor. See the {@code notify} method for a
jaroslav@49
   291
     * description of the ways in which a thread can become the owner of
jaroslav@49
   292
     * a monitor.
jaroslav@49
   293
     *
jaroslav@49
   294
     * @exception  IllegalMonitorStateException  if the current thread is not
jaroslav@49
   295
     *               the owner of this object's monitor.
jaroslav@49
   296
     * @see        java.lang.Object#notify()
jaroslav@49
   297
     * @see        java.lang.Object#wait()
jaroslav@49
   298
     */
jaroslav@49
   299
    public final native void notifyAll();
jaroslav@49
   300
jaroslav@49
   301
    /**
jaroslav@49
   302
     * Causes the current thread to wait until either another thread invokes the
jaroslav@49
   303
     * {@link java.lang.Object#notify()} method or the
jaroslav@49
   304
     * {@link java.lang.Object#notifyAll()} method for this object, or a
jaroslav@49
   305
     * specified amount of time has elapsed.
jaroslav@49
   306
     * <p>
jaroslav@49
   307
     * The current thread must own this object's monitor.
jaroslav@49
   308
     * <p>
jaroslav@49
   309
     * This method causes the current thread (call it <var>T</var>) to
jaroslav@49
   310
     * place itself in the wait set for this object and then to relinquish
jaroslav@49
   311
     * any and all synchronization claims on this object. Thread <var>T</var>
jaroslav@49
   312
     * becomes disabled for thread scheduling purposes and lies dormant
jaroslav@49
   313
     * until one of four things happens:
jaroslav@49
   314
     * <ul>
jaroslav@49
   315
     * <li>Some other thread invokes the {@code notify} method for this
jaroslav@49
   316
     * object and thread <var>T</var> happens to be arbitrarily chosen as
jaroslav@49
   317
     * the thread to be awakened.
jaroslav@49
   318
     * <li>Some other thread invokes the {@code notifyAll} method for this
jaroslav@49
   319
     * object.
jaroslav@49
   320
     * <li>Some other thread {@linkplain Thread#interrupt() interrupts}
jaroslav@49
   321
     * thread <var>T</var>.
jaroslav@49
   322
     * <li>The specified amount of real time has elapsed, more or less.  If
jaroslav@49
   323
     * {@code timeout} is zero, however, then real time is not taken into
jaroslav@49
   324
     * consideration and the thread simply waits until notified.
jaroslav@49
   325
     * </ul>
jaroslav@49
   326
     * The thread <var>T</var> is then removed from the wait set for this
jaroslav@49
   327
     * object and re-enabled for thread scheduling. It then competes in the
jaroslav@49
   328
     * usual manner with other threads for the right to synchronize on the
jaroslav@49
   329
     * object; once it has gained control of the object, all its
jaroslav@49
   330
     * synchronization claims on the object are restored to the status quo
jaroslav@49
   331
     * ante - that is, to the situation as of the time that the {@code wait}
jaroslav@49
   332
     * method was invoked. Thread <var>T</var> then returns from the
jaroslav@49
   333
     * invocation of the {@code wait} method. Thus, on return from the
jaroslav@49
   334
     * {@code wait} method, the synchronization state of the object and of
jaroslav@49
   335
     * thread {@code T} is exactly as it was when the {@code wait} method
jaroslav@49
   336
     * was invoked.
jaroslav@49
   337
     * <p>
jaroslav@49
   338
     * A thread can also wake up without being notified, interrupted, or
jaroslav@49
   339
     * timing out, a so-called <i>spurious wakeup</i>.  While this will rarely
jaroslav@49
   340
     * occur in practice, applications must guard against it by testing for
jaroslav@49
   341
     * the condition that should have caused the thread to be awakened, and
jaroslav@49
   342
     * continuing to wait if the condition is not satisfied.  In other words,
jaroslav@49
   343
     * waits should always occur in loops, like this one:
jaroslav@49
   344
     * <pre>
jaroslav@49
   345
     *     synchronized (obj) {
jaroslav@49
   346
     *         while (&lt;condition does not hold&gt;)
jaroslav@49
   347
     *             obj.wait(timeout);
jaroslav@49
   348
     *         ... // Perform action appropriate to condition
jaroslav@49
   349
     *     }
jaroslav@49
   350
     * </pre>
jaroslav@49
   351
     * (For more information on this topic, see Section 3.2.3 in Doug Lea's
jaroslav@49
   352
     * "Concurrent Programming in Java (Second Edition)" (Addison-Wesley,
jaroslav@49
   353
     * 2000), or Item 50 in Joshua Bloch's "Effective Java Programming
jaroslav@49
   354
     * Language Guide" (Addison-Wesley, 2001).
jaroslav@49
   355
     *
jaroslav@49
   356
     * <p>If the current thread is {@linkplain java.lang.Thread#interrupt()
jaroslav@49
   357
     * interrupted} by any thread before or while it is waiting, then an
jaroslav@49
   358
     * {@code InterruptedException} is thrown.  This exception is not
jaroslav@49
   359
     * thrown until the lock status of this object has been restored as
jaroslav@49
   360
     * described above.
jaroslav@49
   361
     *
jaroslav@49
   362
     * <p>
jaroslav@49
   363
     * Note that the {@code wait} method, as it places the current thread
jaroslav@49
   364
     * into the wait set for this object, unlocks only this object; any
jaroslav@49
   365
     * other objects on which the current thread may be synchronized remain
jaroslav@49
   366
     * locked while the thread waits.
jaroslav@49
   367
     * <p>
jaroslav@49
   368
     * This method should only be called by a thread that is the owner
jaroslav@49
   369
     * of this object's monitor. See the {@code notify} method for a
jaroslav@49
   370
     * description of the ways in which a thread can become the owner of
jaroslav@49
   371
     * a monitor.
jaroslav@49
   372
     *
jaroslav@49
   373
     * @param      timeout   the maximum time to wait in milliseconds.
jaroslav@49
   374
     * @exception  IllegalArgumentException      if the value of timeout is
jaroslav@49
   375
     *               negative.
jaroslav@49
   376
     * @exception  IllegalMonitorStateException  if the current thread is not
jaroslav@49
   377
     *               the owner of the object's monitor.
jaroslav@49
   378
     * @exception  InterruptedException if any thread interrupted the
jaroslav@49
   379
     *             current thread before or while the current thread
jaroslav@49
   380
     *             was waiting for a notification.  The <i>interrupted
jaroslav@49
   381
     *             status</i> of the current thread is cleared when
jaroslav@49
   382
     *             this exception is thrown.
jaroslav@49
   383
     * @see        java.lang.Object#notify()
jaroslav@49
   384
     * @see        java.lang.Object#notifyAll()
jaroslav@49
   385
     */
jaroslav@49
   386
    public final native void wait(long timeout) throws InterruptedException;
jaroslav@49
   387
jaroslav@49
   388
    /**
jaroslav@49
   389
     * Causes the current thread to wait until another thread invokes the
jaroslav@49
   390
     * {@link java.lang.Object#notify()} method or the
jaroslav@49
   391
     * {@link java.lang.Object#notifyAll()} method for this object, or
jaroslav@49
   392
     * some other thread interrupts the current thread, or a certain
jaroslav@49
   393
     * amount of real time has elapsed.
jaroslav@49
   394
     * <p>
jaroslav@49
   395
     * This method is similar to the {@code wait} method of one
jaroslav@49
   396
     * argument, but it allows finer control over the amount of time to
jaroslav@49
   397
     * wait for a notification before giving up. The amount of real time,
jaroslav@49
   398
     * measured in nanoseconds, is given by:
jaroslav@49
   399
     * <blockquote>
jaroslav@49
   400
     * <pre>
jaroslav@49
   401
     * 1000000*timeout+nanos</pre></blockquote>
jaroslav@49
   402
     * <p>
jaroslav@49
   403
     * In all other respects, this method does the same thing as the
jaroslav@49
   404
     * method {@link #wait(long)} of one argument. In particular,
jaroslav@49
   405
     * {@code wait(0, 0)} means the same thing as {@code wait(0)}.
jaroslav@49
   406
     * <p>
jaroslav@49
   407
     * The current thread must own this object's monitor. The thread
jaroslav@49
   408
     * releases ownership of this monitor and waits until either of the
jaroslav@49
   409
     * following two conditions has occurred:
jaroslav@49
   410
     * <ul>
jaroslav@49
   411
     * <li>Another thread notifies threads waiting on this object's monitor
jaroslav@49
   412
     *     to wake up either through a call to the {@code notify} method
jaroslav@49
   413
     *     or the {@code notifyAll} method.
jaroslav@49
   414
     * <li>The timeout period, specified by {@code timeout}
jaroslav@49
   415
     *     milliseconds plus {@code nanos} nanoseconds arguments, has
jaroslav@49
   416
     *     elapsed.
jaroslav@49
   417
     * </ul>
jaroslav@49
   418
     * <p>
jaroslav@49
   419
     * The thread then waits until it can re-obtain ownership of the
jaroslav@49
   420
     * monitor and resumes execution.
jaroslav@49
   421
     * <p>
jaroslav@49
   422
     * As in the one argument version, interrupts and spurious wakeups are
jaroslav@49
   423
     * possible, and this method should always be used in a loop:
jaroslav@49
   424
     * <pre>
jaroslav@49
   425
     *     synchronized (obj) {
jaroslav@49
   426
     *         while (&lt;condition does not hold&gt;)
jaroslav@49
   427
     *             obj.wait(timeout, nanos);
jaroslav@49
   428
     *         ... // Perform action appropriate to condition
jaroslav@49
   429
     *     }
jaroslav@49
   430
     * </pre>
jaroslav@49
   431
     * This method should only be called by a thread that is the owner
jaroslav@49
   432
     * of this object's monitor. See the {@code notify} method for a
jaroslav@49
   433
     * description of the ways in which a thread can become the owner of
jaroslav@49
   434
     * a monitor.
jaroslav@49
   435
     *
jaroslav@49
   436
     * @param      timeout   the maximum time to wait in milliseconds.
jaroslav@49
   437
     * @param      nanos      additional time, in nanoseconds range
jaroslav@49
   438
     *                       0-999999.
jaroslav@49
   439
     * @exception  IllegalArgumentException      if the value of timeout is
jaroslav@49
   440
     *                      negative or the value of nanos is
jaroslav@49
   441
     *                      not in the range 0-999999.
jaroslav@49
   442
     * @exception  IllegalMonitorStateException  if the current thread is not
jaroslav@49
   443
     *               the owner of this object's monitor.
jaroslav@49
   444
     * @exception  InterruptedException if any thread interrupted the
jaroslav@49
   445
     *             current thread before or while the current thread
jaroslav@49
   446
     *             was waiting for a notification.  The <i>interrupted
jaroslav@49
   447
     *             status</i> of the current thread is cleared when
jaroslav@49
   448
     *             this exception is thrown.
jaroslav@49
   449
     */
jaroslav@49
   450
    public final void wait(long timeout, int nanos) throws InterruptedException {
jaroslav@49
   451
        if (timeout < 0) {
jaroslav@49
   452
            throw new IllegalArgumentException("timeout value is negative");
jaroslav@49
   453
        }
jaroslav@49
   454
jaroslav@49
   455
        if (nanos < 0 || nanos > 999999) {
jaroslav@49
   456
            throw new IllegalArgumentException(
jaroslav@49
   457
                                "nanosecond timeout value out of range");
jaroslav@49
   458
        }
jaroslav@49
   459
jaroslav@49
   460
        if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
jaroslav@49
   461
            timeout++;
jaroslav@49
   462
        }
jaroslav@49
   463
jaroslav@49
   464
        wait(timeout);
jaroslav@49
   465
    }
jaroslav@49
   466
jaroslav@49
   467
    /**
jaroslav@49
   468
     * Causes the current thread to wait until another thread invokes the
jaroslav@49
   469
     * {@link java.lang.Object#notify()} method or the
jaroslav@49
   470
     * {@link java.lang.Object#notifyAll()} method for this object.
jaroslav@49
   471
     * In other words, this method behaves exactly as if it simply
jaroslav@49
   472
     * performs the call {@code wait(0)}.
jaroslav@49
   473
     * <p>
jaroslav@49
   474
     * The current thread must own this object's monitor. The thread
jaroslav@49
   475
     * releases ownership of this monitor and waits until another thread
jaroslav@49
   476
     * notifies threads waiting on this object's monitor to wake up
jaroslav@49
   477
     * either through a call to the {@code notify} method or the
jaroslav@49
   478
     * {@code notifyAll} method. The thread then waits until it can
jaroslav@49
   479
     * re-obtain ownership of the monitor and resumes execution.
jaroslav@49
   480
     * <p>
jaroslav@49
   481
     * As in the one argument version, interrupts and spurious wakeups are
jaroslav@49
   482
     * possible, and this method should always be used in a loop:
jaroslav@49
   483
     * <pre>
jaroslav@49
   484
     *     synchronized (obj) {
jaroslav@49
   485
     *         while (&lt;condition does not hold&gt;)
jaroslav@49
   486
     *             obj.wait();
jaroslav@49
   487
     *         ... // Perform action appropriate to condition
jaroslav@49
   488
     *     }
jaroslav@49
   489
     * </pre>
jaroslav@49
   490
     * This method should only be called by a thread that is the owner
jaroslav@49
   491
     * of this object's monitor. See the {@code notify} method for a
jaroslav@49
   492
     * description of the ways in which a thread can become the owner of
jaroslav@49
   493
     * a monitor.
jaroslav@49
   494
     *
jaroslav@49
   495
     * @exception  IllegalMonitorStateException  if the current thread is not
jaroslav@49
   496
     *               the owner of the object's monitor.
jaroslav@49
   497
     * @exception  InterruptedException if any thread interrupted the
jaroslav@49
   498
     *             current thread before or while the current thread
jaroslav@49
   499
     *             was waiting for a notification.  The <i>interrupted
jaroslav@49
   500
     *             status</i> of the current thread is cleared when
jaroslav@49
   501
     *             this exception is thrown.
jaroslav@49
   502
     * @see        java.lang.Object#notify()
jaroslav@49
   503
     * @see        java.lang.Object#notifyAll()
jaroslav@49
   504
     */
jaroslav@49
   505
    public final void wait() throws InterruptedException {
jaroslav@49
   506
        wait(0);
jaroslav@49
   507
    }
jaroslav@49
   508
jaroslav@49
   509
    /**
jaroslav@49
   510
     * Called by the garbage collector on an object when garbage collection
jaroslav@49
   511
     * determines that there are no more references to the object.
jaroslav@49
   512
     * A subclass overrides the {@code finalize} method to dispose of
jaroslav@49
   513
     * system resources or to perform other cleanup.
jaroslav@49
   514
     * <p>
jaroslav@49
   515
     * The general contract of {@code finalize} is that it is invoked
jaroslav@49
   516
     * if and when the Java<font size="-2"><sup>TM</sup></font> virtual
jaroslav@49
   517
     * machine has determined that there is no longer any
jaroslav@49
   518
     * means by which this object can be accessed by any thread that has
jaroslav@49
   519
     * not yet died, except as a result of an action taken by the
jaroslav@49
   520
     * finalization of some other object or class which is ready to be
jaroslav@49
   521
     * finalized. The {@code finalize} method may take any action, including
jaroslav@49
   522
     * making this object available again to other threads; the usual purpose
jaroslav@49
   523
     * of {@code finalize}, however, is to perform cleanup actions before
jaroslav@49
   524
     * the object is irrevocably discarded. For example, the finalize method
jaroslav@49
   525
     * for an object that represents an input/output connection might perform
jaroslav@49
   526
     * explicit I/O transactions to break the connection before the object is
jaroslav@49
   527
     * permanently discarded.
jaroslav@49
   528
     * <p>
jaroslav@49
   529
     * The {@code finalize} method of class {@code Object} performs no
jaroslav@49
   530
     * special action; it simply returns normally. Subclasses of
jaroslav@49
   531
     * {@code Object} may override this definition.
jaroslav@49
   532
     * <p>
jaroslav@49
   533
     * The Java programming language does not guarantee which thread will
jaroslav@49
   534
     * invoke the {@code finalize} method for any given object. It is
jaroslav@49
   535
     * guaranteed, however, that the thread that invokes finalize will not
jaroslav@49
   536
     * be holding any user-visible synchronization locks when finalize is
jaroslav@49
   537
     * invoked. If an uncaught exception is thrown by the finalize method,
jaroslav@49
   538
     * the exception is ignored and finalization of that object terminates.
jaroslav@49
   539
     * <p>
jaroslav@49
   540
     * After the {@code finalize} method has been invoked for an object, no
jaroslav@49
   541
     * further action is taken until the Java virtual machine has again
jaroslav@49
   542
     * determined that there is no longer any means by which this object can
jaroslav@49
   543
     * be accessed by any thread that has not yet died, including possible
jaroslav@49
   544
     * actions by other objects or classes which are ready to be finalized,
jaroslav@49
   545
     * at which point the object may be discarded.
jaroslav@49
   546
     * <p>
jaroslav@49
   547
     * The {@code finalize} method is never invoked more than once by a Java
jaroslav@49
   548
     * virtual machine for any given object.
jaroslav@49
   549
     * <p>
jaroslav@49
   550
     * Any exception thrown by the {@code finalize} method causes
jaroslav@49
   551
     * the finalization of this object to be halted, but is otherwise
jaroslav@49
   552
     * ignored.
jaroslav@49
   553
     *
jaroslav@49
   554
     * @throws Throwable the {@code Exception} raised by this method
jaroslav@49
   555
     */
jaroslav@49
   556
    protected void finalize() throws Throwable { }
jaroslav@49
   557
}