Skip to content

Commit 8d69179

Browse files
committed
Merge pull request #306 from sialcasa/pr/298
#298 some small fixes for the pull request for list mapping
2 parents 8db526d + 35e4f5b commit 8d69179

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

mvvmfx/src/main/java/de/saxsys/mvvmfx/utils/mapping/ModelWrapper.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@
204204
*
205205
*
206206
*
207-
*
208-
*
209-
*
210207
* @param <M>
211208
* the type of the model class.
212209
*/
@@ -220,8 +217,9 @@ public class ModelWrapper<M> {
220217
/**
221218
* This interface defines the operations that are possible for each field of a wrapped class.
222219
*
223-
* @param <T>
224-
* @param <M>
220+
* @param <T> target type. The base type of the returned property, f.e. {@link String}.
221+
* @param <M> model type. The type of the Model class, that is wrapped by this ModelWrapper instance.
222+
* @param <R> return type. The type of the Property that is returned via {@link #getProperty()}, f.e. {@link StringProperty} or {@link Property<String>}.
225223
*/
226224
private interface PropertyField<T, M, R extends Property<T>> {
227225
void commit(M wrappedObject);
@@ -232,6 +230,14 @@ private interface PropertyField<T, M, R extends Property<T>> {
232230

233231
R getProperty();
234232

233+
/**
234+
* Determines if the value in the model object and the property field are different or not.
235+
*
236+
* This method is used to implement the {@link #differentProperty()} flag.
237+
*
238+
* @param wrappedObject the wrapped model object
239+
* @return <code>false</code> if both the wrapped model object and the property field have the same value, otherwise <code>true</code>
240+
*/
235241
boolean isDifferent(M wrappedObject);
236242
}
237243

@@ -348,8 +354,8 @@ public boolean isDifferent(M wrappedObject) {
348354
}
349355

350356
/**
351-
* An implementation of {@link PropertyField} that is used when the field of the model class is a {@link List} and
352-
* and is a JavaFX {@link ListProperty} too.
357+
* An implementation of {@link PropertyField} that is used when the field of the model class is a {@link List}
358+
* and will be mapped to a JavaFX {@link ListProperty}.
353359
*
354360
* @param <T>
355361
* @param <E>
@@ -370,7 +376,7 @@ public FxListPropertyField(ListPropertyAccessor<M, E> accessor, List<E> defaultV
370376
this.accessor = accessor;
371377
this.defaultValue = defaultValue;
372378

373-
this.targetProperty.addListener((ListChangeListener) change -> propertyWasChanged());
379+
this.targetProperty.addListener((ListChangeListener<E>) change -> ModelWrapper.this.propertyWasChanged());
374380
}
375381

376382
@Override
@@ -398,7 +404,7 @@ public boolean isDifferent(M wrappedObject) {
398404
final List<E> modelValue = accessor.apply(wrappedObject).getValue();
399405
final List<E> wrapperValue = targetProperty;
400406

401-
return !(modelValue.containsAll(wrapperValue) && wrapperValue.containsAll(modelValue));
407+
return !Objects.equals(modelValue, wrapperValue);
402408
}
403409
}
404410

@@ -428,8 +434,8 @@ public BeanListPropertyField(ListGetter<M, E> getter, ListSetter<M, E> setter, L
428434
this.defaultValue = defaultValue;
429435
this.getter = getter;
430436
this.setter = setter;
431-
432-
this.targetProperty.addListener((ListChangeListener) change -> propertyWasChanged());
437+
438+
this.targetProperty.addListener((ListChangeListener<E>) change -> propertyWasChanged());
433439
}
434440

435441
@Override
@@ -457,7 +463,7 @@ public boolean isDifferent(M wrappedObject) {
457463
final List<E> modelValue = getter.apply(wrappedObject);
458464
final List<E> wrapperValue = targetProperty;
459465

460-
return !(modelValue.containsAll(wrapperValue) && wrapperValue.containsAll(modelValue));
466+
return !Objects.equals(modelValue, wrapperValue);
461467
}
462468
}
463469

mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/mapping/ModelWrapperTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,6 @@ public void testDifferentFlag() {
372372
assertThat(personWrapper.isDifferent()).isFalse();
373373

374374

375-
nicknames.remove("captain");
376-
assertThat(personWrapper.isDifferent()).isTrue();
377-
378375
nicknames.remove("captain");
379376
assertThat(personWrapper.isDifferent()).isTrue();
380377

@@ -393,8 +390,19 @@ public void testDifferentFlag() {
393390
personWrapper.reload();
394391
assertThat(personWrapper.isDifferent()).isFalse();
395392

396-
nicknames.add("captain");
397-
assertThat(personWrapper.isDifferent()).isFalse();
393+
nicknames.add("captain"); // duplicate captain
394+
assertThat(personWrapper.isDifferent()).isTrue();
395+
396+
person.getNicknames().add("captain"); // now both have 2x "captain" but the modelWrapper has no chance to realize this change in the model element...
397+
// ... for this reason the different flag will still be true
398+
assertThat(personWrapper.isDifferent()).isTrue();
399+
400+
// ... but if we add another value to the nickname-Property, the modelWrapper can react to this change
401+
person.getNicknames().add("other");
402+
nicknames.add("other");
403+
assertThat(personWrapper.isDifferent()).isFalse();
404+
405+
398406

399407
nicknames.add("player");
400408
assertThat(personWrapper.isDifferent()).isTrue();
@@ -455,12 +463,23 @@ public void testDifferentFlagWithFxProperties() {
455463

456464
nicknames.add("captain");
457465
assertThat(personWrapper.isDifferent()).isFalse();
466+
467+
person.getNicknames().add("captain"); // duplicate value
468+
nicknames.add("captain");
469+
assertThat(personWrapper.isDifferent()).isFalse();
458470

459471
nicknames.add("player");
460472
assertThat(personWrapper.isDifferent()).isTrue();
461473

462-
nicknames.remove("player");
474+
person.getNicknames().add("player");
475+
assertThat(personWrapper.isDifferent()).isTrue(); // still true because the modelWrapper can't detect the change in the model
476+
477+
person.setName("luise");
478+
name.set("luise"); // this triggers the recalculation of the different-flag which will now detect the previous change to the nicknames list
463479
assertThat(personWrapper.isDifferent()).isFalse();
480+
481+
482+
464483

465484
nicknames.setValue(FXCollections.observableArrayList("spectator"));
466485
assertThat(personWrapper.isDifferent()).isTrue();

mvvmfx/src/test/java/de/saxsys/mvvmfx/utils/mapping/Person.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package de.saxsys.mvvmfx.utils.mapping;
22

3-
import javafx.collections.FXCollections;
4-
import javafx.collections.ObservableList;
5-
3+
import java.util.ArrayList;
64
import java.util.List;
75

86
public class Person {
@@ -11,14 +9,14 @@ public class Person {
119

1210
private int age;
1311

14-
private ObservableList<String> nicknames = FXCollections.observableArrayList();
12+
private List<String> nicknames = new ArrayList<>();
1513

1614
public List<String> getNicknames() {
1715
return nicknames;
1816
}
1917

2018
public void setNicknames (List<String> nicknames) {
21-
this.nicknames.setAll(nicknames);
19+
this.nicknames = nicknames;
2220
}
2321

2422
public int getAge() {

0 commit comments

Comments
 (0)