com.stehno.sanctuary.core.MessageSet.java Source code

Java tutorial

Introduction

Here is the source code for com.stehno.sanctuary.core.MessageSet.java

Source

/*
 * Copyright (c) 2011 Christopher J. Stehno
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.stehno.sanctuary.core;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author cjstehno
 */
public class MessageSet {

    private final String rootPath;
    private final List<MessageItem> messages = new LinkedList<MessageItem>();
    private final List<MessageItem> errors = new LinkedList<MessageItem>();

    public MessageSet(final String rootPath) {
        this.rootPath = rootPath;
    }

    public String getRootPath() {
        return rootPath;
    }

    public void addMessage(String path, String message) {
        messages.add(new MessageItem(path, message));
    }

    public void addError(String path, String error) {
        errors.add(new MessageItem(path, error));
    }

    public List<MessageItem> getErrors() {
        return Collections.unmodifiableList(errors);
    }

    public List<MessageItem> getMessages() {
        return Collections.unmodifiableList(messages);
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder("MessageSet:\n");

        for (MessageItem item : messages) {
            str.append(" MSG: ").append(item.path).append(" -> ").append(item.message).append('\n');
        }

        for (MessageItem item : errors) {
            str.append(" ERR: ").append(item.path).append(" -> ").append(item.message).append('\n');
        }

        return str.toString();
    }

    public static class MessageItem {

        String path;
        String message;

        MessageItem(String path, String message) {
            this.path = path;
            this.message = message;
        }

        @Override
        public String toString() {
            return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("path", path)
                    .append("message", message).toString();
        }
    }
}