静态工厂方法添加 @StaticFactoryMethod 注解。

dev
ZhouXY108 2023-04-16 00:16:11 +08:00
parent 0f145e383e
commit fe03b6da4d
13 changed files with 42 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
import com.google.common.io.Files; import com.google.common.io.Files;
import lombok.Getter; import lombok.Getter;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.exception.SysException; import xyz.zhouxy.plusone.exception.SysException;
public class FastDFSUtil { public class FastDFSUtil {
@ -149,21 +150,25 @@ public class FastDFSUtil {
} }
@Nonnull @Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull File file) throws IOException { public static FastDFSFile of(@Nonnull File file) throws IOException {
return new FastDFSFile(file, null); return new FastDFSFile(file, null);
} }
@Nonnull @Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull File file, @Nullable String author) throws IOException { public static FastDFSFile of(@Nonnull File file, @Nullable String author) throws IOException {
return new FastDFSFile(file, author); return new FastDFSFile(file, author);
} }
@Nonnull @Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content) { public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content) {
return new FastDFSFile(fileName, content, null); return new FastDFSFile(fileName, content, null);
} }
@Nonnull @Nonnull
@StaticFactoryMethod(FastDFSFile.class)
public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content, @Nullable String author) { public static FastDFSFile of(@Nonnull String fileName, @Nonnull byte[] content, @Nullable String author) {
return new FastDFSFile(fileName, content, author); return new FastDFSFile(fileName, content, author);
} }

View File

@ -6,6 +6,7 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import lombok.ToString; import lombok.ToString;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithVersion; import xyz.zhouxy.plusone.domain.IWithVersion;
import xyz.zhouxy.plusone.exception.UserOperationException; import xyz.zhouxy.plusone.exception.UserOperationException;
@ -164,6 +165,7 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return newInstance; return newInstance;
} }
@StaticFactoryMethod(Account.class)
public static Account register( public static Account register(
Username username, Username username,
Email email, Email email,
@ -193,6 +195,7 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
password, status, accountInfo, roleRefs, createdBy, updatedBy, version); password, status, accountInfo, roleRefs, createdBy, updatedBy, version);
} }
@StaticFactoryMethod(Account.class)
public static Account newInstance( public static Account newInstance(
String username, String username,
String email, String email,
@ -210,6 +213,7 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return newInstance; return newInstance;
} }
@StaticFactoryMethod(Account.class)
public static Account register( public static Account register(
String username, String username,
String email, String email,

View File

@ -4,6 +4,7 @@ import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import cn.hutool.core.util.DesensitizedUtil; import cn.hutool.core.util.DesensitizedUtil;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/** /**
@ -26,10 +27,12 @@ public class Email extends Principal {
} }
} }
@StaticFactoryMethod(Email.class)
public static Email of(String email) { public static Email of(String email) {
return new Email(email); return new Email(email);
} }
@StaticFactoryMethod(Email.class)
public static Email ofNullable(String email) { public static Email ofNullable(String email) {
return Objects.nonNull(email) ? new Email(email) : null; return Objects.nonNull(email) ? new Email(email) : null;
} }

View File

@ -4,6 +4,7 @@ import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import cn.hutool.core.util.DesensitizedUtil; import cn.hutool.core.util.DesensitizedUtil;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/** /**
@ -26,10 +27,12 @@ public class MobilePhone extends Principal {
} }
} }
@StaticFactoryMethod(MobilePhone.class)
public static MobilePhone of(String mobilePhone) { public static MobilePhone of(String mobilePhone) {
return new MobilePhone(mobilePhone); return new MobilePhone(mobilePhone);
} }
@StaticFactoryMethod(MobilePhone.class)
public static MobilePhone ofNullable(String mobilePhone) { public static MobilePhone ofNullable(String mobilePhone) {
return Objects.nonNull(mobilePhone) ? new MobilePhone(mobilePhone) : null; return Objects.nonNull(mobilePhone) ? new MobilePhone(mobilePhone) : null;
} }

View File

@ -3,6 +3,7 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.domain.ValidatableStringRecord; import xyz.zhouxy.plusone.domain.ValidatableStringRecord;
@ -26,10 +27,12 @@ public class Nickname extends ValidatableStringRecord {
} }
} }
@StaticFactoryMethod(Nickname.class)
public static Nickname of(String nickname) { public static Nickname of(String nickname) {
return new Nickname(nickname); return new Nickname(nickname);
} }
@StaticFactoryMethod(Nickname.class)
public static Nickname ofNullable(String nickname) { public static Nickname ofNullable(String nickname) {
return Objects.nonNull(nickname) ? new Nickname(nickname) : null; return Objects.nonNull(nickname) ? new Nickname(nickname) : null;
} }

View File

@ -7,6 +7,7 @@ import javax.annotation.Nonnull;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.domain.IValueObject; import xyz.zhouxy.plusone.domain.IValueObject;
@ -51,15 +52,18 @@ public class Password implements IValueObject {
this.saltVal = salt; this.saltVal = salt;
} }
@StaticFactoryMethod(Password.class)
public static Password of(String password, String salt) { public static Password of(String password, String salt) {
return new Password(password, salt); return new Password(password, salt);
} }
@StaticFactoryMethod(Password.class)
public static Password newPassword(String newPassword, String passwordConfirmation) { public static Password newPassword(String newPassword, String passwordConfirmation) {
Assert.isTrue(Objects.equals(newPassword, passwordConfirmation), "两次输入的密码不一致"); Assert.isTrue(Objects.equals(newPassword, passwordConfirmation), "两次输入的密码不一致");
return newPassword(newPassword); return newPassword(newPassword);
} }
@StaticFactoryMethod(Password.class)
public static Password newPassword(String newPassword) { public static Password newPassword(String newPassword) {
return new Password(newPassword); return new Password(newPassword);
} }
@ -80,6 +84,7 @@ public class Password implements IValueObject {
return saltVal; return saltVal;
} }
@StaticFactoryMethod(Nickname.class)
public static Password newDefaultPassword() { public static Password newDefaultPassword() {
return newPassword(DEFAULT_PASSWORD); return newPassword(DEFAULT_PASSWORD);
} }

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.util.Enumeration; import xyz.zhouxy.plusone.commons.util.Enumeration;
import xyz.zhouxy.plusone.domain.IValueObject; import xyz.zhouxy.plusone.domain.IValueObject;
@ -26,6 +27,7 @@ public final class Sex extends Enumeration<Sex> implements IValueObject {
private static final ValueSet<Sex> VALUE_SET = new ValueSet<>(UNSET, MALE, FEMALE); private static final ValueSet<Sex> VALUE_SET = new ValueSet<>(UNSET, MALE, FEMALE);
@StaticFactoryMethod(Sex.class)
public static Sex of(int value) { public static Sex of(int value) {
return VALUE_SET.get(value); return VALUE_SET.get(value);
} }

View File

@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.constant.PatternConsts;
/** /**
@ -24,6 +25,7 @@ public class Username extends Principal {
} }
} }
@StaticFactoryMethod(Username.class)
public static Username of(String username) { public static Username of(String username) {
return new Username(username); return new Username(username);
} }

View File

@ -8,6 +8,7 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import lombok.ToString; import lombok.ToString;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithLabel; import xyz.zhouxy.plusone.domain.IWithLabel;
import xyz.zhouxy.plusone.domain.IWithVersion; import xyz.zhouxy.plusone.domain.IWithVersion;
@ -70,12 +71,14 @@ public class Dict extends AggregateRoot<Long> implements IWithLabel, IWithVersio
this.version = version; this.version = version;
} }
@StaticFactoryMethod(Dict.class)
public static Dict newInstance( public static Dict newInstance(
String dictType, String dictType,
String dictLabel) { String dictLabel) {
return new Dict(null, dictType, dictLabel, Collections.emptySet(), 0); return new Dict(null, dictType, dictLabel, Collections.emptySet(), 0);
} }
@StaticFactoryMethod(Dict.class)
public static Dict newInstance( public static Dict newInstance(
String dictType, String dictType,
String dictLabel, String dictLabel,
@ -83,6 +86,7 @@ public class Dict extends AggregateRoot<Long> implements IWithLabel, IWithVersio
return new Dict(null, dictType, dictLabel, values, 0); return new Dict(null, dictType, dictLabel, values, 0);
} }
@StaticFactoryMethod(Dict.class)
public static Dict newInstance( public static Dict newInstance(
String dictType, String dictType,
String dictLabel, String dictLabel,

View File

@ -4,6 +4,7 @@ import java.util.Objects;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.IValueObject; import xyz.zhouxy.plusone.domain.IValueObject;
/** /**
@ -25,6 +26,7 @@ public class DictValue implements IValueObject {
this.label = label; this.label = label;
} }
@StaticFactoryMethod(DictValue.class)
public static DictValue of(int key, String label) { public static DictValue of(int key, String label) {
return new DictValue(key, label); return new DictValue(key, label);
} }

View File

@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.system.domain.model.menu;
import java.util.List; import java.util.List;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.constant.EntityStatus; import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType; import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
@ -16,6 +17,7 @@ public class MenuConstructor {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
@StaticFactoryMethod(Menu.class)
public static Menu newMenuItem( public static Menu newMenuItem(
long parentId, long parentId,
String path, String path,
@ -39,6 +41,7 @@ public class MenuConstructor {
remarks, component, cache, resource, actions, 0L); remarks, component, cache, resource, actions, 0L);
} }
@StaticFactoryMethod(Menu.class)
public static Menu newMenuList( public static Menu newMenuList(
long parentId, long parentId,
String path, String path,

View File

@ -3,6 +3,7 @@ package xyz.zhouxy.plusone.system.domain.model.permission;
import java.util.Optional; import java.util.Optional;
import lombok.Getter; import lombok.Getter;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.Entity; import xyz.zhouxy.plusone.domain.Entity;
import xyz.zhouxy.plusone.domain.IWithLabel; import xyz.zhouxy.plusone.domain.IWithLabel;
import xyz.zhouxy.plusone.domain.IWithVersion; import xyz.zhouxy.plusone.domain.IWithVersion;
@ -20,7 +21,7 @@ public class Action extends Entity<Long> implements IWithLabel, IWithVersion {
@Getter String label; @Getter String label;
@Getter long version; @Getter long version;
public Action(Long id, String resource, String identifier, String label, long version) { private Action(Long id, String resource, String identifier, String label, long version) {
this.id = id; this.id = id;
this.resource = resource; this.resource = resource;
this.identifier = identifier; this.identifier = identifier;
@ -28,10 +29,12 @@ public class Action extends Entity<Long> implements IWithLabel, IWithVersion {
this.version = version; this.version = version;
} }
@StaticFactoryMethod(Action.class)
static Action newInstance(String resource, String identifier, String label) { static Action newInstance(String resource, String identifier, String label) {
return new Action(null, resource, identifier, label, 0L); return new Action(null, resource, identifier, label, 0L);
} }
@StaticFactoryMethod(Action.class)
static Action existingInstance(Long id, String resource, String action, String label, Long version) { static Action existingInstance(Long id, String resource, String action, String label, Long version) {
return new Action(id, resource, action, label, version); return new Action(id, resource, action, label, version);
} }

View File

@ -6,6 +6,7 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import lombok.Getter; import lombok.Getter;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithVersion; import xyz.zhouxy.plusone.domain.IWithVersion;
@ -37,6 +38,7 @@ public class Permission extends AggregateRoot<Long> implements IWithVersion {
// ==================== 实例化 ==================== // ==================== 实例化 ====================
@StaticFactoryMethod(Permission.class)
public static Permission newInstance(String resource) { public static Permission newInstance(String resource) {
return new Permission( return new Permission(
null, resource, null, resource,