/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.util;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class StreamUtils {
    
Turn Enumeration into a Stream
Params:
  • e – Enumeration
Type parameters:
  • <T> – Enumeration type
Returns:Stream
/** * Turn Enumeration into a Stream * * @param <T> Enumeration type * @param e Enumeration * @return Stream */
public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) { return StreamSupport.stream( new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED) { public boolean tryAdvance(Consumer<? super T> action) { if (e.hasMoreElements()) { action.accept(e.nextElement()); return true; } return false; } public void forEachRemaining(Consumer<? super T> action) { while (e.hasMoreElements()) { action.accept(e.nextElement()); } } }, false); }
Turn Iterator into a Stream
Params:
  • i – Iterator
Type parameters:
  • <T> – Iterator type
Returns:Stream
/** * Turn Iterator into a Stream * * @param <T> Iterator type * @param i Iterator * @return Stream */
public static <T> Stream<T> iteratorAsStream(Iterator<T> i) { return StreamSupport.stream( Spliterators.spliteratorUnknownSize(i, Spliterator.ORDERED), false); } }