Created
May 13, 2020 02:34
-
-
Save yevgnenll/84631964512724093af2c516aaf858bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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