package org.ehcache.clustered.common.internal.util;
import org.ehcache.clustered.common.internal.store.Chain;
import org.ehcache.clustered.common.internal.store.Element;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ChainBuilder {
private List<ByteBuffer> buffers = new ArrayList<>();
public ChainBuilder add(final ByteBuffer payload) {
buffers.add(payload);
return this;
}
public Chain build() {
List<Element> elements = new ArrayList<>();
for (final ByteBuffer buffer : buffers) {
elements.add(buffer::asReadOnlyBuffer);
}
return chainFromList(elements);
}
public static Chain chainFromList(List<Element> elements) {
return new Chain() {
@Override
public boolean isEmpty() {
return elements.isEmpty();
}
@Override
public int length() {
return elements.size();
}
@Override
public Iterator<Element> iterator() {
return elements.iterator();
}
};
}
}