com.enterprisemath.math.statistics.observation.ListObservationProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of em-math Show documentation
Show all versions of em-math Show documentation
Advanced mathematical algorithms.
The newest version!
package com.enterprisemath.math.statistics.observation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.enterprisemath.utils.DomainUtils;
/**
* Provides vector observation from the list in the same order as the source list.
* It is guaranteed that all the provided vectors have the specified dimension.
*
* @author radek.hecl
*
* @param type of the observation
*/
public class ListObservationProvider implements ObservationProvider {
/**
* Builder object.
*
* @param type of the observation
*/
public static class Builder {
/**
* Observations.
*/
private List observations = new ArrayList();
/**
* Sets the observations.
*
* @param observations observations
* @return this instance
*/
public Builder setObservations(List observations) {
this.observations = DomainUtils.softCopyList(observations);
return this;
}
/**
* Adds observation into the sequence.
*
* @param observation observation
* @return this instance
*/
public Builder addObservation(T observation) {
observations.add(observation);
return this;
}
/**
* Builds result object.
*
* @return created object
*/
public ListObservationProvider build() {
return new ListObservationProvider(this);
}
}
/**
* Observations.
*/
private List observations = new ArrayList();
/**
* Creates new instance.
*
* @param builder builder object
*/
public ListObservationProvider(Builder builder) {
observations = Collections.unmodifiableList(DomainUtils.softCopyList(builder.observations));
guardInvariants();
}
/**
* Guards this object to be consistent. Throws exception if this is not the case.
*/
private void guardInvariants() {
}
@Override
public ObservationIterator getIterator() {
return new Iterator(observations);
}
/**
* Observation iterator.
*
* @param type of observation
*/
private static class Iterator implements ObservationIterator {
/**
* Iterated observations.
*/
private List observations;
/**
* Current index.
*/
private AtomicInteger index = new AtomicInteger(0);
/**
* Creates new instance.
*
* @param observations observations
*/
public Iterator(List observations) {
this.observations = observations;
}
@Override
public boolean isNextAvailable() {
return index.get() < observations.size();
}
@Override
public synchronized T getNext() {
if (index.get() >= observations.size()) {
throw new NoSuchElementException("next observation is not available");
}
return observations.get(index.getAndIncrement());
}
@Override
public long getNumIterated() {
return index.get();
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
/**
* Creates provider from the list.
*
* @param type of the observation
* @param observations observations
* @return created provider
*/
public static ListObservationProvider create(List observations) {
return new ListObservationProvider.Builder().
setObservations(observations).
build();
}
/**
* Creates cached observation provider from the specified source observation provider.
* All observations will be hold in the memory.
*
* @param type of the observation
* @param source source observation provider
* @return created provider
*/
public static ListObservationProvider createCached(ObservationProvider source) {
List list = new ArrayList();
ObservationIterator iterator = source.getIterator();
while (iterator.isNextAvailable()) {
list.add(iterator.getNext());
}
return new ListObservationProvider.Builder().
setObservations(list).
build();
}
}