-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ListUtils class and refactor list update operations
A new utility class, ListUtils, has been added to handle list update operations. The existing code in MultiColumnListView has been refactored to use this class for updating list elements based on certain conditions. This commit also updates the project version from 2.17.0 to 2.18.0.
- Loading branch information
Showing
5 changed files
with
71 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.dlsc.gemsfx.util; | ||
|
||
import java.util.List; | ||
import java.util.ListIterator; | ||
import java.util.function.Predicate; | ||
|
||
public class ListUtils { | ||
|
||
/** | ||
* Update matching elements in the list with the provided new value. | ||
* | ||
* @param list the list to operate on | ||
* @param matchPredicate the predicate to match elements | ||
* @param newValue the new value to replace matching elements | ||
* @param breakAfterFirstMatch whether to break after the first match | ||
* @param <T> the type of elements in the list | ||
* @return the number of elements updated | ||
*/ | ||
public static <T> int updateMatching(List<T> list, Predicate<T> matchPredicate, T newValue, boolean breakAfterFirstMatch) { | ||
int updateCount = 0; | ||
ListIterator<T> iterator = list.listIterator(); | ||
while (iterator.hasNext()) { | ||
T currentElement = iterator.next(); | ||
if (matchPredicate.test(currentElement)) { | ||
iterator.set(newValue); | ||
updateCount++; | ||
if (breakAfterFirstMatch) { | ||
break; | ||
} | ||
} | ||
} | ||
return updateCount; | ||
} | ||
|
||
/** | ||
* Update the first matching element in the list with the provided new value. | ||
* | ||
* @param list the list to operate on | ||
* @param matchPredicate the predicate to match elements | ||
* @param newValue the new value to replace matching elements | ||
* @param <T> the type of elements in the list | ||
* @return the number of elements updated | ||
*/ | ||
public static <T> int updateFirstMatching(List<T> list, Predicate<T> matchPredicate, T newValue) { | ||
return updateMatching(list, matchPredicate, newValue, true); | ||
} | ||
|
||
/** | ||
* Update all matching elements in the list with the provided new value. | ||
* | ||
* @param list the list to operate on | ||
* @param matchPredicate the predicate to match elements | ||
* @param newValue the new value to replace matching elements | ||
* @param <T> the type of elements in the list | ||
* @return the number of elements updated | ||
*/ | ||
public static <T> int updateAllMatching(List<T> list, Predicate<T> matchPredicate, T newValue) { | ||
return updateMatching(list, matchPredicate, newValue, false); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters