Skip to content

Commit b3eca1f

Browse files
Marius Hanlkevinrushforth
authored andcommitted
8283346: Optimize observable ArrayList creation in FXCollections
Reviewed-by: mstrauss, nlisker, kcr
1 parent 1f62570 commit b3eca1f

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

modules/javafx.base/src/main/java/javafx/collections/FXCollections.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -347,9 +347,7 @@ public static <E> ObservableList<E> observableArrayList(Callback<E, Observable[]
347347
* @see #observableArrayList()
348348
*/
349349
public static <E> ObservableList<E> observableArrayList(E... items) {
350-
ObservableList<E> list = observableArrayList();
351-
list.addAll(items);
352-
return list;
350+
return observableList(new ArrayList<>(Arrays.asList(items)));
353351
}
354352

355353
/**
@@ -360,9 +358,7 @@ public static <E> ObservableList<E> observableArrayList(E... items) {
360358
* @return a newly created observableArrayList
361359
*/
362360
public static <E> ObservableList<E> observableArrayList(Collection<? extends E> col) {
363-
ObservableList<E> list = observableArrayList();
364-
list.addAll(col);
365-
return list;
361+
return observableList(new ArrayList<>(col));
366362
}
367363

368364
/**

modules/javafx.base/src/test/java/test/javafx/collections/FXCollectionsTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,47 @@
4242

4343
public class FXCollectionsTest {
4444

45+
@Test
46+
public void testCreateObservableArrayListFromArray() {
47+
ObservableList<String> observableList = FXCollections.observableArrayList("1", "2", null);
48+
49+
assertEquals(3, observableList.size());
50+
assertTrue(observableList.contains("1"));
51+
assertTrue(observableList.contains("2"));
52+
assertTrue(observableList.contains(null));
53+
}
54+
55+
@Test
56+
public void testCreateObservableArrayListFromCollection() {
57+
List<String> list = Arrays.asList("1", "2", null);
58+
ObservableList<String> observableList = FXCollections.observableArrayList(list);
59+
60+
assertEquals(3, observableList.size());
61+
assertTrue(observableList.contains("1"));
62+
assertTrue(observableList.contains("2"));
63+
assertTrue(observableList.contains(null));
64+
}
65+
66+
@Test
67+
public void testCreateObservableArrayListWithNullCollection() {
68+
assertThrows(NullPointerException.class, () -> FXCollections.observableArrayList((Collection<Object>) null));
69+
}
70+
71+
@Test
72+
public void testCreateObservableArrayListWithNullArray() {
73+
assertThrows(NullPointerException.class, () -> FXCollections.observableArrayList((Object[]) null));
74+
}
75+
76+
@Test
77+
public void testCreateObservableArrayListDoesNotModifyOriginalCollection() {
78+
List<String> list = List.of("1", "2", "3");
79+
ObservableList<String> observableList = FXCollections.observableArrayList(list);
80+
observableList.add("4");
81+
82+
assertEquals(4, observableList.size());
83+
assertEquals(3, list.size());
84+
}
85+
4586
@Test
4687
@SuppressWarnings("unchecked")
4788
public void concatTest() {

0 commit comments

Comments
 (0)