Skip to content

Commit

Permalink
Applied inspection suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolocust committed Apr 30, 2023
1 parent 8436c41 commit ed15264
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@ public MessageDigestResult getMessageDigestResult() {
return _messageDigestResult;
}

/**
* Returns the name that was set to this instance.
*
* @return the name that was set to this instance.
*/
public String getName() {
return _name;
}

@Override
public int hashCode() {
int hash = 7;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ default void changeValue(T value) {
* Detaches the specified listener.
*
* @param listener the listener to be detached.
* @return true if listener was detached, false if listener had not been
* attached before.
*/
boolean detach(Listener<T> listener);
void detach(Listener<T> listener);

/**
* Removes all attached listeners.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public final void attach(Listener<T> listener) {
}

@Override
public final boolean detach(Listener<T> listener) {
return _listeners.remove(listener);
public final void detach(Listener<T> listener) {
_listeners.remove(listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,4 @@ public static <T> Predicate<T> createAlwaysTrue() {
return p -> true;
}

/**
* Creates a new {@link Predicate} which always evaluates to <b>false</b>.
*
* @param <T> the type of the object that is to be consumed by the predicate
* and thus the type of the input to the predicate.
* @return a new instance of {@link Predicate} with the specified type.
*/
public static <T> Predicate<T> createAlwaysFalse() {
return p -> false;
}
}
45 changes: 28 additions & 17 deletions src/main/java/org/gzipper/java/application/util/AppUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ private static String determineJavaVersion() {
return version;
}

private static String createTemporaryFile(BufferedInputStream bis, String tempName) throws IOException {
final File file = File.createTempFile(tempName, ".tmp");
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {

int readBytes;
byte[] bytes = new byte[1024];

while ((readBytes = bis.read(bytes)) != -1) {
bos.write(bytes, 0, readBytes);
}
}

file.deleteOnExit();
return file.getPath();
}

/**
* Determines the current Java version and returns the major version of Java
* as string. For e.g. Java 8, this would return {@code 1.8}.
Expand All @@ -74,31 +90,26 @@ public static String getJavaVersion() {
* @return the resource path of the specified class.
* @throws URISyntaxException if URL conversion failed.
*/
public static String getResource(Class<?> clazz, String name) throws URISyntaxException {
public static String getResource(Class<?> clazz, String name) throws URISyntaxException, FileNotFoundException {

String resource = null;

final URL url = clazz.getResource(name);
if (url.toString().startsWith("jar:")) {

String tempName = name.substring(name.lastIndexOf('/') + 1);

try (BufferedInputStream bis = new BufferedInputStream(clazz.getResourceAsStream(name))) {

File file = File.createTempFile(tempName, ".tmp");
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
if (url == null) {
throw new FileNotFoundException("Resource not found: " + name);
}

int readBytes;
byte[] bytes = new byte[1024];
if (url.toString().startsWith("jar:")) {

while ((readBytes = bis.read(bytes)) != -1) {
bos.write(bytes, 0, readBytes);
}
}
String tempName = name.substring(name.lastIndexOf('/') + 1);
var resourceStream = clazz.getResourceAsStream(name);

resource = file.getPath();
file.deleteOnExit();
if (resourceStream == null) {
throw new FileNotFoundException("Resource not found: " + name);
}

try (BufferedInputStream bis = new BufferedInputStream(resourceStream)) {
resource = createTemporaryFile(bis, tempName);
} catch (IOException ex) {
Log.e(ex.getLocalizedMessage(), ex);
}
Expand Down
59 changes: 3 additions & 56 deletions src/main/java/org/gzipper/java/exceptions/GZipperException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
*/
package org.gzipper.java.exceptions;

import java.io.Serial;

/**
* Class to handle application specific errors.
*
* @author Matthias Fussenegger
*/
public class GZipperException extends Exception {

@Serial
private static final long serialVersionUID = 5822293002523982761L;

private Reason _reason = Reason.UNKNOWN;

/**
* Delegates exception to its super class {@link Exception}.
*/
public GZipperException() {
super();
}

/**
* Delegates error message to its super class {@link Exception}.
*
Expand All @@ -52,28 +48,6 @@ public GZipperException(Throwable cause) {
super(cause);
}

/**
* Delegates error message and cause to its super class {@link Exception}.
*
* @param errorMessage the specified error message.
* @param cause the cause of this exception.
*/
public GZipperException(String errorMessage, Throwable cause) {
super(errorMessage, cause);
}

/**
* Creates a new {@link GZipperException} including a reason.
*
* @param reason the reason of this exception.
* @return a new instance of {@link GZipperException}.
*/
public static GZipperException createWithReason(Reason reason) {
GZipperException ex = new GZipperException();
ex.setReason(reason);
return ex;
}

/**
* Creates a new {@link GZipperException} including a reason.
*
Expand All @@ -87,33 +61,6 @@ public static GZipperException createWithReason(Reason reason, String msg) {
return ex;
}

/**
* Creates a new {@link GZipperException} including a reason.
*
* @param reason the reason of this exception.
* @param cause the cause of this exception.
* @return a new instance of {@link GZipperException}.
*/
public static GZipperException createWithReason(Reason reason, Throwable cause) {
GZipperException ex = new GZipperException(cause);
ex.setReason(reason);
return ex;
}

/**
* Creates a new {@link GZipperException} including a reason.
*
* @param reason the reason of this exception.
* @param msg the specified error message.
* @param cause the cause of this exception.
* @return a new instance of {@link GZipperException}.
*/
public static GZipperException createWithReason(Reason reason, String msg, Throwable cause) {
GZipperException ex = new GZipperException(msg, cause);
ex.setReason(reason);
return ex;
}

/**
* Returns the reason of this exception.
*
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/gzipper/java/presentation/CSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.gzipper.java.presentation;

import javafx.scene.Scene;
import org.gzipper.java.util.Log;

import java.util.List;

Expand Down Expand Up @@ -45,8 +46,15 @@ private CSS() {
public static void load(Theme theme, Scene scene) {
List<String> stylesheets = scene.getStylesheets();
stylesheets.clear();

if (theme != Theme.getDefault()) {
stylesheets.add(CSS.class.getResource(theme.getLocation()).toExternalForm());
var resource = CSS.class.getResource(theme.getLocation());

if (resource != null) {
stylesheets.add(resource.toExternalForm());
} else {
Log.w("Could not load theme: " + theme.getLocation(), true);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.gzipper.java.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -112,6 +113,8 @@ public void initialize(URL location, ResourceBundle resources) {
} catch (URISyntaxException ex) {
Log.e(I18N.getString("error.text"), ex);
imgRes = AppUtils.getDecodedRootPath(getClass()) + IMG_NAME;
} catch (FileNotFoundException ex) {
Log.e(I18N.getString("error.text"), ex);
} finally {
if (imgRes != null) {
_imageFile = new File(imgRes);
Expand Down

0 comments on commit ed15264

Please sign in to comment.