Coverage Summary for Class: ConnectionPointFinder (co.rsk.net.sync)
Class |
Class, %
|
Method, %
|
Line, %
|
ConnectionPointFinder |
0%
(0/1)
|
0%
(0/7)
|
0%
(0/13)
|
1 package co.rsk.net.sync;
2
3 import com.google.common.annotations.VisibleForTesting;
4
5 import java.util.Optional;
6
7 /**
8 * Uses Binary Search to help find a connection point with another peer.
9 */
10 public class ConnectionPointFinder {
11
12 private long start;
13 private long end;
14
15 // Connection point found or not
16 private Long connectionPoint = null;
17
18 public ConnectionPointFinder(long fromHeight, long toHeight) {
19 this.start = fromHeight;
20 this.end = toHeight;
21 }
22
23 public Optional<Long> getConnectionPoint() {
24 return Optional.ofNullable(this.connectionPoint);
25 }
26
27 public long getFindingHeight() {
28 // this is implemented like this to avoid overflow problems
29 return this.start + (this.end - this.start) / 2;
30 }
31
32 public void updateFound() {
33 this.start = getFindingHeight();
34 trySettingConnectionPoint();
35 }
36
37 public void updateNotFound() {
38 this.end = getFindingHeight();
39 trySettingConnectionPoint();
40 }
41
42 private void trySettingConnectionPoint() {
43 if (this.end - this.start <= 1) {
44 this.setConnectionPoint(this.start);
45 }
46 }
47
48 @VisibleForTesting
49 public void setConnectionPoint(long height) {
50 this.connectionPoint = height;
51 }
52 }