Skip to content

Commit ffe7a0c

Browse files
authored
Use records for FileData implementations (#122658) (#122771)
This commit switches to using records in place of classes for FileData impls. It moves them outside the interface so that they can be private. This does not change the fact the interface is sealed, nor the factory methods for creating them.
1 parent 595d507 commit ffe7a0c

File tree

1 file changed

+23
-78
lines changed
  • libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements

1 file changed

+23
-78
lines changed

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlement.java

Lines changed: 23 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -41,83 +41,9 @@ public enum BaseDir {
4141

4242
public sealed interface FileData {
4343

44-
final class AbsolutePathFileData implements FileData {
45-
private final Path path;
46-
private final Mode mode;
47-
48-
private AbsolutePathFileData(Path path, Mode mode) {
49-
this.path = path;
50-
this.mode = mode;
51-
}
52-
53-
@Override
54-
public Stream<Path> resolvePaths(PathLookup pathLookup) {
55-
return Stream.of(path);
56-
}
57-
58-
@Override
59-
public Mode mode() {
60-
return mode;
61-
}
62-
63-
@Override
64-
public boolean equals(Object obj) {
65-
if (obj == this) return true;
66-
if (obj == null || obj.getClass() != this.getClass()) return false;
67-
var that = (AbsolutePathFileData) obj;
68-
return Objects.equals(this.path, that.path) && Objects.equals(this.mode, that.mode);
69-
}
70-
71-
@Override
72-
public int hashCode() {
73-
return Objects.hash(path, mode);
74-
}
75-
}
76-
77-
final class RelativePathFileData implements FileData {
78-
private final Path relativePath;
79-
private final BaseDir baseDir;
80-
private final Mode mode;
81-
82-
private RelativePathFileData(Path relativePath, BaseDir baseDir, Mode mode) {
83-
this.relativePath = relativePath;
84-
this.baseDir = baseDir;
85-
this.mode = mode;
86-
}
87-
88-
@Override
89-
public Stream<Path> resolvePaths(PathLookup pathLookup) {
90-
Objects.requireNonNull(pathLookup);
91-
switch (baseDir) {
92-
case CONFIG:
93-
return Stream.of(pathLookup.configDir().resolve(relativePath));
94-
case DATA:
95-
return Arrays.stream(pathLookup.dataDirs()).map(d -> d.resolve(relativePath));
96-
default:
97-
throw new IllegalArgumentException();
98-
}
99-
}
100-
101-
@Override
102-
public Mode mode() {
103-
return mode;
104-
}
105-
106-
@Override
107-
public boolean equals(Object obj) {
108-
if (obj == this) return true;
109-
if (obj == null || obj.getClass() != this.getClass()) return false;
110-
var that = (RelativePathFileData) obj;
111-
return Objects.equals(this.mode, that.mode)
112-
&& Objects.equals(this.relativePath, that.relativePath)
113-
&& Objects.equals(this.baseDir, that.baseDir);
114-
}
44+
Stream<Path> resolvePaths(PathLookup pathLookup);
11545

116-
@Override
117-
public int hashCode() {
118-
return Objects.hash(relativePath, baseDir, mode);
119-
}
120-
}
46+
Mode mode();
12147

12248
static FileData ofPath(Path path, Mode mode) {
12349
assert path.isAbsolute();
@@ -128,10 +54,29 @@ static FileData ofRelativePath(Path relativePath, BaseDir baseDir, Mode mode) {
12854
assert relativePath.isAbsolute() == false;
12955
return new RelativePathFileData(relativePath, baseDir, mode);
13056
}
57+
}
13158

132-
Stream<Path> resolvePaths(PathLookup pathLookup);
59+
private record AbsolutePathFileData(Path path, Mode mode) implements FileData {
60+
@Override
61+
public Stream<Path> resolvePaths(PathLookup pathLookup) {
62+
return Stream.of(path);
63+
}
64+
}
13365

134-
Mode mode();
66+
private record RelativePathFileData(Path relativePath, BaseDir baseDir, Mode mode) implements FileData {
67+
68+
@Override
69+
public Stream<Path> resolvePaths(PathLookup pathLookup) {
70+
Objects.requireNonNull(pathLookup);
71+
switch (baseDir) {
72+
case CONFIG:
73+
return Stream.of(pathLookup.configDir().resolve(relativePath));
74+
case DATA:
75+
return Arrays.stream(pathLookup.dataDirs()).map(d -> d.resolve(relativePath));
76+
default:
77+
throw new IllegalArgumentException();
78+
}
79+
}
13580
}
13681

13782
private static Mode parseMode(String mode) {

0 commit comments

Comments
 (0)