Skip to content

Commit 59cf4de

Browse files
author
Jose Pereda
committed
8267505: {List,Set,Map}PropertyBase::bind should check against identity
Reviewed-by: mhanl, kcr
1 parent fe81b9c commit 59cf4de

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed

modules/javafx.base/src/main/java/javafx/beans/property/ListPropertyBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public void bind(final ObservableValue<? extends ObservableList<E>> newObservabl
270270
throw new NullPointerException("Cannot bind to null");
271271
}
272272

273-
if (!newObservable.equals(observable)) {
273+
if (newObservable != observable) {
274274
unbind();
275275
observable = newObservable;
276276
if (listener == null) {

modules/javafx.base/src/main/java/javafx/beans/property/MapPropertyBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void bind(final ObservableValue<? extends ObservableMap<K, V>> newObserva
271271
if (newObservable == null) {
272272
throw new NullPointerException("Cannot bind to null");
273273
}
274-
if (!newObservable.equals(observable)) {
274+
if (newObservable != observable) {
275275
unbind();
276276
observable = newObservable;
277277
if (listener == null) {

modules/javafx.base/src/main/java/javafx/beans/property/SetPropertyBase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public void bind(final ObservableValue<? extends ObservableSet<E>> newObservable
272272
throw new NullPointerException("Cannot bind to null");
273273
}
274274

275-
if (!newObservable.equals(this.observable)) {
275+
if (newObservable != this.observable) {
276276
unbind();
277277
observable = newObservable;
278278
if (listener == null) {

modules/javafx.base/src/test/java/test/javafx/beans/property/ListPropertyBaseTest.java

+37
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,43 @@ public void testRebind() {
627627
invalidationListener.check(null, 0);
628628
}
629629

630+
@Test
631+
public void testRebind_Identity() {
632+
final ListProperty<Object> v1 = new SimpleListProperty<>(FXCollections.observableArrayList());
633+
final ListProperty<Object> v2 = new SimpleListProperty<>(FXCollections.observableArrayList());
634+
attachListChangeListener();
635+
636+
// bind
637+
property.bind(v1);
638+
property.check(1);
639+
listChangeListener.check1AddRemove(property, EMPTY_LIST, 0, 0);
640+
listChangeListener.clear();
641+
642+
// rebind to same
643+
property.bind(v1);
644+
property.check(0);
645+
listChangeListener.check0();
646+
647+
// rebind to other, without explicitly unbinding
648+
property.bind(v2);
649+
property.check(1);
650+
listChangeListener.check1AddRemove(property, EMPTY_LIST, 0, 0);
651+
listChangeListener.clear();
652+
653+
v2.add("One");
654+
listChangeListener.check1AddRemove(property, EMPTY_LIST, 0, 1);
655+
listChangeListener.clear();
656+
657+
v2.add("Two");
658+
listChangeListener.check1AddRemove(property, EMPTY_LIST, 1, 2);
659+
listChangeListener.clear();
660+
661+
property.check(4);
662+
assertTrue(property.isBound());
663+
assertEquals(2, property.toArray().length);
664+
assertEquals("ListProperty [bound, value: [One, Two]]", property.toString());
665+
}
666+
630667
@Test
631668
public void testUnbind() {
632669
attachInvalidationListener();

modules/javafx.base/src/test/java/test/javafx/beans/property/MapPropertyBaseTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,41 @@ public void testRebind() {
650650
invalidationListener.check(null, 0);
651651
}
652652

653+
@Test
654+
public void testRebind_Identity() {
655+
final MapProperty<Object, Object> v1 = new SimpleMapProperty<>(FXCollections.observableHashMap());
656+
final MapProperty<Object, Object> v2 = new SimpleMapProperty<>(FXCollections.observableHashMap());
657+
attachMapChangeListener();
658+
659+
// bind
660+
property.bind(v1);
661+
property.check(1);
662+
mapChangeListener.clear();
663+
664+
// rebind to same
665+
property.bind(v1);
666+
property.check(0);
667+
mapChangeListener.check0();
668+
669+
// rebind to other, without explicitly unbinding
670+
property.bind(v2);
671+
property.check(1);
672+
mapChangeListener.clear();
673+
674+
v2.put("One", "1");
675+
mapChangeListener.assertAdded(MockMapObserver.Tuple.tup("One", "1"));
676+
mapChangeListener.clear();
677+
678+
v2.put("Two", "2");
679+
mapChangeListener.assertAdded(MockMapObserver.Tuple.tup("Two", "2"));
680+
mapChangeListener.clear();
681+
682+
property.check(4);
683+
assertTrue(property.isBound());
684+
assertEquals(2, property.size());
685+
assertEquals("MapProperty [bound, value: {One=1, Two=2}]", property.toString());
686+
}
687+
653688
@Test
654689
public void testUnbind() {
655690
attachInvalidationListener();

modules/javafx.base/src/test/java/test/javafx/beans/property/SetPropertyBaseTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,41 @@ public void testRebind() {
587587
invalidationListener.check(null, 0);
588588
}
589589

590+
@Test
591+
public void testRebind_Identity() {
592+
final SetProperty<Object> v1 = new SimpleSetProperty<>(FXCollections.observableSet());
593+
final SetProperty<Object> v2 = new SimpleSetProperty<>(FXCollections.observableSet());
594+
attachSetChangeListener();
595+
596+
// bind
597+
property.bind(v1);
598+
property.check(1);
599+
setChangeListener.clear();
600+
601+
// rebind to same
602+
property.bind(v1);
603+
property.check(0);
604+
setChangeListener.check0();
605+
606+
// rebind to other, without explicitly unbinding
607+
property.bind(v2);
608+
property.check(1);
609+
setChangeListener.clear();
610+
611+
v2.add("One");
612+
setChangeListener.assertAdded(Tuple.tup("One"));
613+
setChangeListener.clear();
614+
615+
v2.add("Two");
616+
setChangeListener.assertAdded(Tuple.tup("Two"));
617+
setChangeListener.clear();
618+
619+
property.check(4);
620+
assertTrue(property.isBound());
621+
assertEquals(2, property.toArray().length);
622+
assertEquals("SetProperty [bound, value: [Two, One]]", property.toString());
623+
}
624+
590625
@Test
591626
public void testUnbind() {
592627
attachInvalidationListener();

0 commit comments

Comments
 (0)