Created
November 10, 2014 10:03
-
-
Save trishagee/a3e4a54329e94ada2fe2 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.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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have the "Lambdas the right way" code anywhere? In curious to see it.