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

org.pitest.util.InputStreamLineIterable Maven / Gradle / Ivy

/*
 * Copyright 2010 Henry Coles
 * 
 * Licensed 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 org.pitest.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.Iterator;

import org.pitest.functional.F;
import org.pitest.functional.FCollection;
import org.pitest.functional.FunctionalIterable;
import org.pitest.functional.FunctionalList;
import org.pitest.functional.SideEffect1;

public class InputStreamLineIterable implements FunctionalIterable {

  private final BufferedReader reader;
  private String               next;

  public InputStreamLineIterable(final Reader reader) {
    this.reader = new BufferedReader(reader);
    advance();
  }

  public InputStreamLineIterable(final InputStream is) {
    this(new InputStreamReader(is));
  }

  private void advance() {
    try {
      this.next = this.reader.readLine();
    } catch (final IOException e) {
      this.next = null;
    }
  }

  public String next() {
    final String t = this.next;
    advance();
    return t;
  }

  public Iterator iterator() {
    return new Iterator() {

      public boolean hasNext() {
        return InputStreamLineIterable.this.next != null;
      }

      public String next() {
        return InputStreamLineIterable.this.next();
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }

    };
  }

  public FunctionalList filter(final F predicate) {
    return FCollection.filter(this, predicate);
  }

  public void forEach(final SideEffect1 e) {
    FCollection.forEach(this, e);

  }

  public  FunctionalList map(final F f) {
    return FCollection.map(this, f);
  }

  public  void mapTo(final F f, final Collection bs) {
    FCollection.mapTo(this, f, bs);
  }

  public  FunctionalList flatMap(final F> f) {

    return FCollection.flatMap(this, f);
  }

  public boolean contains(final F predicate) {
    return FCollection.contains(this, predicate);
  }

}