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

塞巴斯蒂安·卡纳纳科(Sebastian Canonaco):

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

UserRequest类

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;
    }
}

用户类别

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);
    }
}

因此,我的问题是,如何创建用户对象?除空构造函数外,该构造函数是该类唯一的构造函数。此外,该班级没有任何塞特犬。

djharten:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

具有字符串参数的构造函数,以及具有对象参数的相同重载

具有动态对象数组的参数化构造函数

具有不同构造函数参数的 CPP 对象数组

具有构造函数的新对象数组,该构造函数需要C ++中的参数

从具有可变参数模板构造函数的类型构造 std::function 对象

JavaScript函数将具有相同属性的构造函数对象推入数组

通过构造函数制作的所有对象都具有相同的向量

如何在具有相同名称成员的对象的构造函数中限定类型名称?

Unity 容器 - 解析始终相同构造函数参数的对象

构造扩展具有自己的构造函数的超类的对象

模拟对象以返回具有相同参数的真实对象

将ninject绑定对象传递给必须具有0参数的构造函数的类

模拟moq,尝试将对象传递给具有多个参数的构造函数

使用Castle Windsor将具有不同实现的列表的对象作为构造函数参数进行解析

声明具有参数问题的构造函数对象的JavaScript Object属性

具有构造函数参数的Object.assign()属性的TypeScript数据对象类

在对象中具有空参数的Hql构造函数查询

具有默认参数的函数对象的SFINAE

具有对象参数的require()函数?

与参数具有相同原始类型的构造函数

注入具有多个相同类型参数的构造函数

类构造函数如何具有相同类的参数?

具有默认值的单参数构造函数与默认构造函数相同吗?

MVC:此对象没有无参数构造函数

带有对象指针作为参数的C ++复制构造函数

带有布尔对象的odp net oracle参数构造函数

带有构造函数的对象向量,以向量为参数

引用具有禁用的复制构造函数/分配的对象

如何构造具有删除的析构函数的动态对象?