Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to clone values from an object without using reflection?

Tags:

java

java-8

Trying to efficiently check to see if a new copy of the object has any different fields, and if they do, update the local ones and make a note of it. If any of the fields change then I need to persist the object to the database. I don't want to make that call if I don't have to, hence the boolean.

I couldn't think of a better way to do this without using reflection, but I don't want to use reflection here because of the lack of compiler-backed safety (would have string references to field names), and not all the fields are the same type (I have some Java 8 Instant fields in there).

What I really want to avoid is the book-keeping of having to remember to add or subtract to/from the sync method when the fields are modified. Obviously subtracting is not a big deal because the method will break, but adding is scary if someone doesn't remember to update the new field.

public boolean syncWithFieldsFrom(User currentUser) {
    boolean doesUserNeedUpdating = false;
    if (!StringUtils.equals(email, currentUser.email)) {
        email = currentUser.email;
        doesUserNeedUpdating = true;
    }
    if (!StringUtils.equals(firstName, currentUser.firstName)) {
        firstName = currentUser.firstName;
        doesUserNeedUpdating = true;
    }
    if (!StringUtils.equals(lastName, currentUser.lastName)) {
        lastName = currentUser.lastName;
        doesUserNeedUpdating = true;
    }
    if (!StringUtils.equals(fullName, currentUser.fullName)) {
        fullName = currentUser.fullName;
        doesUserNeedUpdating = true;
    }
    return doesUserNeedUpdating;
}
like image 770
Jazzepi Avatar asked Mar 06 '23 01:03

Jazzepi


1 Answers

This may be a little overkill, but you can use lambdas to extract the fields and run a loop against them. I'll assume you have getters and setters for simplicity's sake.

private static class Field<T> {
    final Function<User, T> getter;
    final BiConsumer<User, T> setter;

    Field(Function<User, T> getter, BiConsumer<User, T> setter) {
        this.getter = getter;
        this.setter = setter;
    }

    boolean sync(User src, User dst) {
        T srcField = getter.apply(src);
        if (!Objects.equal(srcField, getter.apply(dst))) {
            setter.accept(dst, srcField);
            return true;
        }
        return false;
    }
}

private static final List<Field<?>> FIELDS = Arrays.asList(
        new Field<>(User::getEmail, User::setEmail),
        new Field<>(User::getFirstName, User::setFirstName),
        new Field<>(User::getLastName, User::setLastName),
        new Field<>(User::getFullName, User::setFullName));

public boolean syncWithFieldsFrom(User currentUser) {
    boolean updated = false;
    for (Field<?> f : FIELDS) {
        updated |= f.sync(currentUser, this);
    }
    return updated;
}
like image 50
shmosel Avatar answered May 09 '23 21:05

shmosel