|
17 | 17 |
|
18 | 18 | import java.nio.ByteBuffer; |
19 | 19 | import java.nio.ByteOrder; |
20 | | -import java.util.function.Consumer; |
21 | 20 |
|
22 | 21 | /** |
23 | 22 | * A reference counted buffer of memory, with separate reader and writer offsets. |
@@ -490,19 +489,74 @@ default void ensureWritable(int size) { |
490 | 489 | * |
491 | 490 | * @return The number of components in this buffer. |
492 | 491 | */ |
493 | | - int componentCount(); |
| 492 | + int countComponents(); |
494 | 493 |
|
495 | 494 | /** |
496 | | - * Process all readable components of this buffer, and return the number of components consumed. |
| 495 | + * Get the number of "components" in this buffer, that are readable. These are the components that would be |
| 496 | + * processed by {@link #forEachReadable(int, ComponentProcessor)}. For composite buffers, this is the number of |
| 497 | + * transitive constituent buffers that are readable, while non-composite buffers only have at most one readable |
| 498 | + * component. |
497 | 499 | * <p> |
498 | | - * The number of components consumed may be less than the {@linkplain #componentCount() component count} if not all |
499 | | - * of them have readable data. |
| 500 | + * The number of readable components may be less than the {@link #countComponents() component count}, if not all of |
| 501 | + * them have readable data. |
500 | 502 | * |
| 503 | + * @return The number of readable components in this buffer. |
| 504 | + */ |
| 505 | + int countReadableComponents(); |
| 506 | + |
| 507 | + /** |
| 508 | + * Get the number of "components" in this buffer, that are writable. These are the components that would be |
| 509 | + * processed by {@link #forEachWritable(int, ComponentProcessor)}. For composite buffers, this is the number of |
| 510 | + * transitive constituent buffers that are writable, while non-composite buffers only have at most one writable |
| 511 | + * component. |
| 512 | + * <p> |
| 513 | + * The number of writable components may be less than the {@link #countComponents() component count}, if not all of |
| 514 | + * them have space for writing. |
| 515 | + * |
| 516 | + * @return The number of writable components in this buffer. |
| 517 | + */ |
| 518 | + int countWritableComponents(); |
| 519 | + |
| 520 | + /** |
| 521 | + * Process all readable components of this buffer, and return the number of components processed. |
| 522 | + * <p> |
| 523 | + * The given {@linkplain ComponentProcessor processor} is called for each component in this buffer, and passed a |
| 524 | + * component index, for the given component in the iteration, and a {@link Component} object for accessing the data |
| 525 | + * within the given component. |
| 526 | + * <p> |
| 527 | + * The component index is specific to the particular invokation of this method, and may change. The first call to |
| 528 | + * the consumer will be passed the given initial index, and the next call will be passed the initial index plus one, |
| 529 | + * and so on. |
| 530 | + * <p> |
| 531 | + * The {@link ComponentProcessor} may stop the iteration at any time by returning {@code false}. This may cause the |
| 532 | + * number of components processed to be returned as a negative number (to signal early return), and the number of |
| 533 | + * components processed may then be less than the {@linkplain #countReadableComponents() readable component count}. |
| 534 | + * <p> |
501 | 535 | * <strong>Note</strong> that the {@link Component} instance passed to the consumer could be reused for multiple |
502 | 536 | * calls, so the data must be extracted from the component in the context of the iteration. |
| 537 | + * <p> |
| 538 | + * The {@link ByteBuffer} instances obtained from the component, share life time with that internal component. |
| 539 | + * This means they can be accessed as long as the internal memory store remain unchanged. Methods that may cause |
| 540 | + * such changes, are any method that requires the buffer to be {@linkplain #isOwned() owned}. |
| 541 | + * <p> |
| 542 | + * The best way to ensure this doesn't cause any trouble, is to use the buffers directly as part of the iteration, |
| 543 | + * or immediately after the iteration. |
| 544 | + * <p> |
| 545 | + * <strong>Note</strong> that the arrays, memory addresses, and byte buffers exposed as components by this method, |
| 546 | + * should not be used for changing the buffer contents. Doing so may cause undefined behaviour. |
| 547 | + * <p> |
| 548 | + * Changes to position and limit of the byte buffers exposed via the processed components, are not reflected back to |
| 549 | + * this buffer instance. |
503 | 550 | * |
504 | | - * @param consumer The consumer that will be used to process the buffer components. |
505 | | - * @return The number of readable components processed, which may be less than {@link #componentCount()}. |
| 551 | + * @param initialIndex The initial index of the iteration, and the index that will be passed to the first call to |
| 552 | + * the {@linkplain ComponentProcessor#process(int, Component) processor}. |
| 553 | + * @param processor The processor that will be used to process the buffer components. |
| 554 | + * @return The number of readable components processed, as a positive number of all readable components were |
| 555 | + * processed, or as a negative number if the iteration was stopped because |
| 556 | + * {@link ComponentProcessor#process(int, Component)} returned {@code false}. |
| 557 | + * In any case, the number of components processed may be less than {@link #countComponents()}. |
506 | 558 | */ |
507 | | - int forEachReadable(Consumer<Component> consumer); |
| 559 | + int forEachReadable(int initialIndex, ComponentProcessor processor); |
| 560 | + |
| 561 | + int forEachWritable(int initialIndex, ComponentProcessor processor); |
508 | 562 | } |
0 commit comments