io.ignitr.dispatchr.manager.domain.client.FindClientsResponse.java Source code

Java tutorial

Introduction

Here is the source code for io.ignitr.dispatchr.manager.domain.client.FindClientsResponse.java

Source

/*
 * 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.client;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.ignitr.dispatchr.manager.controller.client.ClientKeysController;
import io.ignitr.dispatchr.manager.domain.internal.Client;
import org.springframework.hateoas.ResourceSupport;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@JsonPropertyOrder({ "results" })
public class FindClientsResponse extends ResourceSupport {
    private List<Result> results = new ArrayList<>();

    public static FindClientsResponse from(List<Client> clients) {
        FindClientsResponse response = new FindClientsResponse();

        clients.forEach(client -> {
            Result result = new Result();
            result.setClientId(client.getClientId());
            result.setOwnerName(client.getOwnerName());
            result.setOwnerEmail(client.getOwnerEmail());

            result.add(linkTo(methodOn(ClientKeysController.class).findAll(client.getClientId(), null))
                    .withRel("keys"));

            response.addResult(result);
        });

        return response;
    }

    /**
     * Adds a result to the list of returned clients.
     *
     * @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;

        FindClientsResponse that = (FindClientsResponse) o;

        return results.equals(that.results);

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + results.hashCode();
        return result;
    }

    @JsonPropertyOrder({ "clientId", "ownerName", "ownerEmail" })
    public static class Result extends ResourceSupport {
        private String clientId;
        private String ownerName;
        private String ownerEmail;

        public String getClientId() {
            return clientId;
        }

        public void setClientId(String clientId) {
            this.clientId = clientId;
        }

        public String getOwnerName() {
            return ownerName;
        }

        public void setOwnerName(String ownerName) {
            this.ownerName = ownerName;
        }

        public String getOwnerEmail() {
            return ownerEmail;
        }

        public void setOwnerEmail(String ownerEmail) {
            this.ownerEmail = ownerEmail;
        }

        @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 (!clientId.equals(result.clientId))
                return false;
            if (!ownerName.equals(result.ownerName))
                return false;
            return ownerEmail.equals(result.ownerEmail);

        }

        @Override
        public int hashCode() {
            int result = super.hashCode();
            result = 31 * result + clientId.hashCode();
            result = 31 * result + ownerName.hashCode();
            result = 31 * result + ownerEmail.hashCode();
            return result;
        }
    }
}