1 package net.obsearch.example.lev;
2
3 import java.io.IOException;
4
5 import net.obsearch.asserts.OBAsserts;
6 import net.obsearch.exception.OBException;
7 import net.obsearch.ob.OBInt;
8 import net.obsearch.ob.OBShort;
9
10 public class OBString implements OBInt {
11
12 protected String str;
13
14 public OBString(){
15
16 }
17
18 public OBString(String x) throws OBException{
19
20 this.str = x;
21 }
22
23 public int length(){
24 return str.length();
25 }
26
27 @Override
28 public int distance(OBInt object) throws OBException {
29 OBString o = (OBString) object;
30 int d[][];
31 int n;
32 int m;
33 int i;
34 int j;
35 char s_i;
36 char t_j;
37 int cost;
38 String s = str;
39 String t = o.str;
40
41 n = s.length();
42 m = t.length();
43 if (n == 0) {
44 return (short)m;
45 }
46 if (m == 0) {
47 return (short)n;
48 }
49 d = new int[n+1][m+1];
50
51
52 for (i = 0; i <= n; i++) {
53 d[i][0] = i;
54 }
55
56 for (j = 0; j <= m; j++) {
57 d[0][j] = j;
58 }
59
60
61 for (i = 1; i <= n; i++) {
62
63 s_i = s.charAt(i - 1);
64
65 for (j = 1; j <= m; j++) {
66
67 t_j = t.charAt(j - 1);
68
69
70 if (s_i == t_j) {
71 cost = 0;
72 }
73 else {
74 cost = 1;
75 }
76
77
78 d[i][j] = min (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);
79
80 }
81
82 }
83
84
85 return d[n][m];
86 }
87
88 private int min (int a, int b, int c) {
89 int mi;
90
91 mi = a;
92 if (b < mi) {
93 mi = b;
94 }
95 if (c < mi) {
96 mi = c;
97 }
98 return mi;
99
100 }
101
102 @Override
103 public boolean equals(Object o){
104 return str.equals(((OBString)o).str);
105 }
106
107
108
109 @Override
110 public int hashCode() {
111 return str.hashCode();
112 }
113
114 @Override
115 public void load(byte[] input) throws OBException, IOException {
116 str = new String(input);
117
118 }
119
120 @Override
121 public byte[] store() throws OBException, IOException {
122 return str.getBytes();
123 }
124
125 public String toString(){
126 return str;
127 }
128 }