jdk/src/share/classes/java/util/stream/DistinctOps.java
author darcy
Wed, 10 Jul 2013 11:05:39 -0700
changeset 18796 486b43748d9b
parent 17182 b786c0de868c
child 19593 ce0cd954351c
permissions -rw-r--r--
8020294: Fix doclint issues in java.util.Spliterator Reviewed-by: psandoz
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) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    57
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    58
            <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    59
                                              Spliterator<P_IN> spliterator,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    60
                                              IntFunction<T[]> generator) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    61
                if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    62
                    // No-op
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    63
                    return helper.evaluate(spliterator, false, generator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    64
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    65
                else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    66
                    // If the stream is SORTED then it should also be ORDERED so the following will also
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    67
                    // preserve the sort order
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    68
                    TerminalOp<T, LinkedHashSet<T>> reduceOp
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    69
                            = ReduceOps.<T, LinkedHashSet<T>>makeRef(LinkedHashSet::new, LinkedHashSet::add,
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    70
                                                                     LinkedHashSet::addAll);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    71
                    return Nodes.node(reduceOp.evaluateParallel(helper, spliterator));
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    72
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    73
                else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    74
                    // Holder of null state since ConcurrentHashMap does not support null values
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    75
                    AtomicBoolean seenNull = new AtomicBoolean(false);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    76
                    ConcurrentHashMap<T, Boolean> map = new ConcurrentHashMap<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    77
                    TerminalOp<T, Void> forEachOp = ForEachOps.makeRef(t -> {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    78
                        if (t == null)
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    79
                            seenNull.set(true);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    80
                        else
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    81
                            map.putIfAbsent(t, Boolean.TRUE);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    82
                    }, false);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    83
                    forEachOp.evaluateParallel(helper, spliterator);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    84
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    85
                    // 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
    86
                    // and add null
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    87
                    Set<T> keys = map.keySet();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    88
                    if (seenNull.get()) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    89
                        // TODO Implement a more efficient set-union view, rather than copying
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    90
                        keys = new HashSet<>(keys);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    91
                        keys.add(null);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    92
                    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    93
                    return Nodes.node(keys);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    94
                }
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
            @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    98
            Sink<T> opWrapSink(int flags, Sink<T> sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
    99
                Objects.requireNonNull(sink);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   100
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   101
                if (StreamOpFlag.DISTINCT.isKnown(flags)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   102
                    return sink;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   103
                } else if (StreamOpFlag.SORTED.isKnown(flags)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   104
                    return new Sink.ChainedReference<T>(sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   105
                        boolean seenNull;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   106
                        T lastSeen;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   107
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   108
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   109
                        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   110
                            seenNull = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   111
                            lastSeen = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   112
                            downstream.begin(-1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   113
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   114
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   115
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   116
                        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   117
                            seenNull = false;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   118
                            lastSeen = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   119
                            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   120
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   121
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   122
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   123
                        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   124
                            if (t == null) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   125
                                if (!seenNull) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   126
                                    seenNull = true;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   127
                                    downstream.accept(lastSeen = null);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   128
                                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   129
                            } else if (lastSeen == null || !t.equals(lastSeen)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   130
                                downstream.accept(lastSeen = t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   131
                            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   132
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   133
                    };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   134
                } else {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   135
                    return new Sink.ChainedReference<T>(sink) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   136
                        Set<T> seen;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   137
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   138
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   139
                        public void begin(long size) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   140
                            seen = new HashSet<>();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   141
                            downstream.begin(-1);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   142
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   143
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   144
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   145
                        public void end() {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   146
                            seen = null;
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   147
                            downstream.end();
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   148
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   149
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   150
                        @Override
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   151
                        public void accept(T t) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   152
                            if (!seen.contains(t)) {
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   153
                                seen.add(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   154
                                downstream.accept(t);
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   155
                            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   156
                        }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   157
                    };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   158
                }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   159
            }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   160
        };
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   161
    }
b786c0de868c 8011920: Main streams implementation
mduigou
parents:
diff changeset
   162
}