Skip to content

Instantly share code, notes, and snippets.

@timjstewart
Created March 31, 2015 16:29
Show Gist options
  • Select an option

  • Save timjstewart/dfa088217b3a797ee7d8 to your computer and use it in GitHub Desktop.

Select an option

Save timjstewart/dfa088217b3a797ee7d8 to your computer and use it in GitHub Desktop.
One potential implementation
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
/**
* An ad hoc Element class (to be replaced with real Element class).
*/
class Element {
private String key;
private Object value;
public Element(
final String key,
final Object value
) {
this.key = Objects.requireNonNull(key);
this.value = Objects.requireNonNull(value);
}
@Override
public String toString() {
return String.format("Element [key=%s, value=%s]", key, value);
}
}
/**
* An ad hoc Vertex class (to be replaced with real Vertex class).
*/
class Vertex extends Element {
public Vertex(final String key, final Object value) {
super(key, value);
}
}
/**
* The base class for objects returned by ensureVertex.
*/
class Result<T extends Element> {
// The element that was found or created.
private final T element;
// true iff the element was created.
private final boolean wasCreated;
public Result(final T element, final boolean wasCreated) {
this.element = Objects.requireNonNull(element);
this.wasCreated = wasCreated;
}
public T getElement() {
return element;
}
public boolean wasFound() {
return !wasCreated;
}
public boolean wasCreated() {
return wasCreated;
}
public T ifCreated(Consumer<T> func) {
if (wasCreated) {
func.accept(element);
}
return element;
}
public T ifFound(Consumer<T> func) {
if (!wasCreated) {
func.accept(element);
}
return element;
}
}
/**
* An ad hoc Graph class that provides a ensureVertex method.
*/
class Graph {
private final Map<String, Vertex> vs = new HashMap<>();
Result<Vertex> ensureVertex(final String key, final Object value) {
if (vs.containsKey(key)) {
final Vertex el = vs.get(key);
return new Result(el, true);
} else {
final Vertex v = new Vertex(key, value);
vs.put(key, v);
return new Result(v, false);
}
}
}
public class Main {
public static void main(String[] args) {
final Graph g = new Graph();
// If you don't care if the element was created, but you just want the element.
final Vertex v1 = g.ensureVertex("unique", "the value").getElement();
// Example of when the vertex is created. The lambda function is called.
final Vertex v2 = g.ensureVertex("key", "value").ifCreated(x -> {
System.out.println(String.format("Created!: %s", x));
});
// Example of when the vertex is not created. The lambda function is not called.
final Vertex v3 = g.ensureVertex("key", "value").ifCreated(x -> {
// Won't print.
System.out.println(String.format("Created Again!: %s", x));
});
// Example of when the vertex is found. The lambda function is called.
final Vertex v4 = g.ensureVertex("key", "value").ifFound(x -> {
System.out.println(String.format("Should not be Found!: %s", x));
});
// Example of when the vertex is not found. The lambda function is not called.
final Vertex v5 = g.ensureVertex("key2", "value").ifFound(x -> {
// Won't print.
System.out.println(String.format("Found!: %s", x));
});
// Example of when the vertex is not found. The lambda function is not called.
final Result<Vertex> res1 = g.ensureVertex("key2", "value");
// If you want to to do two different things based on whether or not it was created...
if (res1.wasCreated()) {
System.out.println(String.format("Created!: %s", res1.getElement()));
} else {
System.out.println(String.format("Not Created!: %s", res1.getElement()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment