1 package net.obsearch.example.ted;
2
3
4
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.io.IOException;
8
9 import net.obsearch.example.SliceParseException;
10 import net.obsearch.exception.OBException;
11 import net.obsearch.ob.OBShort;
12
13
14
15
16 public class OBTed implements OBShort {
17
18
19
20
21
22 protected SliceForest tree;
23 private int hashCode;
24
25
26
27
28
29 public OBTed() {
30
31 }
32
33
34
35
36
37
38 public OBTed(final String slice) throws OBException {
39 this.updateTree(slice);
40 assert tree.equalsTree( this.parseTree(tree.toFuriaChanTree())) : "This: " + tree.toFuriaChanTree() + " slice: " + slice + " size: " + tree.getSize();
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54 public final short distance(final OBShort object) throws OBException {
55 OBTed b = (OBTed) object;
56 return distance(b.tree, this.tree);
57 }
58
59
60
61
62
63
64
65
66
67 private final short distance(SliceForest a, SliceForest b) {
68
69 DMRW t = new DMRW();
70 return (short)t.ted(a, b);
71 }
72
73
74
75
76
77 protected final void updateTree(String x) throws OBException {
78 tree = parseTree(x);
79 this.hashCode = tree.toFuriaChanTree().hashCode();
80 }
81
82 private final SliceForest parseTree(String x) throws SliceParseException {
83 try {
84
85
86 return SliceFactory.createSliceForest(x);
87 } catch (Exception e) {
88 throw new SliceParseException(x, e);
89 }
90 }
91
92
93
94
95
96
97
98
99
100 public final int size() throws OBException {
101 return tree.getSize();
102 }
103
104
105
106
107 public final String toString() {
108 String res = ":)";
109 try {
110 res = tree.toFuriaChanTree() + "|" + tree.getSize() + "|";
111 } catch (Exception e) {
112 assert false;
113 }
114 return res;
115 }
116
117
118
119
120
121
122
123 public final void load(byte[] in) throws OBException, IOException {
124
125 updateTree(new String(in));
126 }
127
128
129
130
131
132
133
134 public final byte[] store() throws IOException{
135 return tree.toFuriaChanTree().getBytes();
136 }
137
138
139
140
141
142
143
144
145 public final boolean equals(final Object obj) {
146 return this.tree.equalsTree(((OBTed) obj).tree);
147 }
148
149
150
151
152
153 public final int hashCode() {
154 return this.hashCode;
155 }
156
157 }