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

dk.alexandra.fresco.lib.helper.SingleProtocolProducer Maven / Gradle / Ivy

There is a newer version: 1.3.7
Show newest version
package dk.alexandra.fresco.lib.helper;

import dk.alexandra.fresco.framework.DRes;
import dk.alexandra.fresco.framework.NativeProtocol;
import dk.alexandra.fresco.framework.ProtocolCollection;
import dk.alexandra.fresco.framework.ProtocolProducer;
import dk.alexandra.fresco.framework.sce.resources.ResourcePool;

/**
 * A protocol producer that only produces a single protocol.
 */
public class SingleProtocolProducer implements ProtocolProducer, DRes {

  private NativeProtocol protocol;
  private boolean evaluated = false;
  private T result;

  public SingleProtocolProducer(NativeProtocol protocol) {
    this.protocol = protocol;
  }

  @SuppressWarnings({"unchecked"})
  @Override
  public  void getNextProtocols(
      ProtocolCollection protocolCollection) {
    evaluated = true;
    protocolCollection.addProtocol((NativeProtocol) protocol);
  }

  @Override
  public boolean hasNextProtocols() {
    return !evaluated;
  }

  @Override
  public String toString() {
    return "SingleProtocolProducer{"
        + "protocol=" + protocol
        + '}';
  }

  @Override
  public T out() {
    if (result == null) {
      result = protocol.out();
      // Break chain of native protocols to ensure garbage collection
      protocol = null;
    }
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy