How to remove duplicates in Java 8 using field or property ? We might use reduce!
List<MyClass> distinctByField = myList.stream().reduce(new ArrayList<>(), (List<MyClass> accumulator, MyClass myClass) -> { if (accumulator.stream().noneMatch(item -> item.getField().equals(myClass.getField()))) { accumulator.add(MyClass); } return accumulator; }, (acc1, acc2) -> { acc1.addAll(acc2); return acc1; });
Recently I was creating small distributed application called ClientBackend just for interview purpose. Solution consists of thee microservices :
- Client (received request from API Gateway and sends it to Backend)
- Backend (performs operation defined in request)
- Discovery (Spring Eureka)
- API Gateway (Spring Zuul)
- Java 8
- Spring Boot
- Hystrix
- Eureka
- Feign
- Zuul
- TypeOf
Sometimes asserting custom exception is difficult. Sometimes it's not, for instance when you are using AssertJ, it's very quite easy to achieve. In this post I would like to highlight how to achieve that using Hamcrest matcher.
Below you might find a BaseExceptionMatcher which can be easily extended for our needs. Let's assume that we have an exception with four properties like errorType, errorCode, status and description.
import com.my.package.ErrorCode; import com.my.package.ErrorType; import com.my.package.base.BaseException; import com.my.package.MessageType; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; import javax.ws.rs.core.Response; public abstract class BaseExceptionMatcher extends TypeSafeMatcher<BaseException> { protected ErrorType expectedErrorType; protected ErrorCode expectedErrorCode; protected Response.Status expectedStatus; protected String expectedDescription; public BaseExceptionMatcher(ErrorCode expectedErrorCode, Response.Status expectedStatus, ErrorType expectedErrorType, String expectedDescription) { this.expectedErrorCode = expectedErrorCode; this.expectedStatus = expectedStatus; this.expectedErrorType = expectedErrorType; this.expectedDescription = expectedDescription; } @Override protected boolean matchesSafely(BaseException item) { return item.getStatus().equals(expectedStatus) && item.getError().stream().allMatch(p -> p.getType().equals(expectedErrorType.name()) && p.getCode().equals(expectedErrorCode.name())) && item.getError().stream().allMatch(p -> p.getDescription().contains(expectedDescription)); } @Override public void describeTo(Description description) { description.appendText("Response status : ") .appendValue(expectedStatus.getReasonPhrase() + " (" + expectedStatus.getStatusCode() + ")") .appendText(" | ErrorType : ").appendValue(expectedErrorType.name()) .appendText(" | ErrorCode : ").appendValue(expectedErrorCode.name()) .appendText(" | Description : ").appendValue(expectedDescription); } @Override protected void describeMismatchSafely(BaseException item, Description mismatchDescription) { mismatchDescription.appendText("Exception contains response status : ") .appendValue(item.getStatus().getReasonPhrase() + "(" + item.getStatus().getStatusCode() + ")") .appendText(" | ErrorType : ").appendValue(getMessageType(item).getType()) .appendText(" | ErrorCode : ").appendValue(getMessageType(item).getCode()) .appendText(" | Description : ").appendValue(getMessageType(item).getDescription()); } private MessageType getMessageType(BaseException item) { return item.getError().stream().findFirst().orElseGet(() -> { MessageType messageType = new MessageType(); messageType.setType(ErrorType.APPLICATION.name()); messageType.setCode(ErrorCode.INTERNAL_SERVER_ERROR.name()); messageType.setDescription("Matcher Error"); return messageType; }); } }
import my.package.ErrorCode; import my.package.ErrorType; import my.package.BaseExceptionMatcher; import javax.ws.rs.core.Response; public class MyExceptionMatcher extends BaseExceptionMatcher { public MyExceptionMatcher(ErrorType expectedErrorType, ErrorCode expectedErrorCode, Response.Status expectedStatus, String expectedDescription) { super(expectedErrorCode, expectedStatus, expectedErrorType, expectedDescription); } }
@Rule public ExpectedException expectedException = ExpectedException.none();
public void testFailure() throws MyException { expectedException.expect(new MyExceptionMatcher(ErrorType.APPLICATION, ErrorCode.DOCUMENT_NOT_FOUND, Response.Status.NOT_FOUND, "Document not found")); //do smth to fail }
Last time I was describing how to enable Tomcat for projects maintained by Gradle in post with title called "IntelliJ with Tomcat and Gradle using Gretty plugin". The thing that I found recently even a better way to achieve that! Moreover this way is less invasive in terms of that you do not need to append any configuration to your gradle scripts! This better way is known as a Smart Tomcat! It is a plugin for IntelliJ which allow you to configure literally everything around your app if it should be run on Tomcat.
Smart Tomcat - IntelliJ IDEA Plugins
Last time I was looking for a way to convert a Map into a Map of List. In otherwise direction, I mean backward from value up to key. Below you might find an solution for that problem using of course main Java 8 components like stream, filter, grouping and mapping.
Map<String, String> input = new HashMap<>(); input.put("SHARK", null); input.put("DOG", "HOME"); input.put("CAT", "HOME"); input.put("TURTLE", "HOME"); input.put("RABBIT", "HOME"); input.put("LION", "NOT_HOME"); input.put("HIPPO", "NOT_HOME"); input.put("TIGER", "NOT_HOME"); input.put("ZEBRA", "NOT_HOME"); Map<String, List<String>> result = input.entrySet().stream() .filter(i -> i.getValue() != null) .collect(Collectors.groupingBy(i -> i.getValue(), Collectors.mapping(i -> i.getKey(), Collectors.toList())));
Results
Key : HOME with List of DOG, CAT, TURTLE and RABBIT
Key : NOT_HOME with List of LION, HIPPO, TIGER, ZEBRA
