class CollectionFunctions {

    public static <T> Optional<T> getOnlyElementOrEmpty(List<T> list) {
        if (list == null || list.isEmpty()) {
            return empty();
        }

        if (list.size() > 1) {
            throw new IllegalArgumentException("More than one element was available in list " + list + ", so cannot return only element");
        }

        return Optional.of(list.get(0));
    }

    private  CollectionFunctions();

}