com.github.gimmi.AnyListBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of any Show documentation
Show all versions of any Show documentation
An immutable data structure that provide an uniform interface to map, list or scalar data
package com.github.gimmi;
import java.util.ArrayList;
public class AnyListBuilder {
private final ArrayList list = new ArrayList<>();
private int lastNonNullIndex = -1;
public AnyListBuilder put(Any value) {
if (value == null) {
value = Any.NULL;
}
if (value.count() > 0) {
lastNonNullIndex = list.size();
}
list.add(value);
return this;
}
public int count() {
return lastNonNullIndex + 1;
}
public Any build() {
if (lastNonNullIndex < 0) {
return Any.NULL;
} else if (lastNonNullIndex == 0) {
return list.get(0);
}
return new Any(null, null, new ArrayList<>(list.subList(0, lastNonNullIndex + 1)));
}
}