新增 base 包的 Javadoc;优化 base 包中 JSR-305 注解的使用。

This commit is contained in:
zhouxy108 2025-01-07 16:21:24 +08:00
parent cda624c528
commit 8813923c86
4 changed files with 90 additions and 10 deletions

View File

@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.commons.base;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* 规定实现类带有 {@code getCode} 方法
@ -31,19 +32,19 @@ public interface IWithCode<T> {
@Nonnull
T getCode();
default boolean isCodeEquals(T code) {
default boolean isCodeEquals(@Nullable T code) {
return Objects.equals(getCode(), code);
}
default boolean isSameCodeAs(IWithCode<?> other) {
default boolean isSameCodeAs(@Nullable IWithCode<?> other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean isSameCodeAs(IWithIntCode other) {
default boolean isSameCodeAs(@Nullable IWithIntCode other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean isSameCodeAs(IWithLongCode other) {
default boolean isSameCodeAs(@Nullable IWithLongCode other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
}

View File

@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.base;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* 规定实现类带有 {@code getCode} 方法
* 用于像自定义异常等需要带有 {@code code} 字段的类
@ -32,15 +34,15 @@ public interface IWithIntCode {
return getCode() == code;
}
default boolean isSameCodeAs(IWithCode<?> other) {
default boolean isSameCodeAs(@Nullable IWithCode<?> other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean isSameCodeAs(IWithIntCode other) {
default boolean isSameCodeAs(@Nullable IWithIntCode other) {
return other != null && getCode() == other.getCode();
}
default boolean isSameCodeAs(IWithLongCode other) {
default boolean isSameCodeAs(@Nullable IWithLongCode other) {
return other != null && getCode() == other.getCode();
}
}

View File

@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.base;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* 规定实现类带有 {@code getCode} 方法
* 用于像自定义异常等需要带有 {@code code} 字段的类
@ -32,15 +34,15 @@ public interface IWithLongCode {
return getCode() == code;
}
default boolean isSameCodeAs(IWithCode<?> other) {
default boolean isSameCodeAs(@Nullable IWithCode<?> other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean isSameCodeAs(IWithIntCode other) {
default boolean isSameCodeAs(@Nullable IWithIntCode other) {
return other != null && getCode() == other.getCode();
}
default boolean isSameCodeAs(IWithLongCode other) {
default boolean isSameCodeAs(@Nullable IWithLongCode other) {
return other != null && getCode() == other.getCode();
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright 2025 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.
*/
/**
* 基础组件
*
* <h2>Ref</h2>
* <p>
* {@link Ref} 包装了一个值表示对该值的应用
* </p>
* <p>灵感来自于 C# {@value ref} 参数修饰符C# 允许通过以下方式将值返回给调用端</p>
* <pre>
* void Method(ref int refArgument)
* {
* refArgument = refArgument + 44;
* }
*
* int number = 1;
* Method(ref number);
* Console.WriteLine(number); // Output: 45
* </pre>
* {@link Ref} 使 Java 可以达到类似的效果
* <pre>
* void method(Ref&lt;Integer&gt; refArgument) {
* refArgument.transformValue(i -&gt; i + 44);
* }
*
* Ref&lt;Integer&gt; number = Ref.of(1);
* method(number);
* System.out.println(number.getValue()); // Output: 45
* </pre>
* <p>
* 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值
* 调用方在调用方法之后使用 {@code getValue()} 获取结果
* </p>
* <pre>
* String method(Ref&lt;Integer&gt; intRefArgument, Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44);
* strRefArgument.setValue("Hello " + strRefArgument.getValue());
* return "Return string";
* }
*
* Ref&lt;Integer&gt; number = Ref.of(1);
* Ref&lt;String&gt; str = Ref.of("Java");
* String result = method(number, str);
* System.out.println(number.getValue()); // Output: 45
* System.out.println(str.getValue()); // Output: Hello Java
* System.out.println(result); // Output: Return string
* </pre>
*
* <h2>IWithCode</h2>
* <p>
* 类似于枚举之类的类通常需要设置固定的码值表示对应的含义
* 可实现 {@link IWithCode}{@link IWithIntCode}{@link IWithLongCode}便于在需要的地方对这些接口的实现进行处理
* </p>
*/
@CheckReturnValue
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.base;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.CheckReturnValue;