Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8283346: Optimize observable ArrayList creation in FXCollections
Reviewed-by: mstrauss, nlisker, kcr
  • Loading branch information
Marius Hanl authored and kevinrushforth committed Jul 2, 2022
1 parent 1f62570 commit b3eca1f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -347,9 +347,7 @@ public static <E> ObservableList<E> observableArrayList(Callback<E, Observable[]
* @see #observableArrayList()
*/
public static <E> ObservableList<E> observableArrayList(E... items) {
ObservableList<E> list = observableArrayList();
list.addAll(items);
return list;
return observableList(new ArrayList<>(Arrays.asList(items)));
}

/**
Expand All @@ -360,9 +358,7 @@ public static <E> ObservableList<E> observableArrayList(E... items) {
* @return a newly created observableArrayList
*/
public static <E> ObservableList<E> observableArrayList(Collection<? extends E> col) {
ObservableList<E> list = observableArrayList();
list.addAll(col);
return list;
return observableList(new ArrayList<>(col));
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -42,6 +42,47 @@

public class FXCollectionsTest {

@Test
public void testCreateObservableArrayListFromArray() {
ObservableList<String> observableList = FXCollections.observableArrayList("1", "2", null);

assertEquals(3, observableList.size());
assertTrue(observableList.contains("1"));
assertTrue(observableList.contains("2"));
assertTrue(observableList.contains(null));
}

@Test
public void testCreateObservableArrayListFromCollection() {
List<String> list = Arrays.asList("1", "2", null);
ObservableList<String> observableList = FXCollections.observableArrayList(list);

assertEquals(3, observableList.size());
assertTrue(observableList.contains("1"));
assertTrue(observableList.contains("2"));
assertTrue(observableList.contains(null));
}

@Test
public void testCreateObservableArrayListWithNullCollection() {
assertThrows(NullPointerException.class, () -> FXCollections.observableArrayList((Collection<Object>) null));
}

@Test
public void testCreateObservableArrayListWithNullArray() {
assertThrows(NullPointerException.class, () -> FXCollections.observableArrayList((Object[]) null));
}

@Test
public void testCreateObservableArrayListDoesNotModifyOriginalCollection() {
List<String> list = List.of("1", "2", "3");
ObservableList<String> observableList = FXCollections.observableArrayList(list);
observableList.add("4");

assertEquals(4, observableList.size());
assertEquals(3, list.size());
}

@Test
@SuppressWarnings("unchecked")
public void concatTest() {
Expand Down

1 comment on commit b3eca1f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.