|
| 1 | +#Diff Objects |
| 2 | + |
| 3 | +This project helps you to build functionalities that needs to show the differences between two objects. |
| 4 | + |
| 5 | +You can use this project in two ways, using annotations or a Builder to map the properties of the object that will be |
| 6 | +checked for difference. |
| 7 | + |
| 8 | +##DiffBuilder |
| 9 | + |
| 10 | +The DiffBuilder provide an API to map an object without the use of annotations: |
| 11 | + |
| 12 | +**Example** |
| 13 | + |
| 14 | +``` |
| 15 | +public class User { |
| 16 | + |
| 17 | + private final String login; |
| 18 | + private final String password; |
| 19 | + private final LocalDate birthday; |
| 20 | + private final Boolean active; |
| 21 | + private final List<Email> emails; |
| 22 | + |
| 23 | + // getters and constructor ommited ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +``` |
| 28 | +public class Email { |
| 29 | + |
| 30 | + private final String description; |
| 31 | + private final Boolean active; |
| 32 | + |
| 33 | + // getters and constructor ommited ... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +``` |
| 38 | +public class Main { |
| 39 | + |
| 40 | + public static void main(String[] args) { |
| 41 | + User u1 = new User("user1", "123456", LocalDate.now().minusDays(1), true); |
| 42 | + User u2 = new User("user2", "12345678", LocalDate.now(), true); |
| 43 | + |
| 44 | + u1.getEmails().add(new Email("user@gmail.com", true)); |
| 45 | + u2.getEmails().add(new Email("user@gmail.com", false)); |
| 46 | + |
| 47 | + DiffConfiguration configuration = DiffBuilder.map(User.class) |
| 48 | + .mapper() |
| 49 | + .mappingAll() |
| 50 | + .mapping("emails", "description") |
| 51 | + .instance() |
| 52 | + .configuration(); |
| 53 | + |
| 54 | + List<DiffResult<?>> diffs = DiffObjects.diff(u1, u2, configuration); |
| 55 | + |
| 56 | + for (DiffResult<?> diff : diffs) { |
| 57 | + if (!diff.isEquals()) { |
| 58 | + System.out.println("Field: " + diff.getProperties().get("field")); |
| 59 | + System.out.println("Before: " + diff.getBefore()); |
| 60 | + System.out.println("After: " + diff.getAfter()); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +##Annotations |
| 68 | + |
| 69 | +The annotations provided by the diff objects are: |
| 70 | + |
| 71 | +- DiffMapping - to map a method of the object; |
| 72 | +- DiffMappings - to map multiple properties of an object relationship; |
| 73 | +- DiffProperty - to add properties that will be on the diff result for access; |
| 74 | + |
| 75 | +**Example** |
| 76 | + |
| 77 | +``` |
| 78 | +public class User { |
| 79 | + |
| 80 | + // fields and constructor ommited ... |
| 81 | + |
| 82 | + @DiffMapping |
| 83 | + public String getLogin() { |
| 84 | + return login; |
| 85 | + } |
| 86 | + |
| 87 | + @DiffMapping |
| 88 | + public String getPassword() { |
| 89 | + return password; |
| 90 | + } |
| 91 | + |
| 92 | + @DiffMapping |
| 93 | + public LocalDate getBirthday() { |
| 94 | + return birthday; |
| 95 | + } |
| 96 | + |
| 97 | + @DiffMapping |
| 98 | + public Boolean getActive() { |
| 99 | + return active; |
| 100 | + } |
| 101 | + |
| 102 | + @DiffMapping(value = "description") |
| 103 | + public List<Email> getEmails() { |
| 104 | + return emails; |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +``` |
| 110 | +public class Main { |
| 111 | + |
| 112 | + public static void main(String[] args) { |
| 113 | + User u1 = new User("user1", "123456", LocalDate.now().minusDays(1), true); |
| 114 | + User u2 = new User("user2", "12345678", LocalDate.now(), true); |
| 115 | + |
| 116 | + u1.getEmails().add(new Email("user@gmail.com", true)); |
| 117 | + u2.getEmails().add(new Email("user@gmail.com", false)); |
| 118 | + |
| 119 | + List<DiffResult<?>> diffs = DiffObjects.diff(u1, u2); |
| 120 | + |
| 121 | + for (DiffResult<?> diff : diffs) { |
| 122 | + if (!diff.isEquals()) { |
| 123 | + System.out.println("Field: " + diff.getProperties().get("field")); |
| 124 | + System.out.println("Before: " + diff.getBefore()); |
| 125 | + System.out.println("After: " + diff.getAfter()); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +###@DiffProperty |
| 133 | + |
| 134 | +This annotations is used to provide information in the DiffResult about the field. All objects already have the |
| 135 | +property "field" with the field name as value. |
| 136 | + |
| 137 | +`@DiffProperty(key = "field", value = "{{fieldName}}")` |
| 138 | + |
| 139 | +**Example** |
| 140 | + |
| 141 | +``` |
| 142 | +public class User { |
| 143 | + |
| 144 | + // code ommited ... |
| 145 | + |
| 146 | + @DiffMapping(value = "description", properties = { |
| 147 | + @DiffProperty(key = "id", value = "E-mail:"), |
| 148 | + @DiffProperty(key = "maxlength", value = "50") |
| 149 | + }) |
| 150 | + public List<Email> getEmails() { |
| 151 | + return emails; |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +``` |
| 157 | +public class Main { |
| 158 | + |
| 159 | + public static void main(String[] args) { |
| 160 | + // code omitted ... |
| 161 | + |
| 162 | + List<DiffResult<?>> diffs = DiffObjects.diff(u1, u2); |
| 163 | + |
| 164 | + for (DiffResult<?> diff : diffs) { |
| 165 | + if (diff.getProperties().containsKey("id")) { |
| 166 | + String id = diff.getProperties().get("id"); |
| 167 | + String maxlength = diff.getProperties().get("maxlength"); |
| 168 | + |
| 169 | + System.out.println("Id: " + id); |
| 170 | + System.out.println("Maxlength: " + maxlength); |
| 171 | + System.out.println("Field: " + diff.getProperties().get("field")); |
| 172 | + System.out.println("Before: " + diff.getBefore()); |
| 173 | + System.out.println("After: " + diff.getAfter()); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +##Future implementations |
| 181 | + |
| 182 | +- Order of the result using @DiffOrder not implemented yet; |
| 183 | +- Disable mapped fields after calling DiffBuilder.map(User.class).mapper().mappingAll(); |
| 184 | +- Enable only different object for diff result; |
| 185 | +- Collection with value working with navigate properties like "emails.principal.description"; |
| 186 | +- Remove wildcard from DiffResult class; |
0 commit comments