-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageRankIndex.java
42 lines (32 loc) · 1.23 KB
/
PageRankIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.github.ivangomes.pagerank;
import lombok.ToString;
import java.util.*;
@ToString
public class PageRankIndex {
private final Map<String, Set<String>> outgoingLinksMap = new HashMap<>();
private final Map<String, Set<String>> incomingLinksMap = new HashMap<>();
private final Map<String, Double> pageRanks = new HashMap<>();
public void addPage(String id) {
outgoingLinksMap.computeIfAbsent(id, k -> new HashSet<>());
incomingLinksMap.computeIfAbsent(id, k -> new HashSet<>());
}
public void addLink(String sourceId, String targetId) {
if (sourceId.equals(targetId)) {
return;
}
outgoingLinksMap.computeIfAbsent(sourceId, k -> new HashSet<>()).add(targetId);
incomingLinksMap.computeIfAbsent(targetId, k -> new HashSet<>()).add(sourceId);
}
public Map<String, Set<String>> getOutgoingLinksMap() {
return Collections.unmodifiableMap(outgoingLinksMap);
}
public Map<String, Set<String>> getIncomingLinksMap() {
return Collections.unmodifiableMap(incomingLinksMap);
}
public Map<String, Double> getPageRanks() {
return pageRanks;
}
public int size() {
return outgoingLinksMap.size();
}
}