brainleg.app.engine.EData.java Source code

Java tutorial

Introduction

Here is the source code for brainleg.app.engine.EData.java

Source

/*
 * Copyright 2011 Roman Stepanenko
 *
 * 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 brainleg.app.engine;

import brainleg.app.util.AppUtil;
import org.apache.commons.lang.SerializationUtils;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * A structure containing headers and traces of the exception.
 * Can also be linked to another EData, typically the nested cause
 * of this one.
 * @author Roman Stepanenko
 */
public class EData implements Serializable {
    public void addHeader(ELine header) {
        headers.add(header);
        header.parent = this;
    }

    public EData deepCopy() {
        return (EData) AppUtil.deserialize(SerializationUtils.serialize(this)); //quick way of doing deep copy
    }

    public void addTrace(ETrace trace) {
        traces.add(trace);
        trace.parent = this;
    }

    public void setNext(EData next) {
        this.next = next;
        if (next != null) {
            next.previous = this;
        }
    }

    public int getHeaderCount() {
        return headers.size();
    }

    public int getTraceCount() {
        return traces.size();
    }

    public int getRecursiveTraceCount() {
        int count = getTraceCount();
        if (next != null) {
            count = count + next.getRecursiveTraceCount();
        }
        return count;
    }

    public void acceptRecursively(EDataVisitor visitor) {
        visitor.visit(this);
        if (next != null) {
            next.acceptRecursively(visitor);
        }
    }

    public EData getPrevious() {
        return previous;
    }

    public List<ELine> getHeaders() {
        return headers;
    }

    public List<ETrace> getTraces() {
        return traces;
    }

    /**
     * The leaf (bottom) exception in the chain, i.e. the CausedBy
     * @return
     */
    public EData getLeaf() {
        if (next == null) {
            return this;
        } else {
            return next.getLeaf();
        }
    }

    @Override
    public String toString() {
        return "EData{" + "headers=" + headers + ", traces=" + traces + ", next=" + next + '}';
    }

    private List<ELine> headers = new LinkedList<ELine>();
    private List<ETrace> traces = new LinkedList<ETrace>();
    private EData next;
    private EData previous;
}