Skip to content

Commit

Permalink
Static factory methods for UcanaccessRuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 2, 2024
1 parent 27cab25 commit 0b3e4c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/main/java/net/ucanaccess/converters/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,15 @@ public static Timestamp timestamp0(String _dt) {
if (mtc.find()) {
gc.set(Integer.parseInt(mtc.group(1)), Integer.parseInt(mtc.group(2)) - 1, Integer.parseInt(mtc.group(3)));
} else {
throw new UcanaccessRuntimeException("internal error in parsing timestamp");
UcanaccessRuntimeException.throwNow("internal error in parsing timestamp");
}
mtc = pth.matcher(_dt);
if (mtc.find()) {
gc.set(Calendar.HOUR_OF_DAY, Integer.parseInt(mtc.group(1)));
gc.set(Calendar.MINUTE, Integer.parseInt(mtc.group(2)));
gc.set(Calendar.SECOND, Integer.parseInt(mtc.group(3)));
} else {
throw new UcanaccessRuntimeException("internal error in parsing timestamp");
UcanaccessRuntimeException.throwNow("internal error in parsing timestamp");
}
gc.set(Calendar.MILLISECOND, 0);
return new Timestamp(gc.getTime().getTime());
Expand Down Expand Up @@ -870,7 +870,7 @@ public static String mid(String _value, int start, int length) {
}
int len = start - 1 + length;
if (start < 1) {
throw new UcanaccessRuntimeException("Invalid function call");
UcanaccessRuntimeException.throwNow("Invalid function call");
}
if (len > _value.length()) {
len = _value.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,24 @@ public UcanaccessRuntimeException(String _message, Throwable _cause) {
super(_message, _cause);
}

public static <T> T requireNonNull(T obj, String _message) {
if (obj == null) {
throw new UcanaccessRuntimeException(_message);
}
return obj;
}

/**
* Convenience method to throw a {@code UcanaccessRuntimeException} with the specified error message.<br> Using this method rather than {@code throw new} avoids blocks in lambdas.
*
* @param _message exception message
*/
public static void throwNow(String _message) {
throw new UcanaccessRuntimeException(_message);
}

public static void throwNow(String _message, Throwable _cause) {
throw new UcanaccessRuntimeException(_message, _cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public UcanaccessConnectionBuilder withNewDatabaseVersion(String _version) {
if (_version != null) {
version = FileFormat.parse(_version);
if (version == null) {
throw new UcanaccessRuntimeException("Version required");
UcanaccessRuntimeException.throwNow("Valid version required: " + _version);
}
}
return withProp(newDatabaseVersion, version);
}

public UcanaccessConnectionBuilder withProp(Metadata.Property _prop, Object _value) {
Objects.requireNonNull(_prop, "Property required");
UcanaccessRuntimeException.requireNonNull(_prop, "Property required");

String val = Optional.ofNullable(_value).map(Object::toString).orElse("");
props.put(_prop, val);
Expand All @@ -102,7 +102,7 @@ public UcanaccessConnectionBuilder withProp(Metadata.Property _prop, Object _val
}

public String getUrl() {
Objects.requireNonNull(dbPath, "Database path required");
UcanaccessRuntimeException.requireNonNull(dbPath, "Database path required");

String url = UcanaccessDriver.URL_PREFIX + dbPath;

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/net/ucanaccess/test/AbstractBaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ucanaccess.test;

import net.ucanaccess.exception.UcanaccessRuntimeException;
import net.ucanaccess.util.Try;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand All @@ -16,7 +17,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -110,16 +110,16 @@ protected static String getTempDir() {
}

protected static File copyFile(Path _source, File _target) {
Objects.requireNonNull(_source, "Source file required");
Objects.requireNonNull(_target, "Target file required");
UcanaccessRuntimeException.requireNonNull(_source, "Source file required");
UcanaccessRuntimeException.requireNonNull(_target, "Target file required");
Try.catching(() -> Files.copy(_source, _target.toPath(), StandardCopyOption.REPLACE_EXISTING))
.orThrow(e -> new UncheckedIOException("Failed to copy '" + _source + "' to '" + _target + "'", e));
return _target;
}

protected static File copyFile(InputStream _in, File _target) {
Objects.requireNonNull(_in, "Input stream required");
Objects.requireNonNull(_target, "Target file required");
UcanaccessRuntimeException.requireNonNull(_in, "Input stream required");
UcanaccessRuntimeException.requireNonNull(_target, "Target file required");
Try.catching(() -> Files.copy(_in, _target.toPath(), StandardCopyOption.REPLACE_EXISTING))
.orThrow(e -> new UncheckedIOException("Failed to copy to '" + _target + "'", e));
return _target;
Expand Down

0 comments on commit 0b3e4c6

Please sign in to comment.