Spring #8

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 값으로 return 해주는 방법 -> 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

좋은 웹페이지 즐겨찾기