Skip to content

Instantly share code, notes, and snippets.

@yevgnenll
Created May 13, 2020 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yevgnenll/84631964512724093af2c516aaf858bc to your computer and use it in GitHub Desktop.
Save yevgnenll/84631964512724093af2c516aaf858bc to your computer and use it in GitHub Desktop.
package com.geo.spatial.manager;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;
public final class CollectionHelper {
private Collection<?> collection;
private boolean isEmpty = true;
private CollectionHelper() {
new AssertionError("no");
}
private void validate() {
if (Objects.isNull(collection)) {
new AssertionError("call check first");
}
}
@SuppressWarnings("unchecked")
private CollectionHelper(Collection<?> collection) {
Objects.requireNonNull(collection);
this.collection = collection;
}
@SuppressWarnings("unchecked")
public static CollectionHelper check(Collection<?> collection) {
return new CollectionHelper(collection);
}
@SuppressWarnings("unchecked")
public CollectionHelper isEmpty() {
validate();
isEmpty = ((Collection)collection).isEmpty();
return this;
}
@SuppressWarnings("unchecked")
public CollectionHelper thenFetch(Collection<?> collection) {
validate();
if (isEmpty && !collection.isEmpty()) {
this.collection = collection;
isEmpty = collection.isEmpty();
}
return this;
}
@SuppressWarnings("unchecked")
public CollectionHelper thenFetch(Supplier<Collection<?>> function) {
if (!isEmpty)
return this;
Collection<?> result = function.get();
return thenFetch(result);
}
public Collection<?> get() {
validate();
return collection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment