11package me .hsgamer .topper .agent .snapshot ;
22
33import me .hsgamer .topper .agent .core .Agent ;
4- import me .hsgamer .topper .data .core .DataHolder ;
54
65import java .util .*;
76import java .util .concurrent .atomic .AtomicReference ;
8- import java .util .function .Predicate ;
97import java .util .stream .Collectors ;
10- import java .util .stream .IntStream ;
118import java .util .stream .Stream ;
129
1310public abstract class SnapshotAgent <K , V > implements Agent , Runnable {
14- private final AtomicReference <List <Map .Entry <K , V >>> entryList = new AtomicReference <>(null );
15- private final AtomicReference <Snapshot > snapshot = new AtomicReference <>(new Snapshot ());
16- private Predicate <Map .Entry <K , V >> filter = null ;
11+ private final AtomicReference <Snapshot <K , V >> snapshot = new AtomicReference <>(null );
1712 private Comparator <V > comparator ;
1813
19- public static <K , V > SnapshotAgent <K , V > create (DataHolder <K , V > holder ) {
20- return new SnapshotAgent <K , V >() {
21- @ Override
22- protected Stream <Map .Entry <K , V >> getDataStream () {
23- return holder .getEntryMap ()
24- .entrySet ()
25- .stream ()
26- .map (entry -> new AbstractMap .SimpleImmutableEntry <>(entry .getKey (), entry .getValue ().getValue ()));
27- }
28-
29- @ Override
30- protected boolean needUpdate () {
31- return true ;
32- }
33- };
34- }
35-
3614 protected abstract Stream <Map .Entry <K , V >> getDataStream ();
3715
3816 protected abstract boolean needUpdate ();
3917
4018 @ Override
4119 public void run () {
42- boolean isUpdate = needUpdate ();
43- List <Map .Entry <K , V >> currentEntryList = entryList .get ();
44- if (filter == null && !isUpdate && currentEntryList != null ) {
20+ Snapshot <K , V > currentSnapshot = snapshot .get ();
21+ if (currentSnapshot != null && !needUpdate ()) {
4522 return ;
4623 }
4724
48- final List <Map .Entry <K , V >> list ;
49- if (isUpdate || currentEntryList == null ) {
50- list = getUrgentSnapshot ();
51- entryList .set (list );
52- } else {
53- list = currentEntryList ;
54- }
55-
56- final List <Map .Entry <K , V >> resultList ;
57- if (filter != null ) {
58- resultList = list .stream ().filter (filter ).collect (Collectors .toList ());
59- } else {
60- resultList = list ;
25+ List <Map .Entry <K , V >> list = getUrgentSnapshot ();
26+ Map <K , Integer > map = new HashMap <>(list .size ());
27+ for (int i = 0 ; i < list .size (); i ++) {
28+ map .put (list .get (i ).getKey (), i );
6129 }
62-
63- Map <K , Integer > map = IntStream .range (0 , resultList .size ())
64- .boxed ()
65- .collect (Collectors .toMap (i -> resultList .get (i ).getKey (), i -> i ));
66- snapshot .set (new Snapshot (resultList , map ));
30+ snapshot .set (new Snapshot <>(list , map ));
6731 }
6832
6933 @ Override
7034 public void stop () {
71- snapshot .set (new Snapshot () );
35+ snapshot .set (null );
7236 }
7337
7438 public List <Map .Entry <K , V >> getUrgentSnapshot () {
@@ -80,11 +44,13 @@ public List<Map.Entry<K, V>> getUrgentSnapshot() {
8044 }
8145
8246 public List <Map .Entry <K , V >> getSnapshot () {
83- return snapshot .get ().entryList ;
47+ Snapshot <K , V > s = snapshot .get ();
48+ return s == null ? Collections .emptyList () : s .entryList ;
8449 }
8550
8651 public int getSnapshotIndex (K key ) {
87- return snapshot .get ().indexMap .getOrDefault (key , -1 );
52+ Snapshot <K , V > s = snapshot .get ();
53+ return s == null ? -1 : s .indexMap .getOrDefault (key , -1 );
8854 }
8955
9056 public Optional <Map .Entry <K , V >> getSnapshotByIndex (int index ) {
@@ -97,21 +63,13 @@ public void setComparator(Comparator<V> comparator) {
9763 this .comparator = comparator ;
9864 }
9965
100- public void setFilter (Predicate <Map .Entry <K , V >> filter ) {
101- this .filter = filter ;
102- }
103-
104- private final class Snapshot {
66+ private static final class Snapshot <K , V > {
10567 private final List <Map .Entry <K , V >> entryList ;
10668 private final Map <K , Integer > indexMap ;
10769
10870 private Snapshot (List <Map .Entry <K , V >> entryList , Map <K , Integer > indexMap ) {
10971 this .entryList = entryList ;
11072 this .indexMap = indexMap ;
11173 }
112-
113- private Snapshot () {
114- this (Collections .emptyList (), Collections .emptyMap ());
115- }
11674 }
11775}
0 commit comments