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

net.ripe.db.whois.common.Messages Maven / Gradle / Ivy

package net.ripe.db.whois.common;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

public final class Messages {
    private Map> messages = Maps.newEnumMap(Type.class);

    public Messages() {
    }

    public Messages(final Message... messages) {
        for (Message message : messages) {
            add(message);
        }
    }

    public void add(final Message message) {
        Set messageSet = messages.get(message.getType());
        if (messageSet == null) {
            messageSet = Sets.newLinkedHashSet();
            messages.put(message.getType(), messageSet);
        }

        messageSet.add(message);
    }

    public void remove(final Message message) {
        final Set messageSet = messages.get(message.getType());
        if (messageSet == null) {
            return;
        }

        messageSet.remove(message);
    }

    public void addAll(final Messages otherMessages) {
        for (final Map.Entry> otherEntry : otherMessages.messages.entrySet()) {
            final Type type = otherEntry.getKey();
            final Set messageSet = messages.get(type);
            final Set otherMessageSet = otherEntry.getValue();
            if (messageSet == null) {
                messages.put(type, otherMessageSet);
            } else {
                messageSet.addAll(otherMessageSet);
            }
        }
    }

    public Collection getAllMessages() {
        final Set result = Sets.newLinkedHashSet();

        for (final Map.Entry> messageEntry : messages.entrySet()) {
            result.addAll(messageEntry.getValue());
        }

        return result;
    }

    public Collection getMessages(final Type type) {
        final Set messageSet = messages.get(type);
        if (messageSet == null) {
            return Collections.emptyList();
        }

        return messageSet;
    }

    public boolean hasMessages() {
        for (final Set messageSet : messages.values()) {
            if (!messageSet.isEmpty()) {
                return true;
            }
        }

        return false;
    }

    public Collection getInfos() {
        return getMessages(Type.INFO);
    }

    public Collection getWarnings() {
        return getMessages(Type.WARNING);
    }

    public Collection getErrors() {
        return getMessages(Type.ERROR);
    }

    public enum Type {
        ERROR("Error"),
        WARNING("Warning"),
        INFO("Info");

        private final String string;

        private Type(final String string) {
            this.string = string;
        }

        @Override
        public String toString() {
            return string;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy