Skip to content

Commit 10b1df2

Browse files
committed
[refactor] Use switch expressions (in a better way)
1 parent 3062ed6 commit 10b1df2

File tree

6 files changed

+103
-101
lines changed

6 files changed

+103
-101
lines changed

exist-core/src/main/java/org/exist/security/internal/AccountImpl.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,23 @@ private void instantiate(final Account from_user) throws PermissionDeniedExcepti
172172
//copy umask
173173
setUserMask(from_user.getUserMask());
174174

175-
if(from_user instanceof AccountImpl user) {
176-
177-
groups = new ArrayList<>(user.groups);
178-
179-
password = user.password;
180-
digestPassword = user.digestPassword;
181-
182-
hasDbaRole = user.hasDbaRole;
183-
184-
_cred = user._cred;
185-
} else if(from_user instanceof UserAider user) {
186-
187-
final String[] groups = user.getGroups();
188-
for (final String group : groups) {
189-
addGroup(group);
175+
switch (from_user) {
176+
case AccountImpl user -> {
177+
groups = new ArrayList<>(user.groups);
178+
password = user.password;
179+
digestPassword = user.digestPassword;
180+
hasDbaRole = user.hasDbaRole;
181+
_cred = user._cred;
190182
}
191-
192-
setPassword(user.getPassword());
193-
digestPassword = user.getDigestPassword();
194-
} else {
195-
addGroup(from_user.getDefaultGroup());
183+
case UserAider user -> {
184+
final String[] groups = user.getGroups();
185+
for (final String group : groups) {
186+
addGroup(group);
187+
}
188+
setPassword(user.getPassword());
189+
digestPassword = user.getDigestPassword();
190+
}
191+
default -> addGroup(from_user.getDefaultGroup());
196192
//TODO: groups
197193
}
198194
}

exist-core/src/main/java/org/exist/security/internal/Password.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ public enum Hash {
5454

5555
final Pattern ptnHash = Pattern.compile("\\{([A-Z0-9]+)\\}(.*)");
5656

57-
public Password(Account account, String password) {
58-
57+
public Password(final Account account, final String password) {
58+
5959
if (password == null) {
6060
this.algorithm = DEFAULT_ALGORITHM;
6161
this.pw = null;
6262
this.digestPw = null;
63-
} else{
63+
} else {
6464
final Matcher mtcHash = ptnHash.matcher(password);
65-
65+
6666
if (mtcHash.matches()) {
6767
this.algorithm = Hash.valueOf(mtcHash.group(1));
6868
this.pw = mtcHash.group(2);
6969
} else {
7070
this.algorithm = DEFAULT_ALGORITHM;
7171
this.pw = hashAndEncode(password);
7272
}
73-
73+
7474
this.digestPw = digest(account.getName(), account.getRealmId(), pw);
7575
}
7676
}
@@ -148,31 +148,22 @@ public boolean check(Object credentials) {
148148

149149
@Override
150150
public boolean equals(Object obj) {
151-
152-
if(obj == this) {
151+
152+
if (obj == this) {
153153
return true;
154154
}
155155

156-
switch (obj) {
157-
case null -> {
158-
return false;
159-
}
160-
case Password p -> {
161-
162-
if (algorithm != p.algorithm) {
163-
throw new RuntimeException("Cannot compare passwords with different algorithms i.e. " + algorithm + " and " + p.algorithm);
156+
return switch (obj) {
157+
case Password password -> {
158+
if (algorithm != password.algorithm) {
159+
throw new RuntimeException("Cannot compare passwords with different algorithms i.e. "
160+
+ algorithm + " and " + password.algorithm);
164161
}
165-
166-
return (Objects.equals(pw, p.pw));
167-
}
168-
case String s -> {
169-
return (hashAndEncode(s)).equals(pw);
170-
}
171-
default -> {
162+
yield Objects.equals(pw, password.pw);
172163
}
173-
}
174-
175-
return false;
164+
case String s -> (hashAndEncode(s)).equals(pw);
165+
case null, default -> false;
166+
};
176167
}
177168

178169
@Override

exist-core/src/main/java/org/exist/storage/btree/Repair.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,29 @@ public Repair() {
5353
}
5454

5555
public void repair(String id) {
56-
try(final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
56+
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
5757

58-
BTree btree = null;
59-
switch (id) {
60-
case "collections" -> btree = ((NativeBroker) broker).getStorage(NativeBroker.COLLECTIONS_DBX_ID);
61-
case "dom" -> btree = ((NativeBroker) broker).getStorage(NativeBroker.DOM_DBX_ID);
62-
case "range" -> btree = ((NativeBroker) broker).getStorage(NativeBroker.VALUES_DBX_ID);
58+
final BTree btree = switch (id) {
59+
case null -> null; // prevent NPE in index lookup (default)
60+
case "collections" -> ((NativeBroker) broker).getStorage(NativeBroker.COLLECTIONS_DBX_ID);
61+
case "dom" -> ((NativeBroker) broker).getStorage(NativeBroker.DOM_DBX_ID);
62+
case "range" -> ((NativeBroker) broker).getStorage(NativeBroker.VALUES_DBX_ID);
6363
case "structure" -> {
6464
NativeStructuralIndexWorker index = (NativeStructuralIndexWorker)
6565
broker.getIndexController().getWorkerByIndexName(StructuralIndex.STRUCTURAL_INDEX_ID);
66-
btree = index.getStorage();
66+
yield index.getStorage();
6767
}
68-
case null, default -> {
68+
default -> {
6969
// use index id defined in conf.xml
7070
Index index = pool.getIndexManager().getIndexByName(id);
7171
if (index != null) {
72-
btree = index.getStorage();
72+
yield index.getStorage();
73+
} else {
74+
yield null;
7375
}
7476
}
75-
}
77+
};
78+
7679
if (btree == null) {
7780
System.console().printf("Unkown index: %s\n", id);
7881
return;

exist-core/src/main/java/org/exist/xmldb/LocalBinaryResource.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,42 +120,47 @@ Object getContent(final DBBroker broker, final Txn transaction) throws XMLDBExce
120120
}
121121

122122
private Object getContent(final Object res) throws XMLDBException {
123-
if(res != null) {
124-
switch (res) {
125-
case Path path -> {
126-
return readFile(path);
127-
}
128-
case File file1 -> {
129-
return readFile(file1.toPath());
130-
}
131-
case InputSource source -> {
132-
return readFile(source);
133-
}
134-
case byte[] bytes -> {
135-
return res;
136-
}
137-
case BinaryValue value -> {
138-
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
139-
((BinaryValue) res).streamBinaryTo(baos);
140-
return baos.toByteArray();
141-
} catch (final IOException e) {
142-
throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage(), e);
143-
}
144-
}
145-
case InputStream inputStream -> {
146-
try (final InputStream is = (InputStream) res) {
147-
return readFile(is);
148-
} catch (final IOException e) {
149-
throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage(), e);
150-
}
123+
124+
/*
125+
DW: can use "return switch(res)" as this will give deep Java Validation errors.
126+
This might be a java bug.
127+
*/
128+
switch (res) {
129+
case Path path -> {
130+
return readFile(path);
131+
}
132+
case File file -> {
133+
return readFile(file.toPath());
134+
}
135+
case InputSource inputSource -> {
136+
return readFile(inputSource);
137+
}
138+
case byte[] bytes -> {
139+
return res;
140+
}
141+
case BinaryValue binaryValue -> {
142+
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
143+
binaryValue.streamBinaryTo(baos);
144+
return baos.toByteArray();
145+
} catch (final IOException e) {
146+
throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage(), e);
151147
}
152-
default -> {
148+
}
149+
case InputStream inputStream -> {
150+
/* This will manage the inputstream */
151+
try (final InputStream is = inputStream) {
152+
return readFile(is);
153+
} catch (final IOException e) {
154+
throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage(), e);
153155
}
154156
}
157+
case null, default -> {
158+
return null;
159+
}
155160
}
156-
return res;
157161
}
158162

163+
159164
@Override
160165
public void setContent(final Object value) throws XMLDBException {
161166
switch (value) {

exist-core/src/main/java/org/exist/xmldb/RemoteCollection.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -471,36 +471,43 @@ public void storeResource(final Resource res) throws XMLDBException {
471471

472472
@Override
473473
public void storeResource(final Resource res, final Instant a, final Instant b) throws XMLDBException {
474-
final Object content = (res instanceof ExtendedResource) ? ((ExtendedResource) res).getExtendedContent() : res.getContent();
474+
475+
final Object content = (res instanceof ExtendedResource)
476+
? ((ExtendedResource) res).getExtendedContent()
477+
: res.getContent();
478+
475479
if (content instanceof Path || content instanceof File || content instanceof InputSource) {
476-
long fileLength = -1;
477-
switch (content) {
478-
case Path file -> {
479-
if (!Files.isReadable(file)) {
480-
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Failed to read resource from file " + file.toAbsolutePath());
480+
long fileLength = switch (content) {
481+
case Path path -> {
482+
if (!Files.isReadable(path)) {
483+
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
484+
"Failed to read resource from file " + path.toAbsolutePath());
481485
}
482-
fileLength = FileUtils.sizeQuietly(file);
486+
yield FileUtils.sizeQuietly(path);
483487
}
484488
case File file -> {
485489
if (!file.canRead()) {
486-
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Failed to read resource from file " + file.getAbsolutePath());
490+
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
491+
"Failed to read resource from file " + file.getAbsolutePath());
487492
}
488-
fileLength = file.length();
493+
yield file.length();
489494
}
490-
case EXistInputSource eXistInputSource -> fileLength = eXistInputSource.getByteStreamLength();
491-
default -> {
492-
}
493-
}
495+
case EXistInputSource eXistInputSource -> eXistInputSource.getByteStreamLength();
496+
default -> -1;
497+
};
498+
494499
if (res instanceof AbstractRemoteResource) {
495500
((AbstractRemoteResource) res).dateCreated = a;
496501
((AbstractRemoteResource) res).dateModified = b;
497502
}
503+
498504
if (!BINARY_RESOURCE.equals(res.getResourceType()) && fileLength != -1
499505
&& fileLength < MAX_CHUNK_LENGTH) {
500506
store((RemoteXMLResource) res);
501507
} else {
502508
uploadAndStore(res);
503509
}
510+
504511
} else {
505512
((AbstractRemoteResource) res).dateCreated = a;
506513
((AbstractRemoteResource) res).dateModified = b;

exist-core/src/main/java/org/exist/xquery/functions/math/OneParamFunctions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,9 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
142142

143143
} else {
144144
final NumericValue value = (NumericValue) firstArgument.itemAt(0).convertTo(Type.DOUBLE);
145-
double calcValue = 0;
146145
final String functionName = getSignature().getName().getLocalPart();
147146

148-
calcValue = switch (functionName) {
147+
final double calcValue = switch (functionName) {
149148
case ACOS -> Math.acos(value.getDouble());
150149
case ASIN -> Math.asin(value.getDouble());
151150
case ATAN -> Math.atan(value.getDouble());
@@ -157,7 +156,8 @@ public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathExce
157156
case SIN -> Math.sin(value.getDouble());
158157
case SQRT -> Math.sqrt(value.getDouble());
159158
case TAN -> Math.tan(value.getDouble());
160-
case null, default -> throw new XPathException(this, ERROR, "Function " + functionName + " not found.");
159+
case null -> throw new XPathException(this, ERROR, "Function " + functionName + " not found.");
160+
default -> 0;
161161
};
162162
return new DoubleValue(this, calcValue);
163163
}

0 commit comments

Comments
 (0)