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

com.github.gimmi.AnyListBuilder Maven / Gradle / Ivy

Go to download

An immutable data structure that provide an uniform interface to map, list or scalar data

There is a newer version: 0.4.0
Show newest version
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)));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy