forked from plusone/plusone-commons
修改类名,调整包结构。
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
|
||||
/**
|
||||
* 带校验的字符串值对象
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract class ValidatableStringRecord
|
||||
implements Comparable<ValidatableStringRecord>, Serializable {
|
||||
private final String value;
|
||||
|
||||
protected ValidatableStringRecord(String value, Pattern pattern) {
|
||||
Preconditions.checkNotNull(pattern, "The pattern must not be null.");
|
||||
Preconditions.checkNotNull(value, "The value must not be null.");
|
||||
Preconditions.checkArgument(RegexTools.matches(value, pattern));
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 值对象的字符串值。
|
||||
*
|
||||
* @return 字符串(不为空)
|
||||
*/
|
||||
public final String value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(ValidatableStringRecord o) {
|
||||
return this.value.compareTo(o.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
ValidatableStringRecord other = (ValidatableStringRecord) obj;
|
||||
return Objects.equals(value, other.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value();
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -8365241662025469652L;
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
|
||||
/**
|
||||
* 返回分页查询的结果
|
||||
*
|
||||
* @param <T> 内容列表的元素类型
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @see PagingAndSortingQueryParams
|
||||
*/
|
||||
public class PageResult<T> {
|
||||
|
||||
private final long total;
|
||||
|
||||
private final List<T> content;
|
||||
|
||||
private PageResult(List<T> content, long total) {
|
||||
Preconditions.checkNotNull(content, "Content must not be null.");
|
||||
this.content = content;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
@StaticFactoryMethod(PageResult.class)
|
||||
public static <T> PageResult<T> of(List<T> content, long total) {
|
||||
return new PageResult<>(content, total);
|
||||
}
|
||||
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public List<T> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PageDTO [total=" + total + ", content=" + content + "]";
|
||||
}
|
||||
}
|
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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 xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* 统一结果,对返回给前端的数据进行封装。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
public abstract class UnifiedResponse {
|
||||
|
||||
private Object status;
|
||||
private String message;
|
||||
|
||||
private @Nullable Object data;
|
||||
|
||||
public static UnifiedResponse success() {
|
||||
return new SuccessResult();
|
||||
}
|
||||
|
||||
public static UnifiedResponse success(@Nullable String message) {
|
||||
return new SuccessResult(message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse success(@Nullable String message, @Nullable Object data) {
|
||||
return new SuccessResult(message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(@Nullable String message) {
|
||||
return new ErrorResult(message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(@Nullable String message, @Nullable Object data) {
|
||||
return new ErrorResult(message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(Object status, @Nullable String message) {
|
||||
return new ErrorResult(status, message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(Object status, @Nullable String message, @Nullable Object data) {
|
||||
return new ErrorResult(status, message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse error(Object status, Throwable e) {
|
||||
return new ErrorResult(status, e);
|
||||
}
|
||||
|
||||
public static <E extends Throwable & IWithCode<?>> UnifiedResponse error(E e) {
|
||||
return new ErrorResult(e);
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(Object status, @Nullable String message) {
|
||||
return new CustomResult(status, message);
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(Object status, @Nullable String message, @Nullable Object data) {
|
||||
return new CustomResult(status, message, data);
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(final boolean isSuccess,
|
||||
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
|
||||
Preconditions.checkNotNull(successResult, "Success supplier must not be null.");
|
||||
Preconditions.checkNotNull(errorResult, "Error supplier must not be null.");
|
||||
return isSuccess ? successResult.get() : errorResult.get();
|
||||
}
|
||||
|
||||
public static UnifiedResponse of(final BooleanSupplier isSuccess,
|
||||
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
|
||||
Preconditions.checkNotNull(isSuccess, "Conditions for success must not be null.");
|
||||
Preconditions.checkNotNull(successResult, "Success supplier must not be null.");
|
||||
Preconditions.checkNotNull(errorResult, "Error supplier must not be null.");
|
||||
return isSuccess.getAsBoolean() ? successResult.get() : errorResult.get();
|
||||
}
|
||||
|
||||
protected UnifiedResponse(Object status, @Nullable String message) {
|
||||
setStatus(status);
|
||||
setMessage(message);
|
||||
}
|
||||
|
||||
protected UnifiedResponse(Object status, @Nullable String message, @Nullable Object data) {
|
||||
setStatus(status);
|
||||
setMessage(message);
|
||||
setData(data);
|
||||
}
|
||||
|
||||
private void setStatus(Object status) {
|
||||
this.status = Objects.requireNonNull(status);
|
||||
}
|
||||
|
||||
private void setMessage(@Nullable String message) {
|
||||
this.message = message == null ? "" : message;
|
||||
}
|
||||
|
||||
private void setData(@Nullable Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
// Constructors end
|
||||
|
||||
// Getters
|
||||
|
||||
public Object getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
// Getters end
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("{status: %s, message: \"%s\", data: %s}",
|
||||
transValue(this.status), this.message, transValue(this.data));
|
||||
}
|
||||
|
||||
private static String transValue(Object value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return "\"" + value + "\"";
|
||||
}
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
protected static class SuccessResult extends UnifiedResponse {
|
||||
public static final String SUCCESS_STATUS = "2000000";
|
||||
|
||||
private static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
|
||||
|
||||
SuccessResult() {
|
||||
super(SUCCESS_STATUS, DEFAULT_SUCCESS_MSG);
|
||||
}
|
||||
|
||||
SuccessResult(@Nullable String message) {
|
||||
super(SUCCESS_STATUS, message);
|
||||
}
|
||||
|
||||
SuccessResult(@Nullable String message, @Nullable Object data) {
|
||||
super(SUCCESS_STATUS, message, data);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class ErrorResult extends UnifiedResponse {
|
||||
public static final String DEFAULT_ERROR_STATUS = "9999999";
|
||||
|
||||
ErrorResult(@Nullable String message) {
|
||||
super(DEFAULT_ERROR_STATUS, message);
|
||||
}
|
||||
|
||||
ErrorResult(@Nullable String message, @Nullable Object data) {
|
||||
super(DEFAULT_ERROR_STATUS, message, data);
|
||||
}
|
||||
|
||||
ErrorResult(Object status, @Nullable String message) {
|
||||
super(status, message);
|
||||
}
|
||||
|
||||
ErrorResult(Object status, @Nullable String message, @Nullable Object data) {
|
||||
super(status, message, data);
|
||||
}
|
||||
|
||||
ErrorResult(Object status, Throwable e) {
|
||||
super(status, Objects.requireNonNull(e).getMessage());
|
||||
}
|
||||
|
||||
<E extends Throwable & IWithCode<?>> ErrorResult(E e) {
|
||||
super(e.getCode(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义结果
|
||||
*
|
||||
* @author zhouxy
|
||||
*/
|
||||
protected static class CustomResult extends UnifiedResponse {
|
||||
|
||||
CustomResult(Object status, @Nullable String message) {
|
||||
super(status, message);
|
||||
}
|
||||
|
||||
CustomResult(Object status, @Nullable String message, @Nullable Object data) {
|
||||
super(status, message, data);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user