com.linkedin.dagli.objectio.ConstantReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectio-core Show documentation
Show all versions of objectio-core Show documentation
DAG-oriented machine learning framework for bug-resistant, readable, efficient, maintainable and trivially deployable models in Java and other JVM languages
package com.linkedin.dagli.objectio;
import java.util.Arrays;
import java.util.NoSuchElementException;
/**
* A reader whose values are all the same, constant object.
*
* @param the type of the constant value
*/
public class ConstantReader implements ObjectReader {
private final long _count;
private T _obj; // non-final so it can be cleared by close()
/**
* Creates a new reader that will contain the specified value in the specified quantity
*
* @param obj the object that will be read by this reader
* @param count the number of times this object is (logically) present in this reader. This will be the value
* returned by {@link ConstantReader#size64()}, etc.
*/
public ConstantReader(T obj, long count) {
_count = count;
_obj = obj;
}
@Override
public long size64() {
return _count;
}
@Override
public ObjectIterator iterator() {
return new Iterator<>(_obj, _count);
}
@Override
public void close() {
_obj = null;
}
/**
* An iterator that returns a constant object a fixed number of times.
*
* @param the type of object that will be returned
*/
public static class Iterator implements ObjectIterator {
private long _current = 0;
private final long _count;
private T _obj;
/**
* Creates a new instance.
*
* @param obj the constant object value that the iterator will return
* @param count the number of times this iterator (logically) contains this object
*/
public Iterator(T obj, long count) {
_obj = obj;
_count = count;
}
@Override
public long skip(long toSkip) {
toSkip = Math.min(toSkip, _count - _current);
this._current += toSkip;
return toSkip;
}
@Override
public boolean hasNext() {
return _current < _count;
}
@Override
public T next() {
if (_current >= _count) {
throw new NoSuchElementException();
}
_current++;
return _obj;
}
@Override
public int tryNextAvailable(Object[] destination, int offset, int count) {
return next(destination, offset, count);
}
@Override
public int nextAvailable(Object[] destination, int offset, int count) {
return next(destination, offset, count);
}
@Override
public int next(Object[] destination, int offset, int count) {
count = (int) Math.min(count, _count - _current);
_current += count;
Arrays.fill(destination, offset, offset + count, _obj);
return count;
}
@Override
public void close() {
_obj = null;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy