Coverage Summary for Class: Filter (org.ethereum.rpc)
Class |
Method, %
|
Line, %
|
Filter |
0%
(0/8)
|
0%
(0/20)
|
Filter$FilterEvent |
0%
(0/1)
|
0%
(0/1)
|
Total |
0%
(0/9)
|
0%
(0/21)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2018 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package org.ethereum.rpc;
20
21 import org.ethereum.core.Block;
22 import org.ethereum.core.Transaction;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28 /**
29 * Created by ajlopez on 17/01/2018.
30 */
31
32 public class Filter {
33 abstract static class FilterEvent {
34 public abstract Object getJsonEventObject();
35 }
36
37 private List<FilterEvent> events = new ArrayList<>();
38 private int processedEvents = 0;
39 private long accessTime = System.currentTimeMillis();
40
41 public boolean hasExpired(long timeout) {
42 long nowTime = System.currentTimeMillis();
43
44 return accessTime + timeout <= nowTime;
45 }
46
47 public synchronized Object[] getNewEvents() {
48 this.accessTime = System.currentTimeMillis();
49
50 Object[] ret = events.stream().skip(processedEvents).map(fe -> fe.getJsonEventObject()).collect(Collectors.toList()).toArray();
51
52 processedEvents = events.size();
53
54 return ret;
55 }
56
57 public synchronized void clearEvents() {
58 this.accessTime = System.currentTimeMillis();
59
60 events.clear();
61 processedEvents = 0;
62 }
63
64 public synchronized Object[] getEvents() {
65 this.accessTime = System.currentTimeMillis();
66
67 return events.stream().map(fe -> fe.getJsonEventObject()).collect(Collectors.toList()).toArray();
68 }
69
70 protected synchronized void add(FilterEvent evt) {
71 events.add(evt);
72 }
73
74 public void newBlockReceived(Block b) {
75 }
76
77 public void newPendingTx(Transaction tx) {
78 // add TransactionReceipt for PendingTx
79 }
80 }