WordsElementsFactory.java
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Decompiled with CFR 0_118.
*/
package com.day.util.diff;
import com.day.util.diff.Document;
import com.day.util.diff.DocumentSource;
import com.day.util.diff.ElementsFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
public class WordsElementsFactory
implements ElementsFactory {
static final String CVS_ID = "$URL$ $Rev$ $Date$";
private static final int MAX_ELEMENTS = 100000;
private final Document.Element[] elements;
public WordsElementsFactory(Document.Element[] elements) {
this.elements = elements;
}
public Document.Element[] getElements() {
return this.elements;
}
public static WordsElementsFactory create(DocumentSource source, String text) {
try {
StringReader reader = new StringReader(text);
return WordsElementsFactory.create(source, reader);
}
catch (IOException e) {
throw new IllegalArgumentException(e.toString());
}
}
public static WordsElementsFactory create(DocumentSource source, Reader text) throws IOException {
Document.Element[] elements = WordsElementsFactory.getElements(source, text);
return new WordsElementsFactory(elements);
}
private static Document.Element[] getElements(DocumentSource source, Reader text) throws IOException {
int c;
BufferedReader r = text instanceof BufferedReader ? (BufferedReader)text : new BufferedReader(text);
ArrayList<WordElement> lines = new ArrayList<WordElement>();
StringBuffer gutter = new StringBuffer();
StringBuffer word = new StringBuffer();
while ((c = r.read()) >= 0 && lines.size() < 100000) {
if (Character.isLetterOrDigit((char)c)) {
if (gutter.length() > 0) {
lines.add(new WordElement(source, word.toString(), gutter.toString()));
gutter.setLength(0);
word.setLength(0);
}
word.append((char)c);
continue;
}
gutter.append((char)c);
}
if (word.length() > 0) {
lines.add(new WordElement(source, word.toString(), gutter.toString()));
}
return lines.toArray(new WordElement[lines.size()]);
}
public static class WordElement
implements Document.Element {
private final DocumentSource source;
private final String word;
private final String gutter;
public WordElement(DocumentSource source, String word, String gutter) {
this.source = source;
this.word = word;
this.gutter = gutter;
}
public String getString() {
return this.word + this.gutter;
}
public DocumentSource getDocumentSource() {
return this.source;
}
public int hashCode() {
return this.word.hashCode();
}
public String toString() {
return this.getString();
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof WordElement) {
return ((WordElement)obj).word.equals(this.word);
}
return false;
}
}
}