-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringTextFile.java
95 lines (79 loc) · 2.35 KB
/
StringTextFile.java
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
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.document;
import java.io.StringReader;
import java.util.Objects;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.util.datasource.DataSource;
import net.sourceforge.pmd.util.datasource.internal.ReaderDataSourceWithLanguage;
/**
* Read-only view on a string.
*
* @author Clément Fournier
*/
@Experimental
class StringTextFile implements TextFile {
private final String content;
private final String pathId;
private final String displayName;
private final LanguageVersion languageVersion;
StringTextFile(String content,
String pathId,
String displayName,
LanguageVersion languageVersion) {
AssertionUtil.requireParamNotNull("source text", content);
AssertionUtil.requireParamNotNull("file name", displayName);
AssertionUtil.requireParamNotNull("file ID", pathId);
AssertionUtil.requireParamNotNull("language version", languageVersion);
this.languageVersion = languageVersion;
this.content = content;
this.pathId = pathId;
this.displayName = displayName;
}
@Override
public LanguageVersion getLanguageVersion() {
return languageVersion;
}
@Override
public String getDisplayName() {
return displayName;
}
@Override
public String getPathId() {
return pathId;
}
@Override
public String readContents() {
return content;
}
@Override
public DataSource toDataSourceCompat() {
return new ReaderDataSourceWithLanguage(
new StringReader(content),
pathId,
languageVersion
);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StringTextFile that = (StringTextFile) o;
return Objects.equals(pathId, that.pathId);
}
@Override
public int hashCode() {
return Objects.hash(pathId);
}
@Override
public String toString() {
return getPathId();
}
}