1 /* 2 * Copyright (c) 2000, 2013, 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 import dalvik.annotation.codegen.CovariantReturnType; 32 33 /** 34 * A float buffer. 35 * 36 * <p> This class defines four categories of operations upon 37 * float buffers: 38 * 39 * <ul> 40 * 41 * <li><p> Absolute and relative {@link #get() <i>get</i>} and 42 * {@link #put(float) <i>put</i>} methods that read and write 43 * single floats; </p></li> 44 * 45 * <li><p> Relative {@link #get(float[]) <i>bulk get</i>} 46 * methods that transfer contiguous sequences of floats from this buffer 47 * into an array; and</p></li> 48 * 49 * <li><p> Relative {@link #put(float[]) <i>bulk put</i>} 50 * methods that transfer contiguous sequences of floats from a 51 * float array or some other float 52 * buffer into this buffer; and </p></li> 53 * 54 * 55 * <li><p> Methods for {@link #compact compacting}, {@link 56 * #duplicate duplicating}, and {@link #slice slicing} 57 * a float buffer. </p></li> 58 * 59 * </ul> 60 * 61 * <p> Float buffers can be created either by {@link #allocate 62 * <i>allocation</i>}, which allocates space for the buffer's 63 * 64 * 65 * content, by {@link #wrap(float[]) <i>wrapping</i>} an existing 66 * float array into a buffer, or by creating a 67 * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer. 68 * 69 * 70 * 71 * 72 * <p> Like a byte buffer, a float buffer is either <a 73 * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A 74 * float buffer created via the <tt>wrap</tt> methods of this class will 75 * be non-direct. A float buffer created as a view of a byte buffer will 76 * be direct if, and only if, the byte buffer itself is direct. Whether or not 77 * a float buffer is direct may be determined by invoking the {@link 78 * #isDirect isDirect} method. </p> 79 * 80 * 81 * 82 * 83 * <p> Methods in this class that do not otherwise have a value to return are 84 * specified to return the buffer upon which they are invoked. This allows 85 * method invocations to be chained. 86 * 87 * 88 * 89 * @author Mark Reinhold 90 * @author JSR-51 Expert Group 91 * @since 1.4 92 */ 93 94 public abstract class FloatBuffer 95 extends Buffer 96 implements Comparable<FloatBuffer> 97 { 98 99 // These fields are declared here rather than in Heap-X-Buffer in order to 100 // reduce the number of virtual method invocations needed to access these 101 // values, which is especially costly when coding small buffers. 102 // 103 final float[] hb; // Non-null only for heap buffers 104 final int offset; 105 boolean isReadOnly; // Valid only for heap buffers 106 107 // Creates a new buffer with the given mark, position, limit, capacity, 108 // backing array, and array offset 109 // FloatBuffer(int mark, int pos, int lim, int cap, float[] hb, int offset)110 FloatBuffer(int mark, int pos, int lim, int cap, // package-private 111 float[] hb, int offset) 112 { 113 // Android-added: elementSizeShift parameter (log2 of element size). 114 super(mark, pos, lim, cap, 2 /* elementSizeShift */); 115 this.hb = hb; 116 this.offset = offset; 117 } 118 119 // Creates a new buffer with the given mark, position, limit, and capacity 120 // FloatBuffer(int mark, int pos, int lim, int cap)121 FloatBuffer(int mark, int pos, int lim, int cap) { // package-private 122 this(mark, pos, lim, cap, null, 0); 123 } 124 125 126 /** 127 * Allocates a new float buffer. 128 * 129 * <p> The new buffer's position will be zero, its limit will be its 130 * capacity, its mark will be undefined, and each of its elements will be 131 * initialized to zero. It will have a {@link #array backing array}, 132 * and its {@link #arrayOffset array offset} will be zero. 133 * 134 * @param capacity 135 * The new buffer's capacity, in floats 136 * 137 * @return The new float buffer 138 * 139 * @throws IllegalArgumentException 140 * If the <tt>capacity</tt> is a negative integer 141 */ allocate(int capacity)142 public static FloatBuffer allocate(int capacity) { 143 if (capacity < 0) 144 throw new IllegalArgumentException(); 145 return new HeapFloatBuffer(capacity, capacity); 146 } 147 148 /** 149 * Wraps a float array into a buffer. 150 * 151 * <p> The new buffer will be backed by the given float array; 152 * that is, modifications to the buffer will cause the array to be modified 153 * and vice versa. The new buffer's capacity will be 154 * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit 155 * will be <tt>offset + length</tt>, and its mark will be undefined. Its 156 * {@link #array backing array} will be the given array, and 157 * its {@link #arrayOffset array offset} will be zero. </p> 158 * 159 * @param array 160 * The array that will back the new buffer 161 * 162 * @param offset 163 * The offset of the subarray to be used; must be non-negative and 164 * no larger than <tt>array.length</tt>. The new buffer's position 165 * will be set to this value. 166 * 167 * @param length 168 * The length of the subarray to be used; 169 * must be non-negative and no larger than 170 * <tt>array.length - offset</tt>. 171 * The new buffer's limit will be set to <tt>offset + length</tt>. 172 * 173 * @return The new float buffer 174 * 175 * @throws IndexOutOfBoundsException 176 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 177 * parameters do not hold 178 */ wrap(float[] array, int offset, int length)179 public static FloatBuffer wrap(float[] array, 180 int offset, int length) 181 { 182 try { 183 return new HeapFloatBuffer(array, offset, length); 184 } catch (IllegalArgumentException x) { 185 throw new IndexOutOfBoundsException(); 186 } 187 } 188 189 /** 190 * Wraps a float array into a buffer. 191 * 192 * <p> The new buffer will be backed by the given float array; 193 * that is, modifications to the buffer will cause the array to be modified 194 * and vice versa. The new buffer's capacity and limit will be 195 * <tt>array.length</tt>, its position will be zero, and its mark will be 196 * undefined. Its {@link #array backing array} will be the 197 * given array, and its {@link #arrayOffset array offset>} will 198 * be zero. </p> 199 * 200 * @param array 201 * The array that will back this buffer 202 * 203 * @return The new float buffer 204 */ wrap(float[] array)205 public static FloatBuffer wrap(float[] array) { 206 return wrap(array, 0, array.length); 207 } 208 209 210 /** 211 * Creates a new float buffer whose content is a shared subsequence of 212 * this buffer's content. 213 * 214 * <p> The content of the new buffer will start at this buffer's current 215 * position. Changes to this buffer's content will be visible in the new 216 * buffer, and vice versa; the two buffers' position, limit, and mark 217 * values will be independent. 218 * 219 * <p> The new buffer's position will be zero, its capacity and its limit 220 * will be the number of floats remaining in this buffer, and its mark 221 * will be undefined. The new buffer will be direct if, and only if, this 222 * buffer is direct, and it will be read-only if, and only if, this buffer 223 * is read-only. </p> 224 * 225 * @return The new float buffer 226 */ slice()227 public abstract FloatBuffer slice(); 228 229 /** 230 * Creates a new float buffer that shares this buffer's content. 231 * 232 * <p> The content of the new buffer will be that of this buffer. Changes 233 * to this buffer's content will be visible in the new buffer, and vice 234 * versa; the two buffers' position, limit, and mark values will be 235 * independent. 236 * 237 * <p> The new buffer's capacity, limit, position, and mark values will be 238 * identical to those of this buffer. The new buffer will be direct if, 239 * and only if, this buffer is direct, and it will be read-only if, and 240 * only if, this buffer is read-only. </p> 241 * 242 * @return The new float buffer 243 */ duplicate()244 public abstract FloatBuffer duplicate(); 245 246 /** 247 * Creates a new, read-only float buffer that shares this buffer's 248 * content. 249 * 250 * <p> The content of the new buffer will be that of this buffer. Changes 251 * to this buffer's content will be visible in the new buffer; the new 252 * buffer itself, however, will be read-only and will not allow the shared 253 * content to be modified. The two buffers' position, limit, and mark 254 * values will be independent. 255 * 256 * <p> The new buffer's capacity, limit, position, and mark values will be 257 * identical to those of this buffer. 258 * 259 * <p> If this buffer is itself read-only then this method behaves in 260 * exactly the same way as the {@link #duplicate duplicate} method. </p> 261 * 262 * @return The new, read-only float buffer 263 */ asReadOnlyBuffer()264 public abstract FloatBuffer asReadOnlyBuffer(); 265 266 267 // -- Singleton get/put methods -- 268 269 /** 270 * Relative <i>get</i> method. Reads the float at this buffer's 271 * current position, and then increments the position. 272 * 273 * @return The float at the buffer's current position 274 * 275 * @throws BufferUnderflowException 276 * If the buffer's current position is not smaller than its limit 277 */ get()278 public abstract float get(); 279 280 /** 281 * Relative <i>put</i> method <i>(optional operation)</i>. 282 * 283 * <p> Writes the given float into this buffer at the current 284 * position, and then increments the position. </p> 285 * 286 * @param f 287 * The float to be written 288 * 289 * @return This buffer 290 * 291 * @throws BufferOverflowException 292 * If this buffer's current position is not smaller than its limit 293 * 294 * @throws ReadOnlyBufferException 295 * If this buffer is read-only 296 */ put(float f)297 public abstract FloatBuffer put(float f); 298 299 /** 300 * Absolute <i>get</i> method. Reads the float at the given 301 * index. 302 * 303 * @param index 304 * The index from which the float will be read 305 * 306 * @return The float at the given index 307 * 308 * @throws IndexOutOfBoundsException 309 * If <tt>index</tt> is negative 310 * or not smaller than the buffer's limit 311 */ get(int index)312 public abstract float get(int index); 313 314 /** 315 * Absolute <i>put</i> method <i>(optional operation)</i>. 316 * 317 * <p> Writes the given float into this buffer at the given 318 * index. </p> 319 * 320 * @param index 321 * The index at which the float will be written 322 * 323 * @param f 324 * The float value to be written 325 * 326 * @return This buffer 327 * 328 * @throws IndexOutOfBoundsException 329 * If <tt>index</tt> is negative 330 * or not smaller than the buffer's limit 331 * 332 * @throws ReadOnlyBufferException 333 * If this buffer is read-only 334 */ put(int index, float f)335 public abstract FloatBuffer put(int index, float f); 336 337 338 // -- Bulk get operations -- 339 340 /** 341 * Relative bulk <i>get</i> method. 342 * 343 * <p> This method transfers floats from this buffer into the given 344 * destination array. If there are fewer floats remaining in the 345 * buffer than are required to satisfy the request, that is, if 346 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 347 * floats are transferred and a {@link BufferUnderflowException} is 348 * thrown. 349 * 350 * <p> Otherwise, this method copies <tt>length</tt> floats from this 351 * buffer into the given array, starting at the current position of this 352 * buffer and at the given offset in the array. The position of this 353 * buffer is then incremented by <tt>length</tt>. 354 * 355 * <p> In other words, an invocation of this method of the form 356 * <tt>src.get(dst, off, len)</tt> has exactly the same effect as 357 * the loop 358 * 359 * <pre>{@code 360 * for (int i = off; i < off + len; i++) 361 * dst[i] = src.get(); 362 * }</pre> 363 * 364 * except that it first checks that there are sufficient floats in 365 * this buffer and it is potentially much more efficient. 366 * 367 * @param dst 368 * The array into which floats are to be written 369 * 370 * @param offset 371 * The offset within the array of the first float to be 372 * written; must be non-negative and no larger than 373 * <tt>dst.length</tt> 374 * 375 * @param length 376 * The maximum number of floats to be written to the given 377 * array; must be non-negative and no larger than 378 * <tt>dst.length - offset</tt> 379 * 380 * @return This buffer 381 * 382 * @throws BufferUnderflowException 383 * If there are fewer than <tt>length</tt> floats 384 * remaining in this buffer 385 * 386 * @throws IndexOutOfBoundsException 387 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 388 * parameters do not hold 389 */ get(float[] dst, int offset, int length)390 public FloatBuffer get(float[] dst, int offset, int length) { 391 checkBounds(offset, length, dst.length); 392 if (length > remaining()) 393 throw new BufferUnderflowException(); 394 int end = offset + length; 395 for (int i = offset; i < end; i++) 396 dst[i] = get(); 397 return this; 398 } 399 400 /** 401 * Relative bulk <i>get</i> method. 402 * 403 * <p> This method transfers floats from this buffer into the given 404 * destination array. An invocation of this method of the form 405 * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation 406 * 407 * <pre> 408 * src.get(a, 0, a.length) </pre> 409 * 410 * @param dst 411 * The destination array 412 * 413 * @return This buffer 414 * 415 * @throws BufferUnderflowException 416 * If there are fewer than <tt>length</tt> floats 417 * remaining in this buffer 418 */ get(float[] dst)419 public FloatBuffer get(float[] dst) { 420 return get(dst, 0, dst.length); 421 } 422 423 424 // -- Bulk put operations -- 425 426 /** 427 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 428 * 429 * <p> This method transfers the floats remaining in the given source 430 * buffer into this buffer. If there are more floats remaining in the 431 * source buffer than in this buffer, that is, if 432 * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>, 433 * then no floats are transferred and a {@link 434 * BufferOverflowException} is thrown. 435 * 436 * <p> Otherwise, this method copies 437 * <i>n</i> = <tt>src.remaining()</tt> floats from the given 438 * buffer into this buffer, starting at each buffer's current position. 439 * The positions of both buffers are then incremented by <i>n</i>. 440 * 441 * <p> In other words, an invocation of this method of the form 442 * <tt>dst.put(src)</tt> has exactly the same effect as the loop 443 * 444 * <pre> 445 * while (src.hasRemaining()) 446 * dst.put(src.get()); </pre> 447 * 448 * except that it first checks that there is sufficient space in this 449 * buffer and it is potentially much more efficient. 450 * 451 * @param src 452 * The source buffer from which floats are to be read; 453 * must not be this buffer 454 * 455 * @return This buffer 456 * 457 * @throws BufferOverflowException 458 * If there is insufficient space in this buffer 459 * for the remaining floats in the source buffer 460 * 461 * @throws IllegalArgumentException 462 * If the source buffer is this buffer 463 * 464 * @throws ReadOnlyBufferException 465 * If this buffer is read-only 466 */ put(FloatBuffer src)467 public FloatBuffer put(FloatBuffer src) { 468 if (src == this) 469 throw new IllegalArgumentException(); 470 if (isReadOnly()) 471 throw new ReadOnlyBufferException(); 472 int n = src.remaining(); 473 if (n > remaining()) 474 throw new BufferOverflowException(); 475 for (int i = 0; i < n; i++) 476 put(src.get()); 477 return this; 478 } 479 480 /** 481 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 482 * 483 * <p> This method transfers floats into this buffer from the given 484 * source array. If there are more floats to be copied from the array 485 * than remain in this buffer, that is, if 486 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 487 * floats are transferred and a {@link BufferOverflowException} is 488 * thrown. 489 * 490 * <p> Otherwise, this method copies <tt>length</tt> floats from the 491 * given array into this buffer, starting at the given offset in the array 492 * and at the current position of this buffer. The position of this buffer 493 * is then incremented by <tt>length</tt>. 494 * 495 * <p> In other words, an invocation of this method of the form 496 * <tt>dst.put(src, off, len)</tt> has exactly the same effect as 497 * the loop 498 * 499 * <pre>{@code 500 * for (int i = off; i < off + len; i++) 501 * dst.put(a[i]); 502 * }</pre> 503 * 504 * except that it first checks that there is sufficient space in this 505 * buffer and it is potentially much more efficient. 506 * 507 * @param src 508 * The array from which floats are to be read 509 * 510 * @param offset 511 * The offset within the array of the first float to be read; 512 * must be non-negative and no larger than <tt>array.length</tt> 513 * 514 * @param length 515 * The number of floats to be read from the given array; 516 * must be non-negative and no larger than 517 * <tt>array.length - offset</tt> 518 * 519 * @return This buffer 520 * 521 * @throws BufferOverflowException 522 * If there is insufficient space in this buffer 523 * 524 * @throws IndexOutOfBoundsException 525 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 526 * parameters do not hold 527 * 528 * @throws ReadOnlyBufferException 529 * If this buffer is read-only 530 */ put(float[] src, int offset, int length)531 public FloatBuffer put(float[] src, int offset, int length) { 532 checkBounds(offset, length, src.length); 533 if (length > remaining()) 534 throw new BufferOverflowException(); 535 int end = offset + length; 536 for (int i = offset; i < end; i++) 537 this.put(src[i]); 538 return this; 539 } 540 541 /** 542 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 543 * 544 * <p> This method transfers the entire content of the given source 545 * float array into this buffer. An invocation of this method of the 546 * form <tt>dst.put(a)</tt> behaves in exactly the same way as the 547 * invocation 548 * 549 * <pre> 550 * dst.put(a, 0, a.length) </pre> 551 * 552 * @param src 553 * The source array 554 * 555 * @return This buffer 556 * 557 * @throws BufferOverflowException 558 * If there is insufficient space in this buffer 559 * 560 * @throws ReadOnlyBufferException 561 * If this buffer is read-only 562 */ put(float[] src)563 public final FloatBuffer put(float[] src) { 564 return put(src, 0, src.length); 565 } 566 567 568 // -- Other stuff -- 569 570 /** 571 * Tells whether or not this buffer is backed by an accessible float 572 * array. 573 * 574 * <p> If this method returns <tt>true</tt> then the {@link #array() array} 575 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked. 576 * </p> 577 * 578 * @return <tt>true</tt> if, and only if, this buffer 579 * is backed by an array and is not read-only 580 */ hasArray()581 public final boolean hasArray() { 582 return (hb != null) && !isReadOnly; 583 } 584 585 /** 586 * Returns the float array that backs this 587 * buffer <i>(optional operation)</i>. 588 * 589 * <p> Modifications to this buffer's content will cause the returned 590 * array's content to be modified, and vice versa. 591 * 592 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 593 * method in order to ensure that this buffer has an accessible backing 594 * array. </p> 595 * 596 * @return The array that backs this buffer 597 * 598 * @throws ReadOnlyBufferException 599 * If this buffer is backed by an array but is read-only 600 * 601 * @throws UnsupportedOperationException 602 * If this buffer is not backed by an accessible array 603 */ array()604 public final float[] array() { 605 if (hb == null) 606 throw new UnsupportedOperationException(); 607 if (isReadOnly) 608 throw new ReadOnlyBufferException(); 609 return hb; 610 } 611 612 /** 613 * Returns the offset within this buffer's backing array of the first 614 * element of the buffer <i>(optional operation)</i>. 615 * 616 * <p> If this buffer is backed by an array then buffer position <i>p</i> 617 * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>. 618 * 619 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 620 * method in order to ensure that this buffer has an accessible backing 621 * array. </p> 622 * 623 * @return The offset within this buffer's array 624 * of the first element of the buffer 625 * 626 * @throws ReadOnlyBufferException 627 * If this buffer is backed by an array but is read-only 628 * 629 * @throws UnsupportedOperationException 630 * If this buffer is not backed by an accessible array 631 */ arrayOffset()632 public final int arrayOffset() { 633 if (hb == null) 634 throw new UnsupportedOperationException(); 635 if (isReadOnly) 636 throw new ReadOnlyBufferException(); 637 return offset; 638 } 639 640 // BEGIN Android-added: covariant overloads of *Buffer methods that return this. 641 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 642 @Override position(int newPosition)643 public Buffer position(int newPosition) { 644 return super.position(newPosition); 645 } 646 647 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 648 @Override limit(int newLimit)649 public Buffer limit(int newLimit) { 650 return super.limit(newLimit); 651 } 652 653 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 654 @Override mark()655 public Buffer mark() { 656 return super.mark(); 657 } 658 659 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 660 @Override reset()661 public Buffer reset() { 662 return super.reset(); 663 } 664 665 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 666 @Override clear()667 public Buffer clear() { 668 return super.clear(); 669 } 670 671 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 672 @Override flip()673 public Buffer flip() { 674 return super.flip(); 675 } 676 677 @CovariantReturnType(returnType = FloatBuffer.class, presentAfter = 28) 678 @Override rewind()679 public Buffer rewind() { 680 return super.rewind(); 681 } 682 // END Android-added: covariant overloads of *Buffer methods that return this. 683 684 /** 685 * Compacts this buffer <i>(optional operation)</i>. 686 * 687 * <p> The floats between the buffer's current position and its limit, 688 * if any, are copied to the beginning of the buffer. That is, the 689 * float at index <i>p</i> = <tt>position()</tt> is copied 690 * to index zero, the float at index <i>p</i> + 1 is copied 691 * to index one, and so forth until the float at index 692 * <tt>limit()</tt> - 1 is copied to index 693 * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>. 694 * The buffer's position is then set to <i>n+1</i> and its limit is set to 695 * its capacity. The mark, if defined, is discarded. 696 * 697 * <p> The buffer's position is set to the number of floats copied, 698 * rather than to zero, so that an invocation of this method can be 699 * followed immediately by an invocation of another relative <i>put</i> 700 * method. </p> 701 * 702 703 * 704 * @return This buffer 705 * 706 * @throws ReadOnlyBufferException 707 * If this buffer is read-only 708 */ compact()709 public abstract FloatBuffer compact(); 710 711 /** 712 * Tells whether or not this float buffer is direct. 713 * 714 * @return <tt>true</tt> if, and only if, this buffer is direct 715 */ isDirect()716 public abstract boolean isDirect(); 717 718 719 /** 720 * Returns a string summarizing the state of this buffer. 721 * 722 * @return A summary string 723 */ toString()724 public String toString() { 725 StringBuffer sb = new StringBuffer(); 726 sb.append(getClass().getName()); 727 sb.append("[pos="); 728 sb.append(position()); 729 sb.append(" lim="); 730 sb.append(limit()); 731 sb.append(" cap="); 732 sb.append(capacity()); 733 sb.append("]"); 734 return sb.toString(); 735 } 736 737 738 /** 739 * Returns the current hash code of this buffer. 740 * 741 * <p> The hash code of a float buffer depends only upon its remaining 742 * elements; that is, upon the elements from <tt>position()</tt> up to, and 743 * including, the element at <tt>limit()</tt> - <tt>1</tt>. 744 * 745 * <p> Because buffer hash codes are content-dependent, it is inadvisable 746 * to use buffers as keys in hash maps or similar data structures unless it 747 * is known that their contents will not change. </p> 748 * 749 * @return The current hash code of this buffer 750 */ hashCode()751 public int hashCode() { 752 int h = 1; 753 int p = position(); 754 for (int i = limit() - 1; i >= p; i--) 755 h = 31 * h + (int) get(i); 756 return h; 757 } 758 759 /** 760 * Tells whether or not this buffer is equal to another object. 761 * 762 * <p> Two float buffers are equal if, and only if, 763 * 764 * <ol> 765 * 766 * <li><p> They have the same element type, </p></li> 767 * 768 * <li><p> They have the same number of remaining elements, and 769 * </p></li> 770 * 771 * <li><p> The two sequences of remaining elements, considered 772 * independently of their starting positions, are pointwise equal. 773 774 * This method considers two float elements {@code a} and {@code b} 775 * to be equal if 776 * {@code (a == b) || (Float.isNaN(a) && Float.isNaN(b))}. 777 * The values {@code -0.0} and {@code +0.0} are considered to be 778 * equal, unlike {@link Float#equals(Object)}. 779 780 * </p></li> 781 * 782 * </ol> 783 * 784 * <p> A float buffer is not equal to any other type of object. </p> 785 * 786 * @param ob The object to which this buffer is to be compared 787 * 788 * @return <tt>true</tt> if, and only if, this buffer is equal to the 789 * given object 790 */ equals(Object ob)791 public boolean equals(Object ob) { 792 if (this == ob) 793 return true; 794 if (!(ob instanceof FloatBuffer)) 795 return false; 796 FloatBuffer that = (FloatBuffer)ob; 797 if (this.remaining() != that.remaining()) 798 return false; 799 int p = this.position(); 800 for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) 801 if (!equals(this.get(i), that.get(j))) 802 return false; 803 return true; 804 } 805 equals(float x, float y)806 private static boolean equals(float x, float y) { 807 808 return (x == y) || (Float.isNaN(x) && Float.isNaN(y)); 809 810 811 } 812 813 /** 814 * Compares this buffer to another. 815 * 816 * <p> Two float buffers are compared by comparing their sequences of 817 * remaining elements lexicographically, without regard to the starting 818 * position of each sequence within its corresponding buffer. 819 820 * Pairs of {@code float} elements are compared as if by invoking 821 * {@link Float#compare(float,float)}, except that 822 * {@code -0.0} and {@code 0.0} are considered to be equal. 823 * {@code Float.NaN} is considered by this method to be equal 824 * to itself and greater than all other {@code float} values 825 * (including {@code Float.POSITIVE_INFINITY}). 826 * 827 * 828 * 829 * 830 * 831 * <p> A float buffer is not comparable to any other type of object. 832 * 833 * @return A negative integer, zero, or a positive integer as this buffer 834 * is less than, equal to, or greater than the given buffer 835 */ compareTo(FloatBuffer that)836 public int compareTo(FloatBuffer that) { 837 int n = this.position() + Math.min(this.remaining(), that.remaining()); 838 for (int i = this.position(), j = that.position(); i < n; i++, j++) { 839 int cmp = compare(this.get(i), that.get(j)); 840 if (cmp != 0) 841 return cmp; 842 } 843 return this.remaining() - that.remaining(); 844 } 845 compare(float x, float y)846 private static int compare(float x, float y) { 847 848 return ((x < y) ? -1 : 849 (x > y) ? +1 : 850 (x == y) ? 0 : 851 Float.isNaN(x) ? (Float.isNaN(y) ? 0 : +1) : -1); 852 853 } 854 855 // -- Other char stuff -- 856 857 // -- Other byte stuff: Access to binary data -- 858 859 /** 860 * Retrieves this buffer's byte order. 861 * 862 * <p> The byte order of a float buffer created by allocation or by 863 * wrapping an existing <tt>float</tt> array is the {@link 864 * ByteOrder#nativeOrder native order} of the underlying 865 * hardware. The byte order of a float buffer created as a <a 866 * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the 867 * byte buffer at the moment that the view is created. </p> 868 * 869 * @return This buffer's byte order 870 */ order()871 public abstract ByteOrder order(); 872 873 874 } 875