1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
13  */

14 package ch.qos.logback.core.subst;
15
16 public class Node {
17
18     enum Type {
19         LITERAL, VARIABLE
20     }
21
22     Type type;
23     Object payload;
24     Object defaultPart;
25     Node next;
26
27     public Node(Type type, Object payload) {
28         this.type = type;
29         this.payload = payload;
30     }
31
32     public Node(Type type, Object payload, Object defaultPart) {
33         this.type = type;
34         this.payload = payload;
35         this.defaultPart = defaultPart;
36     }
37
38     void append(Node newNode) {
39         if (newNode == null)
40             return;
41         Node n = this;
42         while (true) {
43             if (n.next == null) {
44                 n.next = newNode;
45                 return;
46             }
47             n = n.next;
48         }
49     }
50
51     @Override
52     public String toString() {
53         switch (type) {
54         case LITERAL:
55             return "Node{" + "type=" + type + ", payload='" + payload + "'}";
56         case VARIABLE:
57             StringBuilder payloadBuf = new StringBuilder();
58             StringBuilder defaultPartBuf2 = new StringBuilder();
59             if (defaultPart != null)
60                 recursive((Node) defaultPart, defaultPartBuf2);
61
62             recursive((Node) payload, payloadBuf);
63             String r = "Node{" + "type=" + type + ", payload='" + payloadBuf.toString() + "'";
64             if (defaultPart != null)
65                 r += ", defaultPart=" + defaultPartBuf2.toString();
66             r += '}';
67             return r;
68         }
69         return null;
70     }
71
72     public void dump() {
73         System.out.print(this.toString());
74         System.out.print(" -> ");
75         if (next != null) {
76             next.dump();
77         } else {
78             System.out.print(null");
79         }
80     }
81
82     void recursive(Node n, StringBuilder sb) {
83         Node c = n;
84         while (c != null) {
85             sb.append(c.toString()).append(" --> ");
86             c = c.next;
87         }
88         sb.append("null ");
89     }
90
91     public void setNext(Node n) {
92         this.next = n;
93     }
94
95     @Override
96     public boolean equals(Object o) {
97         if (this == o)
98             return true;
99         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 }
125