Spring #8

12397 ワード

ApiMessage




ApiMessage.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ApiMessage<T>{
    private int code;
    private String message;
    private T data;

    public ApiMessage(Status status) {
        this.code = status.getCode();
        this.message = status.getMessage();
    }

    public static <T> ApiMessage<T> putDataInMessage(ApiMessage apiMessage,T data){
        return (ApiMessage<T>) ApiMessage.builder()
                .message(apiMessage.message)
                .code(apiMessage.code)
                .data(data)
                .build();
    }

    public static ApiMessage getSuccessMessage(){
        return new ApiMessage(Status.SUCCESS);
    }

    public static ApiMessage getFailMessage(){
        return new ApiMessage(Status.FAIL);
    }

    public static ApiMessage getUnAuthorizationMessage(){
        return new ApiMessage(Status.UN_AUTHORIZATION);
    }

    public static ApiMessage getArtistDoesNotExistError(){
        return new ApiMessage(Status.ARTIST_DOES_NOT_EXIST);
    }

    @Getter
    public enum Status{
        SUCCESS(200, "Success"),
        UN_AUTHORIZATION(403, "UnAuthorization")
        , FAIL(403, "Fail")
        , ARTIST_DOES_NOT_EXIST(403,"Artist is not exist");

        private int code;
        private String message;

        Status(int code, String message){
            this.code = code;
            this.message = message;
        }
    }
}

Key,Value値を返す方法->Mapでよい.

Collections.sort

Collections.sort(list, (o1, o2) -> {
    if(o1.getViewCount()<o2.getViewCount()){
        return 1;
    }else if(o1.getViewCount()>o2.getViewCount()){
        return -1;
    }
    return 0;
});
後の大きな値に基づいて1->DESCを返します.
前の値より大きい場合は1->ASCを返します.