package cz.senslog.analytics.domain;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

public final class ListHolder<T> {

    private final List<T> dataList;

    public static <T> ListHolder<T> of(final List<T> dataList) {
        return new ListHolder<>(Objects.requireNonNullElse(dataList, Collections.emptyList()));
    }

    private ListHolder(List<T> dataList) {
        this.dataList = dataList;
    }

    public List<T> list() {
        return this.dataList;
    }

    public boolean isEmpty() {
        return this.dataList.isEmpty();
    }

    public boolean isNotEmpty() {
        return !this.isEmpty();
    }

    public Stream<T> stream() {
        return this.dataList.stream();
    }

    public int size() {
        return this.dataList.size();
    }

    public T first() {
        return this.dataList.get(0);
    }

    public T last() {
        if (dataList.isEmpty()) { return null; }
        return this.dataList.get(this.dataList.size() - 1);
    }
}
