summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/JSONLeagueDatabase.java
blob: 01f4fc39fe1a7a3ee006e195d2ce0d60a5a38351 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package design.persistence;

import design.model.League;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class JSONLeagueDatabase implements LeagueDatabase {

    private static JSONLeagueDatabase INSTANCE;

    public static JSONLeagueDatabase instance() {
        if (INSTANCE == null) {
            INSTANCE = new JSONLeagueDatabase("data/leaguedb.json");
        }
        return INSTANCE;
    }

    private final Map<Integer, League> cache;
    private final File file;

    public JSONLeagueDatabase(String filename) {
        this.file = new File(filename);
        this.cache = new HashMap<>();
    }

    private void load() {
        //TODO impl
    }

    private void save() {
        //TODO impl
    }

    @Override
    public League getLeague(int id) {
        return cache.get(id);
    }

    @Override
    public League[] getLeagues() {
        return cache.values().toArray(League[]::new);
    }

    @Override
    public void addLeague(League league) {
        cache.putIfAbsent(league.getId(), league);
        save();
    }

    @Override
    public void removeLeague(League league) {
        cache.remove(league.getId());
        save();
    }

    @Override
    public void updateLeague(League league) {
        cache.put(league.getId(), league);
        save();
    }
}