< prev index next >

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

Print this page


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


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













 173 
 174     // IntStream
 175 
 176     @Override
 177     public final PrimitiveIterator.OfInt iterator() {
 178         return Spliterators.iterator(spliterator());
 179     }
 180 
 181     @Override
 182     public final Spliterator.OfInt spliterator() {
 183         return adapt(super.spliterator());
 184     }
 185 
 186     // Stateless intermediate ops from IntStream
 187 
 188     @Override
 189     public final LongStream asLongStream() {
 190         return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
 191                                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 192             @Override
 193             Sink<Integer> opWrapSink(int flags, Sink<Long> sink) {
 194                 return new Sink.ChainedInt<Long>(sink) {
 195                     @Override
 196                     public void accept(int t) {
 197                         downstream.accept((long) t);
 198                     }
 199                 };
 200             }
 201         };
 202     }
 203 
 204     @Override
 205     public final DoubleStream asDoubleStream() {
 206         return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
 207                                                        StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 208             @Override
 209             Sink<Integer> opWrapSink(int flags, Sink<Double> sink) {
 210                 return new Sink.ChainedInt<Double>(sink) {
 211                     @Override
 212                     public void accept(int t) {
 213                         downstream.accept((double) t);
 214                     }
 215                 };
 216             }
 217         };
 218     }
 219 
 220     @Override
 221     public final Stream<Integer> boxed() {
 222         return mapToObj(Integer::valueOf);
 223     }
 224 
 225     @Override
 226     public final IntStream map(IntUnaryOperator mapper) {
 227         Objects.requireNonNull(mapper);
 228         return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
 229                                         StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 230             @Override
 231             Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
 232                 return new Sink.ChainedInt<Integer>(sink) {
 233                     @Override
 234                     public void accept(int t) {
 235                         downstream.accept(mapper.applyAsInt(t));
 236                     }
 237                 };
 238             }
 239         };
 240     }
 241 
 242     @Override
 243     public final <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
 244         Objects.requireNonNull(mapper);
 245         return new ReferencePipeline.StatelessOp<Integer, U>(this, StreamShape.INT_VALUE,
 246                                                              StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 247             @Override
 248             Sink<Integer> opWrapSink(int flags, Sink<U> sink) {
 249                 return new Sink.ChainedInt<U>(sink) {
 250                     @Override
 251                     public void accept(int t) {
 252                         downstream.accept(mapper.apply(t));
 253                     }
 254                 };
 255             }
 256         };
 257     }
 258 
 259     @Override
 260     public final LongStream mapToLong(IntToLongFunction mapper) {
 261         Objects.requireNonNull(mapper);
 262         return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
 263                                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 264             @Override
 265             Sink<Integer> opWrapSink(int flags, Sink<Long> sink) {
 266                 return new Sink.ChainedInt<Long>(sink) {
 267                     @Override
 268                     public void accept(int t) {
 269                         downstream.accept(mapper.applyAsLong(t));
 270                     }
 271                 };
 272             }
 273         };
 274     }
 275 
 276     @Override


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


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

 204             @Override
 205             Sink<Integer> opWrapSink(int flags, Sink<Long> sink) {
 206                 return new Sink.ChainedInt<Long>(sink) {
 207                     @Override
 208                     public void accept(int t) {
 209                         downstream.accept((long) t);
 210                     }
 211                 };
 212             }
 213         };
 214     }
 215 
 216     @Override
 217     public final DoubleStream asDoubleStream() {
 218         return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE, StreamOpFlag.NOT_DISTINCT) {

 219             @Override
 220             Sink<Integer> opWrapSink(int flags, Sink<Double> sink) {
 221                 return new Sink.ChainedInt<Double>(sink) {
 222                     @Override
 223                     public void accept(int t) {
 224                         downstream.accept((double) t);
 225                     }
 226                 };
 227             }
 228         };
 229     }
 230 
 231     @Override
 232     public final Stream<Integer> boxed() {
 233         return mapToObj(Integer::valueOf, 0);
 234     }
 235 
 236     @Override
 237     public final IntStream map(IntUnaryOperator mapper) {
 238         Objects.requireNonNull(mapper);
 239         return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
 240                                         StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 241             @Override
 242             Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
 243                 return new Sink.ChainedInt<Integer>(sink) {
 244                     @Override
 245                     public void accept(int t) {
 246                         downstream.accept(mapper.applyAsInt(t));
 247                     }
 248                 };
 249             }
 250         };
 251     }
 252 
 253     @Override
 254     public final <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
 255         Objects.requireNonNull(mapper);
 256         return mapToObj(mapper, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT);











 257     }
 258 
 259     @Override
 260     public final LongStream mapToLong(IntToLongFunction mapper) {
 261         Objects.requireNonNull(mapper);
 262         return new LongPipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
 263                                                      StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
 264             @Override
 265             Sink<Integer> opWrapSink(int flags, Sink<Long> sink) {
 266                 return new Sink.ChainedInt<Long>(sink) {
 267                     @Override
 268                     public void accept(int t) {
 269                         downstream.accept(mapper.applyAsLong(t));
 270                     }
 271                 };
 272             }
 273         };
 274     }
 275 
 276     @Override


< prev index next >