1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.qedeq.kernel.se.dto.list;
17
18 import org.qedeq.kernel.se.base.list.Atom;
19 import org.qedeq.kernel.se.base.list.Element;
20 import org.qedeq.kernel.se.base.list.ElementList;
21
22
23
24
25
26
27
28 public final class DefaultAtom implements Atom {
29
30
31 private final String text;
32
33
34
35
36
37
38
39 public DefaultAtom(final String text) {
40 if (text == null) {
41 throw new IllegalArgumentException("a NullPointer is no valid text");
42 }
43 this.text = text;
44 }
45
46 public final String getString() {
47 return text;
48 }
49
50 public final boolean isAtom() {
51 return true;
52 }
53
54 public final Atom getAtom() {
55 return this;
56 }
57
58 public final boolean isList() {
59 return false;
60 }
61
62 public final ElementList getList() {
63 throw new ClassCastException("this is no " + ElementList.class.getName()
64 + ", but a " + this.getClass().getName());
65 }
66
67 public final Element copy() {
68 return new DefaultAtom(text);
69 }
70
71 public final Element replace(final Element search, final Element replacement) {
72 if (this.equals(search)) {
73 return replacement.copy();
74 }
75 return this.copy();
76 }
77
78 public final String toString() {
79 StringBuffer result = new StringBuffer();
80 result.append("\"");
81
82 for (int i = 0; i < text.length(); i++) {
83 if (text.charAt(i) == '\"') {
84 result.append("\"\"");
85 } else {
86 result.append(text.charAt(i));
87 }
88 }
89 result.append('\"');
90 return result.toString();
91
92 }
93
94 public final boolean equals(final Object object) {
95 if (object instanceof DefaultAtom) {
96 return ((DefaultAtom) object).text.equals(text);
97 }
98 return false;
99 }
100
101 public final int hashCode() {
102 return toString().hashCode();
103 }
104
105 }