001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.subst;
015
016public class Node {
017
018    enum Type {
019        LITERAL, VARIABLE
020    }
021
022    Type type;
023    Object payload;
024    Object defaultPart;
025    Node next;
026
027    public Node(Type type, Object payload) {
028        this.type = type;
029        this.payload = payload;
030    }
031
032    public Node(Type type, Object payload, Object defaultPart) {
033        this.type = type;
034        this.payload = payload;
035        this.defaultPart = defaultPart;
036    }
037
038    void append(Node newNode) {
039        if (newNode == null)
040            return;
041        Node n = this;
042        while (true) {
043            if (n.next == null) {
044                n.next = newNode;
045                return;
046            }
047            n = n.next;
048        }
049    }
050
051    @Override
052    public String toString() {
053        switch (type) {
054        case LITERAL:
055            return "Node{" + "type=" + type + ", payload='" + payload + "'}";
056        case VARIABLE:
057            StringBuilder payloadBuf = new StringBuilder();
058            StringBuilder defaultPartBuf2 = new StringBuilder();
059            if (defaultPart != null)
060                recursive((Node) defaultPart, defaultPartBuf2);
061
062            recursive((Node) payload, payloadBuf);
063            String r = "Node{" + "type=" + type + ", payload='" + payloadBuf.toString() + "'";
064            if (defaultPart != null)
065                r += ", defaultPart=" + defaultPartBuf2.toString();
066            r += '}';
067            return r;
068        }
069        return null;
070    }
071
072    public void dump() {
073        System.out.print(this.toString());
074        System.out.print(" -> ");
075        if (next != null) {
076            next.dump();
077        } else {
078            System.out.print(" null");
079        }
080    }
081
082    void recursive(Node n, StringBuilder sb) {
083        Node c = n;
084        while (c != null) {
085            sb.append(c.toString()).append(" --> ");
086            c = c.next;
087        }
088        sb.append("null ");
089    }
090
091    public void setNext(Node n) {
092        this.next = n;
093    }
094
095    @Override
096    public boolean equals(Object o) {
097        if (this == o)
098            return true;
099        if (o == null || getClass() != o.getClass())
100            return false;
101
102        Node node = (Node) o;
103
104        if (type != node.type)
105            return false;
106        if (payload != null ? !payload.equals(node.payload) : node.payload != null)
107            return false;
108        if (defaultPart != null ? !defaultPart.equals(node.defaultPart) : node.defaultPart != null)
109            return false;
110        if (next != null ? !next.equals(node.next) : node.next != null)
111            return false;
112
113        return true;
114    }
115
116    @Override
117    public int hashCode() {
118        int result = type != null ? type.hashCode() : 0;
119        result = 31 * result + (payload != null ? payload.hashCode() : 0);
120        result = 31 * result + (defaultPart != null ? defaultPart.hashCode() : 0);
121        result = 31 * result + (next != null ? next.hashCode() : 0);
122        return result;
123    }
124}