< prev index next >

src/java.base/share/classes/java/util/stream/LongPipeline.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 2015, 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


 150     @Override
 151     @SuppressWarnings("unchecked")
 152     final Spliterator.OfLong lazySpliterator(Supplier<? extends Spliterator<Long>> supplier) {
 153         return new StreamSpliterators.DelegatingSpliterator.OfLong((Supplier<Spliterator.OfLong>) supplier);
 154     }
 155 
 156     @Override
 157     final boolean forEachWithCancel(Spliterator<Long> spliterator, Sink<Long> sink) {
 158         Spliterator.OfLong spl = adapt(spliterator);
 159         LongConsumer adaptedSink =  adapt(sink);
 160         boolean cancelled;
 161         do { } while (!(cancelled = sink.cancellationRequested()) && spl.tryAdvance(adaptedSink));
 162         return cancelled;
 163     }
 164 
 165     @Override
 166     final Node.Builder<Long> makeNodeBuilder(long exactSizeIfKnown, IntFunction<Long[]> generator) {
 167         return Nodes.longBuilder(exactSizeIfKnown);
 168     }
 169 













 170 
 171     // LongStream
 172 
 173     @Override
 174     public final PrimitiveIterator.OfLong iterator() {
 175         return Spliterators.iterator(spliterator());
 176     }
 177 
 178     @Override
 179     public final Spliterator.OfLong spliterator() {
 180         return adapt(super.spliterator());
 181     }
 182 
 183     // Stateless intermediate ops from LongStream
 184 
 185     @Override
 186     public final DoubleStream asDoubleStream() {
 187         return new DoublePipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE,
 188                                                     StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 189             @Override
 190             Sink<Long> opWrapSink(int flags, Sink<Double> sink) {
 191                 return new Sink.ChainedLong<Double>(sink) {
 192                     @Override
 193                     public void accept(long t) {
 194                         downstream.accept((double) t);
 195                     }
 196                 };
 197             }
 198         };
 199     }
 200 
 201     @Override
 202     public final Stream<Long> boxed() {
 203         return mapToObj(Long::valueOf);
 204     }
 205 
 206     @Override
 207     public final LongStream map(LongUnaryOperator mapper) {
 208         Objects.requireNonNull(mapper);
 209         return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
 210                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 211             @Override
 212             Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
 213                 return new Sink.ChainedLong<Long>(sink) {
 214                     @Override
 215                     public void accept(long t) {
 216                         downstream.accept(mapper.applyAsLong(t));
 217                     }
 218                 };
 219             }
 220         };
 221     }
 222 
 223     @Override
 224     public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
 225         Objects.requireNonNull(mapper);
 226         return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE,
 227                                                           StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 228             @Override
 229             Sink<Long> opWrapSink(int flags, Sink<U> sink) {
 230                 return new Sink.ChainedLong<U>(sink) {
 231                     @Override
 232                     public void accept(long t) {
 233                         downstream.accept(mapper.apply(t));
 234                     }
 235                 };
 236             }
 237         };
 238     }
 239 
 240     @Override
 241     public final IntStream mapToInt(LongToIntFunction mapper) {
 242         Objects.requireNonNull(mapper);
 243         return new IntPipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE,
 244                                                  StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 245             @Override
 246             Sink<Long> opWrapSink(int flags, Sink<Integer> sink) {
 247                 return new Sink.ChainedLong<Integer>(sink) {
 248                     @Override
 249                     public void accept(long t) {
 250                         downstream.accept(mapper.applyAsInt(t));
 251                     }
 252                 };
 253             }
 254         };
 255     }
 256 
 257     @Override


   1 /*
   2  * Copyright (c) 2013, 2016, 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


 150     @Override
 151     @SuppressWarnings("unchecked")
 152     final Spliterator.OfLong lazySpliterator(Supplier<? extends Spliterator<Long>> supplier) {
 153         return new StreamSpliterators.DelegatingSpliterator.OfLong((Supplier<Spliterator.OfLong>) supplier);
 154     }
 155 
 156     @Override
 157     final boolean forEachWithCancel(Spliterator<Long> spliterator, Sink<Long> sink) {
 158         Spliterator.OfLong spl = adapt(spliterator);
 159         LongConsumer adaptedSink =  adapt(sink);
 160         boolean cancelled;
 161         do { } while (!(cancelled = sink.cancellationRequested()) && spl.tryAdvance(adaptedSink));
 162         return cancelled;
 163     }
 164 
 165     @Override
 166     final Node.Builder<Long> makeNodeBuilder(long exactSizeIfKnown, IntFunction<Long[]> generator) {
 167         return Nodes.longBuilder(exactSizeIfKnown);
 168     }
 169 
 170     private <U> Stream<U> mapToObj(LongFunction<? extends U> mapper, int opFlags) {
 171         return new ReferencePipeline.StatelessOp<Long, U>(this, StreamShape.LONG_VALUE, opFlags) {
 172             @Override
 173             Sink<Long> opWrapSink(int flags, Sink<U> sink) {
 174                 return new Sink.ChainedLong<U>(sink) {
 175                     @Override
 176                     public void accept(long t) {
 177                         downstream.accept(mapper.apply(t));
 178                     }
 179                 };
 180             }
 181         };
 182     }
 183 
 184     // LongStream
 185 
 186     @Override
 187     public final PrimitiveIterator.OfLong iterator() {
 188         return Spliterators.iterator(spliterator());
 189     }
 190 
 191     @Override
 192     public final Spliterator.OfLong spliterator() {
 193         return adapt(super.spliterator());
 194     }
 195 
 196     // Stateless intermediate ops from LongStream
 197 
 198     @Override
 199     public final DoubleStream asDoubleStream() {
 200         return new DoublePipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_DISTINCT) {

 201             @Override
 202             Sink<Long> opWrapSink(int flags, Sink<Double> sink) {
 203                 return new Sink.ChainedLong<Double>(sink) {
 204                     @Override
 205                     public void accept(long t) {
 206                         downstream.accept((double) t);
 207                     }
 208                 };
 209             }
 210         };
 211     }
 212 
 213     @Override
 214     public final Stream<Long> boxed() {
 215         return mapToObj(Long::valueOf, 0);
 216     }
 217 
 218     @Override
 219     public final LongStream map(LongUnaryOperator mapper) {
 220         Objects.requireNonNull(mapper);
 221         return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
 222                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 223             @Override
 224             Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
 225                 return new Sink.ChainedLong<Long>(sink) {
 226                     @Override
 227                     public void accept(long t) {
 228                         downstream.accept(mapper.applyAsLong(t));
 229                     }
 230                 };
 231             }
 232         };
 233     }
 234 
 235     @Override
 236     public final <U> Stream<U> mapToObj(LongFunction<? extends U> mapper) {
 237         Objects.requireNonNull(mapper);
 238         return mapToObj(mapper, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT);











 239     }
 240 
 241     @Override
 242     public final IntStream mapToInt(LongToIntFunction mapper) {
 243         Objects.requireNonNull(mapper);
 244         return new IntPipeline.StatelessOp<Long>(this, StreamShape.LONG_VALUE,
 245                                                  StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 246             @Override
 247             Sink<Long> opWrapSink(int flags, Sink<Integer> sink) {
 248                 return new Sink.ChainedLong<Integer>(sink) {
 249                     @Override
 250                     public void accept(long t) {
 251                         downstream.accept(mapper.applyAsInt(t));
 252                     }
 253                 };
 254             }
 255         };
 256     }
 257 
 258     @Override


< prev index next >