src/java.base/share/classes/java/util/stream/AbstractSpinedBuffer.java
author jboes
Thu, 03 Oct 2019 18:59:56 +0100
changeset 58459 e25b317d0350
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231161: Wrong return type in code sample in Collector API documentation Summary: Correct declaration of container from R to A and add compilation test Reviewed-by: smarks, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     1
/*
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     4
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    10
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    15
 * accompanied this code).
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    16
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    20
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    23
 * questions.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    24
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    25
package java.util.stream;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    26
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    27
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
 * Base class for a data structure for gathering elements into a buffer and then
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
 * iterating them. Maintains an array of increasingly sized arrays, so there is
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
 * no copying cost associated with growing the data structure.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
abstract class AbstractSpinedBuffer {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
     * Minimum power-of-two for the first chunk.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
    public static final int MIN_CHUNK_POWER = 4;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
     * Minimum size for the first chunk.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
    public static final int MIN_CHUNK_SIZE = 1 << MIN_CHUNK_POWER;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
     * Max power-of-two for chunks.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
    public static final int MAX_CHUNK_POWER = 30;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
     * Minimum array size for array-of-chunks.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
    public static final int MIN_SPINE_SIZE = 8;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
     * log2 of the size of the first chunk.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
    protected final int initialChunkPower;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
     * Index of the *next* element to write; may point into, or just outside of,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
     * the current chunk.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
    protected int elementIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
     * Index of the *current* chunk in the spine array, if the spine array is
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
     * non-null.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
    protected int spineIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
     * Count of elements in all prior chunks.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
    protected long[] priorElementCount;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
     * Construct with an initial capacity of 16.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
    protected AbstractSpinedBuffer() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
        this.initialChunkPower = MIN_CHUNK_POWER;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
     * Construct with a specified initial capacity.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
     * @param initialCapacity The minimum expected number of elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
    protected AbstractSpinedBuffer(int initialCapacity) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
        if (initialCapacity < 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
        this.initialChunkPower = Math.max(MIN_CHUNK_POWER,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
                                          Integer.SIZE - Integer.numberOfLeadingZeros(initialCapacity - 1));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
     * Is the buffer currently empty?
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
    public boolean isEmpty() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
        return (spineIndex == 0) && (elementIndex == 0);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
     * How many elements are currently in the buffer?
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
    public long count() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
        return (spineIndex == 0)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
               ? elementIndex
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
               : priorElementCount[spineIndex] + elementIndex;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
     * How big should the nth chunk be?
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
    protected int chunkSize(int n) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
        int power = (n == 0 || n == 1)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
                    ? initialChunkPower
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
                    : Math.min(initialChunkPower + n - 1, AbstractSpinedBuffer.MAX_CHUNK_POWER);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
        return 1 << power;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
     * Remove all data from the buffer
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
    public abstract void clear();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
}