1 /*
2  * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 // -- This file was mechanically generated: Do not edit! -- //
27 
28 package java.nio;
29 
30 
31 /**
32  * A double buffer.
33  *
34  * <p> This class defines four categories of operations upon
35  * double buffers:
36  *
37  * <ul>
38  *
39  * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
40  * {@link #put(double) </code><i>put</i><code>} methods that read and write
41  * single doubles; </p></li>
42  *
43  * <li><p> Relative {@link #get(double[]) </code><i>bulk get</i><code>}
44  * methods that transfer contiguous sequences of doubles from this buffer
45  * into an array; and</p></li>
46  *
47  * <li><p> Relative {@link #put(double[]) </code><i>bulk put</i><code>}
48  * methods that transfer contiguous sequences of doubles from a
49  * double array or some other double
50  * buffer into this buffer;&#32;and </p></li>
51  *
52  * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
53  * #duplicate </code>duplicating<code>}, and {@link #slice
54  * </code>slicing<code>} a double buffer.  </p></li>
55  * </ul>
56  *
57  * <p> Double buffers can be created either by {@link #allocate
58  * </code><i>allocation</i><code>}, which allocates space for the buffer's
59  * content, by {@link #wrap(double[]) </code><i>wrapping</i><code>} an existing
60  * double array  into a buffer, or by creating a
61  * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer.
62  *
63  * <p> Like a byte buffer, a double buffer is either <a
64  * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>.  A
65  * double buffer created via the <tt>wrap</tt> methods of this class will
66  * be non-direct.  A double buffer created as a view of a byte buffer will
67  * be direct if, and only if, the byte buffer itself is direct.  Whether or not
68  * a double buffer is direct may be determined by invoking the {@link
69  * #isDirect isDirect} method.  </p>
70  *
71  * <p> Methods in this class that do not otherwise have a value to return are
72  * specified to return the buffer upon which they are invoked.  This allows
73  * method invocations to be chained.
74  *
75  * @author Mark Reinhold
76  * @author JSR-51 Expert Group
77  * @since 1.4
78  */
79 
80 public abstract class DoubleBuffer
81         extends Buffer
82         implements Comparable<DoubleBuffer> {
83 
84     // These fields are declared here rather than in Heap-X-Buffer in order to
85     // reduce the number of virtual method invocations needed to access these
86     // values, which is especially costly when coding small buffers.
87     //
88     final double[] hb;                  // Non-null only for heap buffers
89     final int offset;
90     boolean isReadOnly;                 // Valid only for heap buffers
91 
92     // Creates a new buffer with the given mark, position, limit, capacity,
93     // backing array, and array offset
94     //
DoubleBuffer(int mark, int pos, int lim, int cap, double[] hb, int offset)95     DoubleBuffer(int mark, int pos, int lim, int cap,   // package-private
96                  double[] hb, int offset) {
97         super(mark, pos, lim, cap, 3);
98         this.hb = hb;
99         this.offset = offset;
100     }
101 
102     // Creates a new buffer with the given mark, position, limit, and capacity
103     //
DoubleBuffer(int mark, int pos, int lim, int cap)104     DoubleBuffer(int mark, int pos, int lim, int cap) { // package-private
105         this(mark, pos, lim, cap, null, 0);
106     }
107 
108 
109     /**
110      * Allocates a new double buffer.
111      *
112      * <p> The new buffer's position will be zero, its limit will be its
113      * capacity, its mark will be undefined, and each of its elements will be
114      * initialized to zero.  It will have a {@link #array
115      * </code>backing array<code>}, and its {@link #arrayOffset </code>array
116      * offset<code>} will be zero.
117      *
118      * @param capacity The new buffer's capacity, in doubles
119      * @return The new double buffer
120      * @throws IllegalArgumentException If the <tt>capacity</tt> is a negative integer
121      */
allocate(int capacity)122     public static DoubleBuffer allocate(int capacity) {
123         if (capacity < 0)
124             throw new IllegalArgumentException();
125         return new HeapDoubleBuffer(capacity, capacity);
126     }
127 
128     /**
129      * Wraps a double array into a buffer.
130      *
131      * <p> The new buffer will be backed by the given double array;
132      * that is, modifications to the buffer will cause the array to be modified
133      * and vice versa.  The new buffer's capacity will be
134      * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
135      * will be <tt>offset + length</tt>, and its mark will be undefined.  Its
136      * {@link #array </code>backing array<code>} will be the given array, and
137      * its {@link #arrayOffset </code>array offset<code>} will be zero.  </p>
138      *
139      * @param array  The array that will back the new buffer
140      * @param offset The offset of the subarray to be used; must be non-negative and
141      *               no larger than <tt>array.length</tt>.  The new buffer's position
142      *               will be set to this value.
143      * @param length The length of the subarray to be used;
144      *               must be non-negative and no larger than
145      *               <tt>array.length - offset</tt>.
146      *               The new buffer's limit will be set to <tt>offset + length</tt>.
147      * @return The new double buffer
148      * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and
149      *                                   <tt>length</tt>
150      *                                   parameters do not hold
151      */
wrap(double[] array, int offset, int length)152     public static DoubleBuffer wrap(double[] array,
153                                     int offset, int length) {
154         try {
155             return new HeapDoubleBuffer(array, offset, length);
156         } catch (IllegalArgumentException x) {
157             throw new IndexOutOfBoundsException();
158         }
159     }
160 
161     /**
162      * Wraps a double array into a buffer.
163      *
164      * <p> The new buffer will be backed by the given double array;
165      * that is, modifications to the buffer will cause the array to be modified
166      * and vice versa.  The new buffer's capacity and limit will be
167      * <tt>array.length</tt>, its position will be zero, and its mark will be
168      * undefined.  Its {@link #array </code>backing array<code>} will be the
169      * given array, and its {@link #arrayOffset </code>array offset<code>} will
170      * be zero.  </p>
171      *
172      * @param array The array that will back this buffer
173      * @return The new double buffer
174      */
wrap(double[] array)175     public static DoubleBuffer wrap(double[] array) {
176         return wrap(array, 0, array.length);
177     }
178 
179 
180     /**
181      * Creates a new double buffer whose content is a shared subsequence of
182      * this buffer's content.
183      *
184      * <p> The content of the new buffer will start at this buffer's current
185      * position.  Changes to this buffer's content will be visible in the new
186      * buffer, and vice versa; the two buffers' position, limit, and mark
187      * values will be independent.
188      *
189      * <p> The new buffer's position will be zero, its capacity and its limit
190      * will be the number of doubles remaining in this buffer, and its mark
191      * will be undefined.  The new buffer will be direct if, and only if, this
192      * buffer is direct, and it will be read-only if, and only if, this buffer
193      * is read-only.  </p>
194      *
195      * @return The new double buffer
196      */
slice()197     public abstract DoubleBuffer slice();
198 
199     /**
200      * Creates a new double buffer that shares this buffer's content.
201      *
202      * <p> The content of the new buffer will be that of this buffer.  Changes
203      * to this buffer's content will be visible in the new buffer, and vice
204      * versa; the two buffers' position, limit, and mark values will be
205      * independent.
206      *
207      * <p> The new buffer's capacity, limit, position, and mark values will be
208      * identical to those of this buffer.  The new buffer will be direct if,
209      * and only if, this buffer is direct, and it will be read-only if, and
210      * only if, this buffer is read-only.  </p>
211      *
212      * @return The new double buffer
213      */
duplicate()214     public abstract DoubleBuffer duplicate();
215 
216     /**
217      * Creates a new, read-only double buffer that shares this buffer's
218      * content.
219      *
220      * <p> The content of the new buffer will be that of this buffer.  Changes
221      * to this buffer's content will be visible in the new buffer; the new
222      * buffer itself, however, will be read-only and will not allow the shared
223      * content to be modified.  The two buffers' position, limit, and mark
224      * values will be independent.
225      *
226      * <p> The new buffer's capacity, limit, position, and mark values will be
227      * identical to those of this buffer.
228      *
229      * <p> If this buffer is itself read-only then this method behaves in
230      * exactly the same way as the {@link #duplicate duplicate} method.  </p>
231      *
232      * @return The new, read-only double buffer
233      */
asReadOnlyBuffer()234     public abstract DoubleBuffer asReadOnlyBuffer();
235 
236 
237     // -- Singleton get/put methods --
238 
239     /**
240      * Relative <i>get</i> method.  Reads the double at this buffer's
241      * current position, and then increments the position. </p>
242      *
243      * @return The double at the buffer's current position
244      * @throws BufferUnderflowException If the buffer's current position is not smaller than its
245      *                                  limit
246      */
get()247     public abstract double get();
248 
249     /**
250      * Relative <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
251      *
252      * <p> Writes the given double into this buffer at the current
253      * position, and then increments the position. </p>
254      *
255      * @param d The double to be written
256      * @return This buffer
257      * @throws BufferOverflowException If this buffer's current position is not smaller than its
258      *                                 limit
259      * @throws ReadOnlyBufferException If this buffer is read-only
260      */
put(double d)261     public abstract DoubleBuffer put(double d);
262 
263     /**
264      * Absolute <i>get</i> method.  Reads the double at the given
265      * index. </p>
266      *
267      * @param index The index from which the double will be read
268      * @return The double at the given index
269      * @throws IndexOutOfBoundsException If <tt>index</tt> is negative
270      *                                   or not smaller than the buffer's limit
271      */
get(int index)272     public abstract double get(int index);
273 
274     /**
275      * Absolute <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
276      *
277      * <p> Writes the given double into this buffer at the given
278      * index. </p>
279      *
280      * @param index The index at which the double will be written
281      * @param d     The double value to be written
282      * @return This buffer
283      * @throws IndexOutOfBoundsException If <tt>index</tt> is negative
284      *                                   or not smaller than the buffer's limit
285      * @throws ReadOnlyBufferException   If this buffer is read-only
286      */
put(int index, double d)287     public abstract DoubleBuffer put(int index, double d);
288 
289 
290     // -- Bulk get operations --
291 
292     /**
293      * Relative bulk <i>get</i> method.
294      *
295      * <p> This method transfers doubles from this buffer into the given
296      * destination array.  If there are fewer doubles remaining in the
297      * buffer than are required to satisfy the request, that is, if
298      * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
299      * doubles are transferred and a {@link BufferUnderflowException} is
300      * thrown.
301      *
302      * <p> Otherwise, this method copies <tt>length</tt> doubles from this
303      * buffer into the given array, starting at the current position of this
304      * buffer and at the given offset in the array.  The position of this
305      * buffer is then incremented by <tt>length</tt>.
306      *
307      * <p> In other words, an invocation of this method of the form
308      * <tt>src.get(dst,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
309      * the loop
310      *
311      * <pre>
312      *     for (int i = off; i < off + len; i++)
313      *         dst[i] = src.get(); </pre>
314      *
315      * except that it first checks that there are sufficient doubles in
316      * this buffer and it is potentially much more efficient. </p>
317      *
318      * @param dst    The array into which doubles are to be written
319      * @param offset The offset within the array of the first double to be
320      *               written; must be non-negative and no larger than
321      *               <tt>dst.length</tt>
322      * @param length The maximum number of doubles to be written to the given
323      *               array; must be non-negative and no larger than
324      *               <tt>dst.length - offset</tt>
325      * @return This buffer
326      * @throws BufferUnderflowException  If there are fewer than <tt>length</tt> doubles
327      *                                   remaining in this buffer
328      * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and
329      *                                   <tt>length</tt>
330      *                                   parameters do not hold
331      */
get(double[] dst, int offset, int length)332     public DoubleBuffer get(double[] dst, int offset, int length) {
333         checkBounds(offset, length, dst.length);
334         if (length > remaining())
335             throw new BufferUnderflowException();
336         int end = offset + length;
337         for (int i = offset; i < end; i++)
338             dst[i] = get();
339         return this;
340     }
341 
342     /**
343      * Relative bulk <i>get</i> method.
344      *
345      * <p> This method transfers doubles from this buffer into the given
346      * destination array.  An invocation of this method of the form
347      * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
348      *
349      * <pre>
350      *     src.get(a, 0, a.length) </pre>
351      *
352      * @return This buffer
353      * @throws BufferUnderflowException If there are fewer than <tt>length</tt> doubles
354      *                                  remaining in this buffer
355      */
get(double[] dst)356     public DoubleBuffer get(double[] dst) {
357         return get(dst, 0, dst.length);
358     }
359 
360 
361     // -- Bulk put operations --
362 
363     /**
364      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
365      *
366      * <p> This method transfers the doubles remaining in the given source
367      * buffer into this buffer.  If there are more doubles remaining in the
368      * source buffer than in this buffer, that is, if
369      * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
370      * then no doubles are transferred and a {@link
371      * BufferOverflowException} is thrown.
372      *
373      * <p> Otherwise, this method copies
374      * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> doubles from the given
375      * buffer into this buffer, starting at each buffer's current position.
376      * The positions of both buffers are then incremented by <i>n</i>.
377      *
378      * <p> In other words, an invocation of this method of the form
379      * <tt>dst.put(src)</tt> has exactly the same effect as the loop
380      *
381      * <pre>
382      *     while (src.hasRemaining())
383      *         dst.put(src.get()); </pre>
384      *
385      * except that it first checks that there is sufficient space in this
386      * buffer and it is potentially much more efficient. </p>
387      *
388      * @param src The source buffer from which doubles are to be read;
389      *            must not be this buffer
390      * @return This buffer
391      * @throws BufferOverflowException  If there is insufficient space in this buffer
392      *                                  for the remaining doubles in the source buffer
393      * @throws IllegalArgumentException If the source buffer is this buffer
394      * @throws ReadOnlyBufferException  If this buffer is read-only
395      */
put(DoubleBuffer src)396     public DoubleBuffer put(DoubleBuffer src) {
397         if (src == this)
398             throw new IllegalArgumentException();
399         int n = src.remaining();
400         if (n > remaining())
401             throw new BufferOverflowException();
402         for (int i = 0; i < n; i++)
403             put(src.get());
404         return this;
405     }
406 
407     /**
408      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
409      *
410      * <p> This method transfers doubles into this buffer from the given
411      * source array.  If there are more doubles to be copied from the array
412      * than remain in this buffer, that is, if
413      * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
414      * doubles are transferred and a {@link BufferOverflowException} is
415      * thrown.
416      *
417      * <p> Otherwise, this method copies <tt>length</tt> doubles from the
418      * given array into this buffer, starting at the given offset in the array
419      * and at the current position of this buffer.  The position of this buffer
420      * is then incremented by <tt>length</tt>.
421      *
422      * <p> In other words, an invocation of this method of the form
423      * <tt>dst.put(src,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
424      * the loop
425      *
426      * <pre>
427      *     for (int i = off; i < off + len; i++)
428      *         dst.put(a[i]); </pre>
429      *
430      * except that it first checks that there is sufficient space in this
431      * buffer and it is potentially much more efficient. </p>
432      *
433      * @param src    The array from which doubles are to be read
434      * @param offset The offset within the array of the first double to be read;
435      *               must be non-negative and no larger than <tt>array.length</tt>
436      * @param length The number of doubles to be read from the given array;
437      *               must be non-negative and no larger than
438      *               <tt>array.length - offset</tt>
439      * @return This buffer
440      * @throws BufferOverflowException   If there is insufficient space in this buffer
441      * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and
442      *                                   <tt>length</tt>
443      *                                   parameters do not hold
444      * @throws ReadOnlyBufferException   If this buffer is read-only
445      */
put(double[] src, int offset, int length)446     public DoubleBuffer put(double[] src, int offset, int length) {
447         checkBounds(offset, length, src.length);
448         if (length > remaining())
449             throw new BufferOverflowException();
450         int end = offset + length;
451         for (int i = offset; i < end; i++)
452             this.put(src[i]);
453         return this;
454     }
455 
456     /**
457      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
458      *
459      * <p> This method transfers the entire content of the given source
460      * double array into this buffer.  An invocation of this method of the
461      * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
462      * invocation
463      *
464      * <pre>
465      *     dst.put(a, 0, a.length) </pre>
466      *
467      * @return This buffer
468      * @throws BufferOverflowException If there is insufficient space in this buffer
469      * @throws ReadOnlyBufferException If this buffer is read-only
470      */
put(double[] src)471     public final DoubleBuffer put(double[] src) {
472         return put(src, 0, src.length);
473     }
474 
475 
476     // -- Other stuff --
477 
478     /**
479      * Tells whether or not this buffer is backed by an accessible double
480      * array.
481      *
482      * <p> If this method returns <tt>true</tt> then the {@link #array() array}
483      * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
484      * </p>
485      *
486      * @return <tt>true</tt> if, and only if, this buffer
487      * is backed by an array and is not read-only
488      */
hasArray()489     public final boolean hasArray() {
490         return (hb != null) && !isReadOnly;
491     }
492 
493     /**
494      * Returns the double array that backs this
495      * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
496      *
497      * <p> Modifications to this buffer's content will cause the returned
498      * array's content to be modified, and vice versa.
499      *
500      * <p> Invoke the {@link #hasArray hasArray} method before invoking this
501      * method in order to ensure that this buffer has an accessible backing
502      * array.  </p>
503      *
504      * @return The array that backs this buffer
505      * @throws ReadOnlyBufferException       If this buffer is backed by an array but is read-only
506      * @throws UnsupportedOperationException If this buffer is not backed by an accessible array
507      */
array()508     public final double[] array() {
509         if (hb == null)
510             throw new UnsupportedOperationException();
511         if (isReadOnly)
512             throw new ReadOnlyBufferException();
513         return hb;
514     }
515 
516     /**
517      * Returns the offset within this buffer's backing array of the first
518      * element of the buffer&nbsp;&nbsp;<i>(optional operation)</i>.
519      *
520      * <p> If this buffer is backed by an array then buffer position <i>p</i>
521      * corresponds to array index <i>p</i>&nbsp;+&nbsp;<tt>arrayOffset()</tt>.
522      *
523      * <p> Invoke the {@link #hasArray hasArray} method before invoking this
524      * method in order to ensure that this buffer has an accessible backing
525      * array.  </p>
526      *
527      * @return The offset within this buffer's array
528      * of the first element of the buffer
529      * @throws ReadOnlyBufferException       If this buffer is backed by an array but is read-only
530      * @throws UnsupportedOperationException If this buffer is not backed by an accessible array
531      */
arrayOffset()532     public final int arrayOffset() {
533         if (hb == null)
534             throw new UnsupportedOperationException();
535         if (isReadOnly)
536             throw new ReadOnlyBufferException();
537         return offset;
538     }
539 
540     /**
541      * Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
542      *
543      * <p> The doubles between the buffer's current position and its limit,
544      * if any, are copied to the beginning of the buffer.  That is, the
545      * double at index <i>p</i>&nbsp;=&nbsp;<tt>position()</tt> is copied
546      * to index zero, the double at index <i>p</i>&nbsp;+&nbsp;1 is copied
547      * to index one, and so forth until the double at index
548      * <tt>limit()</tt>&nbsp;-&nbsp;1 is copied to index
549      * <i>n</i>&nbsp;=&nbsp;<tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>&nbsp;-&nbsp;<i>p</i>.
550      * The buffer's position is then set to <i>n+1</i> and its limit is set to
551      * its capacity.  The mark, if defined, is discarded.
552      *
553      * <p> The buffer's position is set to the number of doubles copied,
554      * rather than to zero, so that an invocation of this method can be
555      * followed immediately by an invocation of another relative <i>put</i>
556      * method. </p>
557      *
558      * @return This buffer
559      * @throws ReadOnlyBufferException If this buffer is read-only
560      */
compact()561     public abstract DoubleBuffer compact();
562 
563     /**
564      * Tells whether or not this double buffer is direct. </p>
565      *
566      * @return <tt>true</tt> if, and only if, this buffer is direct
567      */
isDirect()568     public abstract boolean isDirect();
569 
570 
571     /**
572      * Returns a string summarizing the state of this buffer.  </p>
573      *
574      * @return A summary string
575      */
toString()576     public String toString() {
577         StringBuffer sb = new StringBuffer();
578         sb.append(getClass().getName());
579         sb.append("[pos=");
580         sb.append(position());
581         sb.append(" lim=");
582         sb.append(limit());
583         sb.append(" cap=");
584         sb.append(capacity());
585         sb.append("]");
586         return sb.toString();
587     }
588 
589 
590     /**
591      * Returns the current hash code of this buffer.
592      *
593      * <p> The hash code of a double buffer depends only upon its remaining
594      * elements; that is, upon the elements from <tt>position()</tt> up to, and
595      * including, the element at <tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>.
596      *
597      * <p> Because buffer hash codes are content-dependent, it is inadvisable
598      * to use buffers as keys in hash maps or similar data structures unless it
599      * is known that their contents will not change.  </p>
600      *
601      * @return The current hash code of this buffer
602      */
hashCode()603     public int hashCode() {
604         int h = 1;
605         int p = position();
606         for (int i = limit() - 1; i >= p; i--)
607             h = 31 * h + (int) get(i);
608         return h;
609     }
610 
611     /**
612      * Tells whether or not this buffer is equal to another object.
613      *
614      * <p> Two double buffers are equal if, and only if,
615      *
616      * <p><ol>
617      *
618      * <li><p> They have the same element type,  </p></li>
619      *
620      * <li><p> They have the same number of remaining elements, and
621      * </p></li>
622      *
623      * <li><p> The two sequences of remaining elements, considered
624      * independently of their starting positions, are pointwise equal.
625      *
626      * This method considers two double elements {@code a} and {@code b}
627      * to be equal if
628      * {@code (a == b) || (Double.isNaN(a) && Double.isNaN(b))}.
629      * The values {@code -0.0} and {@code +0.0} are considered to be
630      * equal, unlike {@link Double#equals(Object)}.
631      *
632      * </p></li>
633      *
634      * </ol>
635      *
636      * <p> A double buffer is not equal to any other type of object.  </p>
637      *
638      * @param ob The object to which this buffer is to be compared
639      * @return <tt>true</tt> if, and only if, this buffer is equal to the
640      * given object
641      */
equals(Object ob)642     public boolean equals(Object ob) {
643         if (this == ob)
644             return true;
645         if (!(ob instanceof DoubleBuffer))
646             return false;
647         DoubleBuffer that = (DoubleBuffer) ob;
648         if (this.remaining() != that.remaining())
649             return false;
650         int p = this.position();
651         for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--)
652             if (!equals(this.get(i), that.get(j)))
653                 return false;
654         return true;
655     }
656 
equals(double x, double y)657     private static boolean equals(double x, double y) {
658 
659         return (x == y) || (Double.isNaN(x) && Double.isNaN(y));
660 
661 
662     }
663 
664     /**
665      * Compares this buffer to another.
666      *
667      * <p> Two double buffers are compared by comparing their sequences of
668      * remaining elements lexicographically, without regard to the starting
669      * position of each sequence within its corresponding buffer.
670      *
671      * Pairs of {@code double} elements are compared as if by invoking
672      * {@link Double#compare(double, double)}, except that
673      * {@code -0.0} and {@code 0.0} are considered to be equal.
674      * {@code Double.NaN} is considered by this method to be equal
675      * to itself and greater than all other {@code double} values
676      * (including {@code Double.POSITIVE_INFINITY}).
677      *
678      *
679      *
680      *
681      *
682      * <p> A double buffer is not comparable to any other type of object.
683      *
684      * @return A negative integer, zero, or a positive integer as this buffer
685      * is less than, equal to, or greater than the given buffer
686      */
compareTo(DoubleBuffer that)687     public int compareTo(DoubleBuffer that) {
688         int n = this.position() + Math.min(this.remaining(), that.remaining());
689         for (int i = this.position(), j = that.position(); i < n; i++, j++) {
690             // Android-changed: Call through to Double.compare() instead of
691             // duplicating code pointlessly.
692             int cmp = Double.compare(this.get(i), that.get(j));
693             if (cmp != 0)
694                 return cmp;
695         }
696         return this.remaining() - that.remaining();
697     }
698 
compare(double x, double y)699     private static int compare(double x, double y) {
700 
701         return ((x < y) ? -1 :
702                 (x > y) ? +1 :
703                         (x == y) ? 0 :
704                                 Double.isNaN(x) ? (Double.isNaN(y) ? 0 : +1) : -1);
705 
706 
707     }
708 
709     // -- Other char stuff --
710 
711 
712     // -- Other byte stuff: Access to binary data --
713 
714 
715     /**
716      * Retrieves this buffer's byte order.
717      *
718      * <p> The byte order of a double buffer created by allocation or by
719      * wrapping an existing <tt>double</tt> array is the {@link
720      * ByteOrder#nativeOrder </code>native order<code>} of the underlying
721      * hardware.  The byte order of a double buffer created as a <a
722      * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the
723      * byte buffer at the moment that the view is created.  </p>
724      *
725      * @return This buffer's byte order
726      */
order()727     public abstract ByteOrder order();
728 
729 
730 }
731