具有相同对象作为参数的对象构造函数

Object constructor with the same object as parameter

我目前正在 Java 使用 FusionAuth 开发应用程序,我正在使用该工具的 Java 客户端,我想创建一个用户,所以我使用 createUser() 方法,这需要UID和一个UserRequest对象,这个,需要一个User对象,下一个是构造函数:

用户请求class

public class UserRequest {
    public boolean sendSetPasswordEmail;
    public boolean skipVerification;
    public User user;

    @JacksonConstructor
    public UserRequest() {
    }

    public UserRequest(User user) {
        this.sendSetPasswordEmail = false;
        this.skipVerification = true;
        this.user = user;
    }

    public UserRequest(boolean sendSetPasswordEmail, boolean skipVerification, User user) {
        this.sendSetPasswordEmail = sendSetPasswordEmail;
        this.skipVerification = skipVerification;
        this.user = user;
    }
}

用户class

public class User extends SecureIdentity implements Buildable<User>, _InternalJSONColumn, Tenantable {
    @InternalJSONColumn
    @JsonMerge(OptBoolean.FALSE)
    public final List<Locale> preferredLanguages = new ArrayList();
    @JsonMerge(OptBoolean.FALSE)
    private final List<GroupMember> memberships = new ArrayList();
    @JsonMerge(OptBoolean.FALSE)
    private final List<UserRegistration> registrations = new ArrayList();
    public boolean active;
    public LocalDate birthDate;
    public UUID cleanSpeakId;
    @JsonMerge(OptBoolean.FALSE)
    public Map<String, Object> data = new LinkedHashMap();
    public String email;
    public ZonedDateTime expiry;
    public String firstName;
    public String fullName;
    public URI imageUrl;
    public ZonedDateTime insertInstant;
    public ZonedDateTime lastLoginInstant;
    public String lastName;
    public String middleName;
    public String mobilePhone;
    public String parentEmail;
    public UUID tenantId;
    public ZoneId timezone;
    public TwoFactorDelivery twoFactorDelivery;
    public boolean twoFactorEnabled;
    public String twoFactorSecret;
    public String username;
    public ContentStatus usernameStatus;

    public User() {
    }

    public User(User user) {
        this.active = user.active;
        this.birthDate = user.birthDate;
        this.cleanSpeakId = user.cleanSpeakId;
        this.email = user.email;
        this.encryptionScheme = user.encryptionScheme;
        this.expiry = user.expiry;
        this.factor = user.factor;
        this.firstName = user.firstName;
        this.fullName = user.fullName;
        this.id = user.id;
        this.imageUrl = user.imageUrl;
        this.insertInstant = user.insertInstant;
        this.lastLoginInstant = user.lastLoginInstant;
        this.lastName = user.lastName;
        this.memberships.addAll((Collection)user.memberships.stream().map(GroupMember::new).collect(Collectors.toList()));
        this.middleName = user.middleName;
        this.mobilePhone = user.mobilePhone;
        this.parentEmail = user.parentEmail;
        this.password = user.password;
        this.passwordChangeRequired = user.passwordChangeRequired;
        this.passwordLastUpdateInstant = user.passwordLastUpdateInstant;
        this.preferredLanguages.addAll(user.preferredLanguages);
        this.registrations.addAll((Collection)user.registrations.stream().map(UserRegistration::new).collect(Collectors.toList()));
        this.salt = user.salt;
        this.tenantId = user.tenantId;
        this.timezone = user.timezone;
        this.twoFactorDelivery = user.twoFactorDelivery;
        this.twoFactorEnabled = user.twoFactorEnabled;
        this.twoFactorSecret = user.twoFactorSecret;
        this.username = user.username;
        this.usernameStatus = user.usernameStatus;
        this.verified = user.verified;
        if (user.data != null) {
            this.data.putAll(user.data);
        }

    }

    public void addMemberships(GroupMember member) {
        this.memberships.removeIf((m) -> {
            return m.groupId.equals(member.groupId);
        });
        this.memberships.add(member);
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User user = (User)o;
            this.sort();
            user.sort();
            return super.equals(o) && Objects.equals(this.active, user.active) && Objects.equals(this.birthDate, user.birthDate) && Objects.equals(this.cleanSpeakId, user.cleanSpeakId) && Objects.equals(this.data, user.data) && Objects.equals(this.email, user.email) && Objects.equals(this.expiry, user.expiry) && Objects.equals(this.firstName, user.firstName) && Objects.equals(this.fullName, user.fullName) && Objects.equals(this.imageUrl, user.imageUrl) && Objects.equals(this.insertInstant, user.insertInstant) && Objects.equals(this.lastLoginInstant, user.lastLoginInstant) && Objects.equals(this.lastName, user.lastName) && Objects.equals(this.memberships, user.memberships) && Objects.equals(this.middleName, user.middleName) && Objects.equals(this.mobilePhone, user.mobilePhone) && Objects.equals(this.registrations, user.registrations) && Objects.equals(this.parentEmail, user.parentEmail) && Objects.equals(this.tenantId, user.tenantId) && Objects.equals(this.timezone, user.timezone) && Objects.equals(this.twoFactorDelivery, user.twoFactorDelivery) && Objects.equals(this.twoFactorEnabled, user.twoFactorEnabled) && Objects.equals(this.twoFactorSecret, user.twoFactorSecret) && Objects.equals(this.username, user.username) && Objects.equals(this.usernameStatus, user.usernameStatus);
        }
    }

    @JsonIgnore
    public int getAge() {
        return this.birthDate == null ? -1 : (int)this.birthDate.until(LocalDate.now(), ChronoUnit.YEARS);
    }

    @JsonIgnore
    public String getLogin() {
        return this.email == null ? this.username : this.email;
    }

    public List<GroupMember> getMemberships() {
        return this.memberships;
    }

    @JsonIgnore
    public String getName() {
        if (this.fullName != null) {
            return this.fullName;
        } else {
            return this.firstName != null ? this.firstName + (this.lastName != null ? " " + this.lastName : "") : null;
        }
    }

    public UserRegistration getRegistrationForApplication(UUID id) {
        return (UserRegistration)this.getRegistrations().stream().filter((reg) -> {
            return reg.applicationId.equals(id);
        }).findFirst().orElse((Object)null);
    }

    public List<UserRegistration> getRegistrations() {
        return this.registrations;
    }

    public Set<String> getRoleNamesForApplication(UUID id) {
        UserRegistration registration = this.getRegistrationForApplication(id);
        return registration != null ? registration.roles : null;
    }

    public UUID getTenantId() {
        return this.tenantId;
    }

    public boolean hasUserData() {
        if (!this.data.isEmpty()) {
            return true;
        } else {
            Iterator var1 = this.registrations.iterator();

            UserRegistration userRegistration;
            do {
                if (!var1.hasNext()) {
                    return false;
                }

                userRegistration = (UserRegistration)var1.next();
            } while(!userRegistration.hasRegistrationData());

            return true;
        }
    }

    public int hashCode() {
        return Objects.hash(new Object[]{super.hashCode(), this.active, this.birthDate, this.cleanSpeakId, this.data, this.email, this.expiry, this.firstName, this.fullName, this.imageUrl, this.insertInstant, this.lastLoginInstant, this.lastName, this.memberships, this.middleName, this.mobilePhone, this.registrations, this.parentEmail, this.tenantId, this.timezone, this.twoFactorDelivery, this.twoFactorEnabled, this.twoFactorSecret, this.username, this.usernameStatus});
    }

    public String lookupEmail() {
        if (this.email != null) {
            return this.email;
        } else {
            return this.data.containsKey("email") ? this.data.get("email").toString() : null;
        }
    }

    public Locale lookupPreferredLanguage(UUID applicationId) {
        Iterator var2 = this.registrations.iterator();

        UserRegistration registration;
        do {
            if (!var2.hasNext()) {
                if (this.preferredLanguages.size() > 0) {
                    return (Locale)this.preferredLanguages.get(0);
                }

                return null;
            }

            registration = (UserRegistration)var2.next();
        } while(!registration.applicationId.equals(applicationId) || registration.preferredLanguages.size() <= 0);

        return (Locale)registration.preferredLanguages.get(0);
    }

    public void normalize() {
        Normalizer.removeEmpty(this.data);
        this.email = Normalizer.toLowerCase(Normalizer.trim(this.email));
        this.encryptionScheme = Normalizer.trim(this.encryptionScheme);
        this.firstName = Normalizer.trim(this.firstName);
        this.fullName = Normalizer.trim(this.fullName);
        this.lastName = Normalizer.trim(this.lastName);
        this.middleName = Normalizer.trim(this.middleName);
        this.mobilePhone = Normalizer.trim(this.mobilePhone);
        this.parentEmail = Normalizer.toLowerCase(Normalizer.trim(this.parentEmail));
        this.preferredLanguages.removeIf(Objects::isNull);
        this.username = Normalizer.trim(this.username);
        if (this.username != null && this.username.length() == 0) {
            this.username = null;
        }

        this.getRegistrations().forEach(UserRegistration::normalize);
    }

    public void removeMembershipById(UUID groupId) {
        this.memberships.removeIf((m) -> {
            return m.groupId.equals(groupId);
        });
    }

    public User secure() {
        this.encryptionScheme = null;
        this.factor = null;
        this.password = null;
        this.salt = null;
        this.twoFactorSecret = null;
        return this;
    }

    public User sort() {
        this.registrations.sort(Comparator.comparing((ur) -> {
            return ur.applicationId;
        }));
        return this;
    }

    public String toString() {
        return ToString.toString(this);
    }
}

所以,我的问题是,如何创建用户对象? 该构造函数是 class 除了空构造函数之外唯一的构造函数。 class 也没有任何设置器。

我查看了您正在使用的库的源代码。这些字段都是 public,因此您可以创建一个空构造函数,然后通过执行 user.name = xxxx.

来设置它们