Skip to content

Commit 02a3642

Browse files
committed
add ListFilter
1 parent 9478339 commit 02a3642

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.isolpro.custom;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public abstract class ListFilter<T> {
7+
8+
private final Callback<List<T>> onFilterChanged;
9+
private List<T> items, filteredItems;
10+
11+
public ListFilter(Callback<List<T>> onFilterChanged) {
12+
this.onFilterChanged = onFilterChanged;
13+
}
14+
15+
public void setItems(List<T> items) {
16+
this.items = items;
17+
18+
notifyFilterChanged();
19+
}
20+
21+
protected abstract boolean compare(T item);
22+
23+
public void clearFilters() {
24+
notifyFilterChanged();
25+
}
26+
27+
private void filter() {
28+
filteredItems = new ArrayList<>();
29+
30+
if (items == null) return;
31+
32+
for (T item : items) {
33+
if (compare(item))
34+
filteredItems.add(item);
35+
}
36+
}
37+
38+
protected void notifyFilterChanged() {
39+
filter();
40+
41+
if (onFilterChanged != null)
42+
onFilterChanged.exec(filteredItems);
43+
}
44+
45+
public List<T> getFilteredItems() {
46+
return filteredItems;
47+
}
48+
}

0 commit comments

Comments
 (0)