Java tutorial
/* * Copyright 2016 Greg Whitaker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.ignitr.dispatchr.manager.domain.subscription; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import io.ignitr.dispatchr.manager.controller.topic.TopicController; import io.ignitr.dispatchr.manager.domain.internal.Subscription; import org.springframework.hateoas.ResourceSupport; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.TimeZone; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; /** * */ @JsonPropertyOrder({ "results" }) public class FindSubscriptionsResponse extends ResourceSupport { private List<Result> results = new ArrayList<>(); /** * * @param metadatas * @return */ public static FindSubscriptionsResponse from(List<Subscription> metadatas) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); FindSubscriptionsResponse response = new FindSubscriptionsResponse(); metadatas.forEach(metadata -> { Result result = new Result(); result.setSubscriptionId(metadata.getId()); result.setClientId(metadata.getClientId()); result.setTopic(metadata.getTopicName()); result.setTopicArn(metadata.getTopicArn()); result.setQueueArn(metadata.getQueueArn()); result.setCreateDate(sdf.format(new Date(metadata.getCreateDate()))); result.setLastSubscribeDate(sdf.format(new Date(metadata.getLastSubscribeDate()))); result.add(linkTo(methodOn(TopicController.class).findOne(metadata.getTopicName(), null)) .withRel("topic")); response.addResult(result); }); return response; } /** * Adds a result to the list of returned subscriptions. * * @param result the result to add to the list */ public void addResult(Result result) { if (results == null) { results = new ArrayList<>(); } results.add(result); } public List<Result> getResults() { return results; } public void setResults(List<Result> results) { this.results = results; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; FindSubscriptionsResponse that = (FindSubscriptionsResponse) o; return results.equals(that.results); } @Override public int hashCode() { int result = super.hashCode(); result = 31 * result + results.hashCode(); return result; } @JsonPropertyOrder({ "subscriptionId", "clientId", "topic", "topicArn", "queueArn", "createDate", "lastSubscribeDate" }) public static class Result extends ResourceSupport { private String subscriptionId; private String clientId; private String topic; private String topicArn; private String queueArn; private String createDate; private String lastSubscribeDate; @JsonProperty("id") public String getSubscriptionId() { return subscriptionId; } public void setSubscriptionId(String subscriptionId) { this.subscriptionId = subscriptionId; } public String getClientId() { return clientId; } public void setClientId(String clientId) { this.clientId = clientId; } public String getTopic() { return topic; } public void setTopic(String topic) { this.topic = topic; } public String getTopicArn() { return topicArn; } public void setTopicArn(String topicArn) { this.topicArn = topicArn; } public String getQueueArn() { return queueArn; } public void setQueueArn(String queueArn) { this.queueArn = queueArn; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } public String getLastSubscribeDate() { return lastSubscribeDate; } public void setLastSubscribeDate(String lastSubscribeDate) { this.lastSubscribeDate = lastSubscribeDate; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; Result result = (Result) o; if (!subscriptionId.equals(result.subscriptionId)) return false; if (!clientId.equals(result.clientId)) return false; if (!topic.equals(result.topic)) return false; if (!topicArn.equals(result.topicArn)) return false; if (!queueArn.equals(result.queueArn)) return false; if (!createDate.equals(result.createDate)) return false; return lastSubscribeDate.equals(result.lastSubscribeDate); } @Override public int hashCode() { int result = super.hashCode(); result = 31 * result + subscriptionId.hashCode(); result = 31 * result + clientId.hashCode(); result = 31 * result + topic.hashCode(); result = 31 * result + topicArn.hashCode(); result = 31 * result + queueArn.hashCode(); result = 31 * result + createDate.hashCode(); result = 31 * result + lastSubscribeDate.hashCode(); return result; } } }