Skip to content

Instantly share code, notes, and snippets.

@trishagee
Created November 10, 2014 10:03
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 trishagee/a3e4a54329e94ada2fe2 to your computer and use it in GitHub Desktop.
Save trishagee/a3e4a54329e94ada2fe2 to your computer and use it in GitHub Desktop.
package com.mechanitis.talks.stayingahead;
import com.mongodb.DBCollection;
import org.mongojack.JacksonDBCollection;
import org.mongojack.WriteResult;
import javax.ws.rs.core.Response;
import java.net.URI;
public class BadLambdas {
private final DBCollection database;
public BadLambdas(final DBCollection database) {
this.database = database;
}
public Response saveOrder(final Order order) {
DBCollection orders = database.getCollection("orders");
JacksonDBCollection<Order, String> collection = JacksonDBCollection.wrap(orders, Order.class, String.class);
return save(order, collection,
() -> Response.created(URI.create(order.getId())).entity(order).build(),
() -> Response.serverError().build());
}
private Response save(final Order order,
final JacksonDBCollection<Order, String> collection,
final SuccessHandler successHandler,
final ErrorHandler errorHandler) {
WriteResult<Order, String> writeResult = collection.save(order);
if (writeResult == null) {
return errorHandler.onError();
} else {
order.setId(writeResult.getSavedId());
return successHandler.onSuccess();
}
}
@FunctionalInterface
private interface SuccessHandler {
Response onSuccess();
}
@FunctionalInterface
private interface ErrorHandler {
Response onError();
}
}
@mwanji
Copy link

mwanji commented Nov 11, 2014

Do you have the "Lambdas the right way" code anywhere? In curious to see it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment