All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.luan.console.parser.Aliases Maven / Gradle / Ivy

package xyz.luan.console.parser;

import java.util.HashMap;
import java.util.Map;

import xyz.luan.console.parser.call.CallResult;

public class Aliases {
    private Map aliases;
    private boolean enableDefaultAliases;

    public static Aliases createAliasesWithDefaults() {
        return new Aliases(new HashMap<>(), true);
    }

    public static Aliases createAliasesWithoutDefaults(Map map) {
        return new Aliases(map, false);
    }

    public Aliases(Map aliases, boolean enableDefaultAliases) {
        this.aliases = aliases;
        this.enableDefaultAliases = enableDefaultAliases;
    }

    public boolean add(String alias, String keyword) {
        if (this.aliases.get(alias) != null) {
            return false;
        }
        this.aliases.put(alias, keyword);
        return true;
    }

    public boolean remove(String alias) {
        if (this.aliases.get(alias) == null) {
            return false;
        }
        this.aliases.remove(alias);
        return true;
    }

    public String get(String alias) {
        String res = this.aliases.get(alias);
        if (this.enableDefaultAliases && res == null) {
            return ':' + alias;
        }
        return res;
    }

    public CallResult list(Console console) {
        return listFor(console, null);
    }

    public CallResult listFor(Console console, String keyword) {
        boolean printed = false;
        for (Map.Entry entry : aliases.entrySet()) {
            if (keyword == null) {
                console.result(entry.getKey() + ": " + entry.getValue().substring(1));
                printed = true;
            } else {
                if (keyword.equals(entry.getValue())) {
                    console.result(entry.getKey());
                    printed = true;
                }
            }
        }

        if (!printed) {
            console.result("No aliases found.");
        }

        return CallResult.SUCCESS;
    }

    public void setDefaultAliases(boolean enableDefaultAliases) {
        this.enableDefaultAliases = enableDefaultAliases;
    }

    public boolean isDefaultAliasesEnabled() {
        return enableDefaultAliases;
    }

    public void removeAll() {
        this.aliases.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy