package com.voxelgameslib.voxelgameslib.components.scoreboard;

import com.voxelgameslib.voxelgameslib.exception.ScoreboardException;
import com.voxelgameslib.voxelgameslib.user.User;

import javax.annotation.Nonnull;
import java.util.*;

/**
 * Abstract implementation of a scoreboard, implements some limited functionality
 */
public abstract class AbstractScoreboard implements Scoreboard {

    private List<User> users = new ArrayList<>();
    private Map<Integer, ScoreboardLine> lines = new HashMap<>();
    private Map<String, Integer> keys = new HashMap<>();
    private int nextPos = 1;

    @Override
    public void addLine(int key, @Nonnull ScoreboardLine line) {
        lines.put(key, line);
    }

    @Override
    public int addLine(@Nonnull ScoreboardLine line) {
        int pos = nextPos++;
        while (getLine(pos).isPresent()) {
            pos = nextPos++;
        }
        addLine(pos, line);
        return pos;
    }

    @Override
    public int addLine(String key, @Nonnull ScoreboardLine line) {
        int pos = addLine(line);
        keys.put(key, pos);
        return pos;
    }

    @Override
    public void removeLine(@Nonnull String key) {
        Integer pos = keys.get(key);
        if (pos == null) {
            throw new ScoreboardException("Can't remove line with unknown key " + key);
        }

        removeLine(pos);
    }

    @Override
    public void removeLine(int key) {
        lines.remove(key);
    }

    @Override
    public void removeAllLines() {
        new HashSet<>(lines.keySet()).forEach(this::removeLine);
    }

    @Nonnull
    @Override
    public Optional<ScoreboardLine> getLine(int key) {
        return Optional.ofNullable(lines.get(key));
    }

    @Nonnull
    @Override
    public Optional<ScoreboardLine> getLine(@Nonnull String key) {
        Integer pos = keys.get(key);
        if (pos == null) {
            return Optional.empty();
        }

        return getLine(pos);
    }

    @Override
    public void addUser(@Nonnull User user) {
        if (!isAdded(user)) {
            users.add(user);
        }
    }

    @Override
    public void removeUser(@Nonnull User user) {
        users.remove(user);
    }

    @Override
    public boolean isAdded(@Nonnull User user) {
        return users.contains(user);
    }

    @Nonnull
    @Override
    public List<User> getUsers() {
        return users;
    }

    @Override
    public void removeAllUsers() {
        new ArrayList<>(users).forEach(this::removeUser);
    }
}

@@ -189,16 +189,168 @@
 ort java
+.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Optional;%0Aimport java
 x.annota
@@ -365,28 +365,8 @@
 null
-;%0Aimport java.util.*
 ;%0A%0A/

reformat (you really should not click on this if you love your browser)