All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.davidbracewell.stream.MStream Maven / Gradle / Ivy

There is a newer version: 0.5
Show newest version
/*
 * (c) 2005 David B. Bracewell
 *
 * 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
 *
 *   http://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 com.davidbracewell.stream;

import com.davidbracewell.conversion.Cast;
import com.davidbracewell.function.SerializableBinaryOperator;
import com.davidbracewell.function.SerializableConsumer;
import com.davidbracewell.function.SerializableFunction;
import com.davidbracewell.function.SerializablePredicate;
import com.davidbracewell.io.Resources;
import com.davidbracewell.io.resource.Resource;
import com.google.common.collect.Ordering;
import lombok.NonNull;

import java.io.Closeable;
import java.util.*;
import java.util.function.ToDoubleFunction;
import java.util.stream.Collector;

/**
 * The interface M stream.
 *
 * @param  the type parameter
 * @author David B. Bracewell
 */
public interface MStream extends Closeable {

  /**
   * Filter m stream.
   *
   * @param predicate the predicate
   * @return the m stream
   */
  MStream filter(SerializablePredicate predicate);

  /**
   * Map m stream.
   *
   * @param       the type parameter
   * @param function the function
   * @return the m stream
   */
   MStream map(SerializableFunction function);

  /**
   * Flat map m stream.
   *
   * @param     the type parameter
   * @param mapper the mapper
   * @return the m stream
   */
   MStream flatMap(SerializableFunction> mapper);

  /**
   * Flat map to pair m pair stream.
   *
   * @param       the type parameter
   * @param       the type parameter
   * @param function the function
   * @return the m pair stream
   */
   MPairStream flatMapToPair(SerializableFunction>> function);

  /**
   * Map to pair m pair stream.
   *
   * @param       the type parameter
   * @param       the type parameter
   * @param function the function
   * @return the m pair stream
   */
   MPairStream mapToPair(SerializableFunction> function);

  /**
   * Group by m pair stream.
   *
   * @param       the type parameter
   * @param function the function
   * @return the m pair stream
   */
   MPairStream> groupBy(SerializableFunction function);

  /**
   * Collect r.
   *
   * @param        the type parameter
   * @param collector the collector
   * @return the r
   */
   R collect(Collector collector);

  /**
   * Collect list.
   *
   * @return the list
   */
  List collect();

  /**
   * Reduce optional.
   *
   * @param reducer the reducer
   * @return the optional
   */
  Optional reduce(SerializableBinaryOperator reducer);

  /**
   * Fold t.
   *
   * @param zeroValue the zero value
   * @param operator  the operator
   * @return the t
   */
  T fold(T zeroValue, SerializableBinaryOperator operator);

  /**
   * For each.
   *
   * @param consumer the consumer
   */
  void forEach(SerializableConsumer consumer);

  void forEachLocal(SerializableConsumer consumer);

  /**
   * Iterator iterator.
   *
   * @return the iterator
   */
  Iterator iterator();

  /**
   * First optional.
   *
   * @return the optional
   */
  Optional first();

  /**
   * Sample m stream.
   *
   * @param number the number
   * @return the m stream
   */
  MStream sample(int number);

  /**
   * Size long.
   *
   * @return the long
   */
  long count();

  /**
   * Is empty boolean.
   *
   * @return the boolean
   */
  boolean isEmpty();

  /**
   * Count by value map.
   *
   * @return the map
   */
  Map countByValue();

  /**
   * Distinct m stream.
   *
   * @return the m stream
   */
  MStream distinct();

  /**
   * Limit m stream.
   *
   * @param number the number
   * @return the m stream
   */
  MStream limit(long number);

  /**
   * Take list.
   *
   * @param n the n
   * @return the list
   */
  List take(int n);

  /**
   * Skip m stream.
   *
   * @param n the n
   * @return the m stream
   */
  MStream skip(long n);

  /**
   * On close.
   *
   * @param closeHandler the close handler
   */
  void onClose(Runnable closeHandler);

  /**
   * Max optional.
   *
   * @return the optional
   */
  default Optional max() {
    return max(Cast.as(Ordering.natural()));
  }

  /**
   * Min optional.
   *
   * @return the optional
   */
  default Optional min() {
    return min(Cast.as(Ordering.natural()));
  }

  /**
   * Sorted m stream.
   *
   * @param ascending the ascending
   * @return the m stream
   */
  MStream sorted(boolean ascending);

  /**
   * Max optional.
   *
   * @param comparator the comparator
   * @return the optional
   */
  Optional max(Comparator comparator);

  /**
   * Min optional.
   *
   * @param comparator the comparator
   * @return the optional
   */
  Optional min(Comparator comparator);

  /**
   * Zip m pair stream.
   *
   * @param    the type parameter
   * @param other the other
   * @return the m pair stream
   */
   MPairStream zip(MStream other);

  /**
   * Zip with index m pair stream.
   *
   * @return the m pair stream
   */
  MPairStream zipWithIndex();

  /**
   * Map to double m double stream.
   *
   * @param function the function
   * @return the m double stream
   */
  MDoubleStream mapToDouble(ToDoubleFunction function);

  /**
   * Cache m stream.
   *
   * @return the m stream
   */
  MStream cache();

  /**
   * Union m stream.
   *
   * @param other the other
   * @return the m stream
   */
  MStream union(MStream other);

  /**
   * Save as text file.
   *
   * @param location the location
   */
  void saveAsTextFile(Resource location);

  /**
   * Save as text file.
   *
   * @param location the location
   */
  default void saveAsTextFile(@NonNull String location) {
    saveAsTextFile(Resources.from(location));
  }

  /**
   * Parallel m stream.
   *
   * @return the m stream
   */
  MStream parallel();

  /**
   * Shuffle m stream.
   *
   * @return the m stream
   */
  default MStream shuffle() {
    return shuffle(new Random());
  }

  MStream shuffle(Random random);


}//END OF MStream