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

opennlp.tools.util.ObjectStreamUtils Maven / Gradle / Ivy

The newest version!
/*
 * 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 opennlp.tools.util;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

public class ObjectStreamUtils {

  /**
   * Creates an {@link ObjectStream} form an array.
   *
   * @param 
   * @param array
   *
   * @return the object stream over the array elements
   */
  @SafeVarargs
  public static  ObjectStream createObjectStream(final T... array) {

    return new ObjectStream() {

      private int index = 0;

      public T read() {
        if (index < array.length)
          return array[index++];
        else
          return null;
      }

      public void reset() {
        index = 0;
      }

      public void close() {
      }
    };
  }

  /**
   * Creates an {@link ObjectStream} form a collection.
   *
   * @param 
   * @param collection
   *
   * @return the object stream over the collection elements
   */
  public static  ObjectStream createObjectStream(final Collection collection) {

    return new ObjectStream() {

      private Iterator iterator = collection.iterator();

      public T read() {
        if (iterator.hasNext())
          return iterator.next();
        else
          return null;
      }

      public void reset() {
        iterator = collection.iterator();
      }

      public void close() {
      }
    };
  }

  /**
   * Creates a single concatenated ObjectStream from multiple individual
   * ObjectStreams with the same type.
   *
   * @param streams
   * @return
   */
  public static  ObjectStream concatenateObjectStream(final Collection> streams) {

    // We may want to skip null streams instead of throwing a 
    for (ObjectStream stream : streams) {
      if (stream == null) {
        throw new NullPointerException("stream cannot be null");
      }
    }

    return new ObjectStream() {

      private Iterator> iterator = streams.iterator();
      private ObjectStream currentStream = iterator.next();

      @Override
      public T read() throws IOException {
        T object = null;

        while (currentStream != null && object == null) {
          object = currentStream.read();
          if (object == null) {
            currentStream = (iterator.hasNext()) ? iterator.next() : null;
          }
        }
        return object;
      }

      @Override
      public void reset() throws IOException, UnsupportedOperationException {
        for (ObjectStream stream : streams) {
          stream.reset();
        }
        iterator = streams.iterator();
      }

      @Override
      public void close() throws IOException {
        for (ObjectStream stream : streams) {
          stream.close();
        }
      }

    };

  }

  /**
   * Creates a single concatenated ObjectStream from multiple individual
   * ObjectStreams with the same type.
   *
   * @param streams
   * @return
   */
  @SafeVarargs
  public static  ObjectStream concatenateObjectStream(final ObjectStream... streams) {

    for (ObjectStream stream : streams) {
      if (stream == null) {
        throw new NullPointerException("stream cannot be null");
      }
    }

    return new ObjectStream() {

      private int streamIndex = 0;

      public T read() throws IOException {

        T object = null;

        while (streamIndex < streams.length && object == null) {
          object = streams[streamIndex].read();

          if (object == null)
            streamIndex++;
        }

        return object;
      }

      public void reset() throws IOException, UnsupportedOperationException {
        streamIndex = 0;

        for (ObjectStream stream : streams) {
          stream.reset();
        }
      }

      public void close() throws IOException {

        for (ObjectStream stream : streams) {
          stream.close();
        }
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy