Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#281 fix bug in ListTransformation #283

Merged
merged 1 commit into from
Jul 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void onChanged(
// targetList
List<TargetType> deleteStaging = new ArrayList<>();


while (listEvent.next()) {
if (listEvent.wasUpdated()) {
processUpdateEvent(listEvent);
Expand Down Expand Up @@ -118,10 +119,13 @@ public void onChanged(
*/
private void processAddEvent(
ListChangeListener.Change<? extends SourceType> listEvent) {
for (int i = listEvent.getFrom(); i < listEvent.getTo(); i++) {
SourceType item = listEvent.getList().get(i);
viewModelList.add(i, ListTransformation.this.function.apply(item));

final List<TargetType> toAdd = new ArrayList<>();
for (int index = listEvent.getFrom(); index < listEvent.getTo(); index++) {
final SourceType item = listEvent.getList().get(index);
toAdd.add(function.apply(item));
}
viewModelList.addAll(listEvent.getFrom(), toAdd);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
package de.saxsys.mvvmfx.utils.itemlist;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ItemList}.
*
Expand Down Expand Up @@ -143,5 +150,36 @@ public void replaceItemInItemListAtIndex() {
Assert.assertEquals(PREFIX + "replacedPerson", itemList
.stringListProperty().get(1));
}


/**
* This test is used to reproduce the bug <a href="https://github.com/sialcasa/mvvmFX/issues/281">#281</a>.
*
* When multiple elements are added to the list in a single method call ({@link ObservableList#addAll(Object[])},
* only a single change event should be fired.
*/
@Test
public void addMultipleItemsEventListener() {
AtomicInteger counter = new AtomicInteger(0);

itemList.getTargetList().addListener((ListChangeListener<String>) c -> {
counter.incrementAndGet();
});

listWithModelObjects.addAll(new Person("one"), new Person("two"), new Person("three"));

assertThat(counter.get()).isEqualTo(1);
}

@Test
public void removeMultipleItemsEventListener() {
AtomicInteger counter = new AtomicInteger(0);

itemList.getTargetList().addListener((ListChangeListener<String>) c -> {
counter.incrementAndGet();
});

listWithModelObjects.removeAll(person1, person3);

assertThat(counter.get()).isEqualTo(1);
}
}