summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/utils/Utils.java
diff options
context:
space:
mode:
authorGravatar Thog2017-03-08 08:17:04 +0100
committerGravatar Thog2017-03-08 08:17:04 +0100
commit6e464ea251cab63c776ece0b2a356f1498ffa294 (patch)
tree5ed30c03f5ac4cd2d6877874f5ede576049954f7 /src/main/java/cuchaz/enigma/utils/Utils.java
parentDrop unix case style and implement hashCode when equals is overrided (diff)
downloadenigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.gz
enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.xz
enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.zip
Follow Fabric guidelines
Diffstat (limited to 'src/main/java/cuchaz/enigma/utils/Utils.java')
-rw-r--r--src/main/java/cuchaz/enigma/utils/Utils.java114
1 files changed, 56 insertions, 58 deletions
diff --git a/src/main/java/cuchaz/enigma/utils/Utils.java b/src/main/java/cuchaz/enigma/utils/Utils.java
index 474f7ed..8e502d4 100644
--- a/src/main/java/cuchaz/enigma/utils/Utils.java
+++ b/src/main/java/cuchaz/enigma/utils/Utils.java
@@ -8,12 +8,13 @@
8 * Contributors: 8 * Contributors:
9 * Jeff Martin - initial API and implementation 9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/ 10 ******************************************************************************/
11
11package cuchaz.enigma.utils; 12package cuchaz.enigma.utils;
12 13
13import com.google.common.io.CharStreams; 14import com.google.common.io.CharStreams;
14 15
15import java.awt.Desktop; 16import javax.swing.*;
16import java.awt.Font; 17import java.awt.*;
17import java.awt.event.MouseEvent; 18import java.awt.event.MouseEvent;
18import java.io.IOException; 19import java.io.IOException;
19import java.io.InputStream; 20import java.io.InputStream;
@@ -23,69 +24,66 @@ import java.net.URISyntaxException;
23import java.util.Arrays; 24import java.util.Arrays;
24import java.util.List; 25import java.util.List;
25 26
26import javax.swing.*;
27
28public class Utils { 27public class Utils {
29 28
30 public static int combineHashesOrdered(Object... objs) { 29 public static int combineHashesOrdered(Object... objs) {
31 return combineHashesOrdered(Arrays.asList(objs)); 30 return combineHashesOrdered(Arrays.asList(objs));
32 } 31 }
33 32
34 public static int combineHashesOrdered(List<Object> objs) { 33 public static int combineHashesOrdered(List<Object> objs) {
35 final int prime = 67; 34 final int prime = 67;
36 int result = 1; 35 int result = 1;
37 for (Object obj : objs) { 36 for (Object obj : objs) {
38 result *= prime; 37 result *= prime;
39 if (obj != null) { 38 if (obj != null) {
40 result += obj.hashCode(); 39 result += obj.hashCode();
41 } 40 }
42 } 41 }
43 return result; 42 return result;
44 } 43 }
45 44
46 public static String readStreamToString(InputStream in) throws IOException { 45 public static String readStreamToString(InputStream in) throws IOException {
47 return CharStreams.toString(new InputStreamReader(in, "UTF-8")); 46 return CharStreams.toString(new InputStreamReader(in, "UTF-8"));
48 } 47 }
49 48
50 public static String readResourceToString(String path) throws IOException { 49 public static String readResourceToString(String path) throws IOException {
51 InputStream in = Utils.class.getResourceAsStream(path); 50 InputStream in = Utils.class.getResourceAsStream(path);
52 if (in == null) { 51 if (in == null) {
53 throw new IllegalArgumentException("Resource not found! " + path); 52 throw new IllegalArgumentException("Resource not found! " + path);
54 } 53 }
55 return readStreamToString(in); 54 return readStreamToString(in);
56 } 55 }
57 56
58 public static void openUrl(String url) { 57 public static void openUrl(String url) {
59 if (Desktop.isDesktopSupported()) { 58 if (Desktop.isDesktopSupported()) {
60 Desktop desktop = Desktop.getDesktop(); 59 Desktop desktop = Desktop.getDesktop();
61 try { 60 try {
62 desktop.browse(new URI(url)); 61 desktop.browse(new URI(url));
63 } catch (IOException ex) { 62 } catch (IOException ex) {
64 throw new Error(ex); 63 throw new Error(ex);
65 } catch (URISyntaxException ex) { 64 } catch (URISyntaxException ex) {
66 throw new IllegalArgumentException(ex); 65 throw new IllegalArgumentException(ex);
67 } 66 }
68 } 67 }
69 } 68 }
70 69
71 public static JLabel unboldLabel(JLabel label) { 70 public static JLabel unboldLabel(JLabel label) {
72 Font font = label.getFont(); 71 Font font = label.getFont();
73 label.setFont(font.deriveFont(font.getStyle() & ~Font.BOLD)); 72 label.setFont(font.deriveFont(font.getStyle() & ~Font.BOLD));
74 return label; 73 return label;
75 } 74 }
76 75
77 public static void showToolTipNow(JComponent component) { 76 public static void showToolTipNow(JComponent component) {
78 // HACKHACK: trick the tooltip manager into showing the tooltip right now 77 // HACKHACK: trick the tooltip manager into showing the tooltip right now
79 ToolTipManager manager = ToolTipManager.sharedInstance(); 78 ToolTipManager manager = ToolTipManager.sharedInstance();
80 int oldDelay = manager.getInitialDelay(); 79 int oldDelay = manager.getInitialDelay();
81 manager.setInitialDelay(0); 80 manager.setInitialDelay(0);
82 manager.mouseMoved(new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false)); 81 manager.mouseMoved(new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false));
83 manager.setInitialDelay(oldDelay); 82 manager.setInitialDelay(oldDelay);
84 } 83 }
85 84
86 public static boolean getSystemPropertyAsBoolean(String property, boolean defValue) 85 public static boolean getSystemPropertyAsBoolean(String property, boolean defValue) {
87 { 86 String value = System.getProperty(property);
88 String value = System.getProperty(property); 87 return value == null ? defValue : Boolean.parseBoolean(value);
89 return value == null ? defValue : Boolean.parseBoolean(value); 88 }
90 }
91} 89}