jdk/src/share/classes/java/util/stream/DistinctOps.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 21422 6fca66995a27
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
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
import java.util.HashSet;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    28
import java.util.LinkedHashSet;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    29
import java.util.Objects;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    30
import java.util.Set;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    31
import java.util.Spliterator;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    33
import java.util.concurrent.atomic.AtomicBoolean;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    34
import java.util.function.IntFunction;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    35
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    36
/**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    37
 * Factory methods for transforming streams into duplicate-free streams, using
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    38
 * {@link Object#equals(Object)} to determine equality.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    39
 *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    40
 * @since 1.8
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    41
 */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    42
final class DistinctOps {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    43
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    44
    private DistinctOps() { }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    45
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    46
    /**
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    47
     * Appends a "distinct" operation to the provided stream, and returns the
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    48
     * new stream.
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    49
     *
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    50
     * @param <T> the type of both input and output elements
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    51
     * @param upstream a reference stream with element type T
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    52
     * @return the new stream
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    53
     */
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    54
    static <T> ReferencePipeline<T, T> makeRef(AbstractPipeline<?, T, ?> upstream) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    55
        return new ReferencePipeline.StatefulOp<T, T>(upstream, StreamShape.REFERENCE,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    56
                                                      StreamOpFlag.IS_DISTINCT | StreamOpFlag.NOT_SIZED) {
21422
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    57
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    58
            <P_IN> Node<T> reduce(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    59
                // If the stream is SORTED then it should also be ORDERED so the following will also
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    60
                // preserve the sort order
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    61
                TerminalOp<T, LinkedHashSet<T>> reduceOp
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    62
                        = ReduceOps.<T, LinkedHashSet<T>>makeRef(LinkedHashSet::new, LinkedHashSet::add,
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    63
                                                                 LinkedHashSet::addAll);
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    64
                return Nodes.node(reduceOp.evaluateParallel(helper, spliterator));
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    65
            }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    66
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
            <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
                                              Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
                                              IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
                if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
                    // No-op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
                    return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
                else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
21422
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
    76
                    return reduce(helper, spliterator);
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
                    // Holder of null state since ConcurrentHashMap does not support null values
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
                    AtomicBoolean seenNull = new AtomicBoolean(false);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
                    ConcurrentHashMap<T, Boolean> map = new ConcurrentHashMap<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
                    TerminalOp<T, Void> forEachOp = ForEachOps.makeRef(t -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
                        if (t == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
                            seenNull.set(true);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
                        else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    86
                            map.putIfAbsent(t, Boolean.TRUE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
                    }, false);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
                    forEachOp.evaluateParallel(helper, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
                    // If null has been seen then copy the key set into a HashSet that supports null values
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
                    // and add null
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
                    Set<T> keys = map.keySet();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
                    if (seenNull.get()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
                        // TODO Implement a more efficient set-union view, rather than copying
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    95
                        keys = new HashSet<>(keys);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    96
                        keys.add(null);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    97
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
                    return Nodes.node(keys);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
            @Override
21422
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   103
            <P_IN> Spliterator<T> opEvaluateParallelLazy(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   104
                if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   105
                    // No-op
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   106
                    return helper.wrapSpliterator(spliterator);
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   107
                }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   108
                else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   109
                    // Not lazy, barrier required to preserve order
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   110
                    return reduce(helper, spliterator).spliterator();
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   111
                }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   112
                else {
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   113
                    // Lazy
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   114
                    return new StreamSpliterators.DistinctSpliterator<>(helper.wrapSpliterator(spliterator));
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   115
                }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   116
            }
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   117
6fca66995a27 8027316: Distinct operation on an unordered stream should not be a barrier
psandoz
parents: 19593
diff changeset
   118
            @Override
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
            Sink<T> opWrapSink(int flags, Sink<T> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
                Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
                if (StreamOpFlag.DISTINCT.isKnown(flags)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
                    return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
                } else if (StreamOpFlag.SORTED.isKnown(flags)) {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 17182
diff changeset
   125
                    return new Sink.ChainedReference<T, T>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
                        boolean seenNull;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
                        T lastSeen;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
                        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
                            seenNull = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
                            lastSeen = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
                            downstream.begin(-1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
                        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                            seenNull = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                            lastSeen = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
                            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
                        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
                            if (t == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
                                if (!seenNull) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
                                    seenNull = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                                    downstream.accept(lastSeen = null);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
                                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
                            } else if (lastSeen == null || !t.equals(lastSeen)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
                                downstream.accept(lastSeen = t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
                            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
                    };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
                } else {
19593
ce0cd954351c 8023681: Fix raw type warning caused by Sink
henryjen
parents: 17182
diff changeset
   156
                    return new Sink.ChainedReference<T, T>(sink) {
17182
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                        Set<T> seen;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
                        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
                            seen = new HashSet<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
                            downstream.begin(-1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   163
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   164
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   165
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   166
                        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   167
                            seen = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   168
                            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   169
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   170
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   171
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   172
                        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   173
                            if (!seen.contains(t)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   174
                                seen.add(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   175
                                downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   176
                            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   177
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   178
                    };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   179
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   180
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   181
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   182
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   183
}