Skip to content

Commit

Permalink
Added toMutableList collector to mark the need for a mutable list
Browse files Browse the repository at this point in the history
It's useful in cases where we need to modify the resulting list, which
is not possible with the immutable list returned by the Stream#toList
method. We use this method to differentiate between cases where a
mutable list is necessary and to prevent the IDE from suggesting the use
of Stream#toList.

Issue: #3197
  • Loading branch information
buchen committed Feb 25, 2023
1 parent eae0262 commit 4cb0be6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
@@ -1,5 +1,7 @@
package name.abuchen.portfolio.ui.views.dashboard;

import static name.abuchen.portfolio.util.CollectorsUtil.toMutableList;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -11,7 +13,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
Expand Down Expand Up @@ -879,7 +880,7 @@ private void duplicateColumn(Dashboard.Column column, Composite columnControl)
Dashboard.Column newColumn = new Dashboard.Column();
dashboard.getColumns().add(index, newColumn);

newColumn.setWidgets(column.getWidgets().stream().map(Widget::copy).collect(Collectors.toList()));
newColumn.setWidgets(column.getWidgets().stream().map(Widget::copy).collect(toMutableList()));

getClient().touch();

Expand Down
@@ -0,0 +1,25 @@
package name.abuchen.portfolio.util;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class CollectorsUtil
{
private CollectorsUtil()
{
}

/**
* This method returns a collector that gathers the elements into a mutable
* ArrayList. It's useful in cases where we need to modify the resulting
* list, which is not possible with the immutable list returned by the
* Stream#toList method. We use this method to differentiate between cases
* where a mutable list is necessary and to prevent the IDE from suggesting
* the use of Stream#toList.
*/
public static <T> Collector<T, ?, List<T>> toMutableList()
{
return Collectors.toList();
}
}

0 comments on commit 4cb0be6

Please sign in to comment.