Skip to content

Commit

Permalink
Improve maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Nov 9, 2023
1 parent 9bb038d commit ae83051
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,9 @@ public void actionPerformed(ActionEvent ev) {
final Cursor cursor = frame.getCursor();
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
publicIp = UPnP.getExternalIP();
if (isInvalidPublicIp()) {
final URL url = new URL(WHATSMYIP_SERVER_URL);
try (final BufferedReader lines = new BufferedReader(new InputStreamReader(url.openStream()))) {
publicIp = lines.readLine();
}
resolvePublicIp();
if (publicIp != null) {
button.setText(publicIp);
}
} catch (IOException ex) {
Log.error("Could not determine public IP", ex);
Expand All @@ -224,9 +221,6 @@ public void actionPerformed(ActionEvent ev) {
} finally {
frame.setCursor(cursor);
}
if (publicIp != null) {
button.setText(publicIp);
}
});
choices.add(menuItem);
} else {
Expand Down Expand Up @@ -265,8 +259,14 @@ public void actionPerformed(ActionEvent ev) {
choices.setLocation(choicesLocation.x - xOffset, choicesLocation.y + yOffset);
}

private boolean isInvalidPublicIp() {
return publicIp == null || publicIp.startsWith("192.168") || publicIp.startsWith("10.");
private void resolvePublicIp() throws IOException {
publicIp = UPnP.getExternalIP();
if (publicIp == null || publicIp.startsWith("192.168") || publicIp.startsWith("10.")) {
final URL url = new URL(WHATSMYIP_SERVER_URL);
try (final BufferedReader lines = new BufferedReader(new InputStreamReader(url.openStream()))) {
publicIp = lines.readLine();
}
}
}
};
ip.putValue("DISPLAY_NAME", "127.0.0.1"); // always a selection
Expand Down Expand Up @@ -312,7 +312,7 @@ private Action createNetworkAssistantConfigurationAction(Assistant assistant) {
@Override
public void actionPerformed(ActionEvent ev) {
JFrame networkFrame = (JFrame) SwingUtilities.getRoot((Component) ev.getSource());
String upnpActive = String.valueOf(assistant.isUpnpEnabled());
String upnpActive = valueOf(assistant.isUpnpEnabled());

final JPanel pane = new JPanel();
pane.setLayout(new GridLayout(4, 1, 10, -10));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mpo/dayon/common/babylon/Babylon.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static synchronized String translate(String tag, Object... arguments) {
String value;
try {
value = bundle.getString(tag);
if (value.trim().length() == 0) {
if (value.trim().isEmpty()) {
value = tag;
}
} catch (MissingResourceException ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void clear() {
*/
@Override
public void onCaptureProcessed() {
if (tiles.size() > 0 && tiles.size() >= maxSize) {
if (!tiles.isEmpty() && tiles.size() >= maxSize) {
Log.info("Purging the cache...");
while (tiles.size() > purgeSize) {
tiles.remove(lru.removeFirst());
Expand Down

0 comments on commit ae83051

Please sign in to comment.