aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/session/cache/CacheNode.java
blob: 7b584f81a0c01c9e998a1a2bb722a1fea8e83e0a (plain)
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
package org.traccar.session.cache;

import org.traccar.model.BaseModel;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

public class CacheNode {

    private BaseModel value;

    private final Map<Class<? extends BaseModel>, Set<CacheNode>> links = new HashMap<>();
    private final Map<Class<? extends BaseModel>, Set<CacheNode>> backlinks = new HashMap<>();

    public CacheNode(BaseModel value) {
        this.value = value;
    }

    public BaseModel getValue() {
        return value;
    }

    public void setValue(BaseModel value) {
        this.value = value;
    }

    public Set<CacheNode> getLinks(Class<? extends BaseModel> clazz, boolean forward) {
        var map = forward ? links : backlinks;
        return map.computeIfAbsent(clazz, k -> new HashSet<>());
    }

    public Stream<CacheNode> getAllLinks(boolean forward) {
        var map = forward ? links : backlinks;
        return map.values().stream().flatMap(Set::stream);
    }

}