summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/utils/Utils.java
diff options
context:
space:
mode:
authorGravatar Runemoro2020-06-03 13:39:42 -0400
committerGravatar GitHub2020-06-03 18:39:42 +0100
commit0f47403d0220757fed189b76e2071e25b1025cb8 (patch)
tree879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/utils/Utils.java
parentFix search dialog hanging for a short time sometimes (#250) (diff)
downloadenigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz
enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz
enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip
Split GUI code to separate module (#242)
* Split into modules * Post merge compile fixes Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/utils/Utils.java')
-rw-r--r--src/main/java/cuchaz/enigma/utils/Utils.java179
1 files changed, 0 insertions, 179 deletions
diff --git a/src/main/java/cuchaz/enigma/utils/Utils.java b/src/main/java/cuchaz/enigma/utils/Utils.java
deleted file mode 100644
index b45b00d..0000000
--- a/src/main/java/cuchaz/enigma/utils/Utils.java
+++ /dev/null
@@ -1,179 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11
12package cuchaz.enigma.utils;
13
14import com.google.common.io.CharStreams;
15import org.objectweb.asm.Opcodes;
16
17import javax.swing.*;
18import javax.swing.text.BadLocationException;
19import javax.swing.text.JTextComponent;
20import java.awt.*;
21import java.awt.event.MouseEvent;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.net.URI;
26import java.net.URISyntaxException;
27import java.nio.charset.StandardCharsets;
28import java.nio.file.Files;
29import java.nio.file.Path;
30import java.security.MessageDigest;
31import java.security.NoSuchAlgorithmException;
32import java.util.*;
33import java.util.List;
34import java.util.stream.Collectors;
35import java.util.zip.ZipEntry;
36import java.util.zip.ZipFile;
37
38public class Utils {
39
40 public static final int ASM_VERSION = Opcodes.ASM8;
41
42 public static int combineHashesOrdered(Object... objs) {
43 final int prime = 67;
44 int result = 1;
45 for (Object obj : objs) {
46 result *= prime;
47 if (obj != null) {
48 result += obj.hashCode();
49 }
50 }
51 return result;
52 }
53
54 public static int combineHashesOrdered(List<Object> objs) {
55 final int prime = 67;
56 int result = 1;
57 for (Object obj : objs) {
58 result *= prime;
59 if (obj != null) {
60 result += obj.hashCode();
61 }
62 }
63 return result;
64 }
65
66 public static String readStreamToString(InputStream in) throws IOException {
67 return CharStreams.toString(new InputStreamReader(in, "UTF-8"));
68 }
69
70 public static String readResourceToString(String path) throws IOException {
71 InputStream in = Utils.class.getResourceAsStream(path);
72 if (in == null) {
73 throw new IllegalArgumentException("Resource not found! " + path);
74 }
75 return readStreamToString(in);
76 }
77
78 public static void openUrl(String url) {
79 if (Desktop.isDesktopSupported()) {
80 Desktop desktop = Desktop.getDesktop();
81 try {
82 desktop.browse(new URI(url));
83 } catch (IOException ex) {
84 throw new Error(ex);
85 } catch (URISyntaxException ex) {
86 throw new IllegalArgumentException(ex);
87 }
88 }
89 }
90
91 public static JLabel unboldLabel(JLabel label) {
92 Font font = label.getFont();
93 label.setFont(font.deriveFont(font.getStyle() & ~Font.BOLD));
94 return label;
95 }
96
97 public static void showToolTipNow(JComponent component) {
98 // HACKHACK: trick the tooltip manager into showing the tooltip right now
99 ToolTipManager manager = ToolTipManager.sharedInstance();
100 int oldDelay = manager.getInitialDelay();
101 manager.setInitialDelay(0);
102 manager.mouseMoved(new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0, 0, 0, 0, false));
103 manager.setInitialDelay(oldDelay);
104 }
105
106 public static Rectangle safeModelToView(JTextComponent component, int modelPos) {
107 if (modelPos < 0) {
108 modelPos = 0;
109 } else if (modelPos >= component.getText().length()) {
110 modelPos = component.getText().length();
111 }
112 try {
113 return component.modelToView(modelPos);
114 } catch (BadLocationException e) {
115 throw new RuntimeException(e);
116 }
117 }
118
119 public static boolean getSystemPropertyAsBoolean(String property, boolean defValue) {
120 String value = System.getProperty(property);
121 return value == null ? defValue : Boolean.parseBoolean(value);
122 }
123
124 public static void delete(Path path) throws IOException {
125 if (Files.exists(path)) {
126 for (Path p : Files.walk(path).sorted(Comparator.reverseOrder()).collect(Collectors.toList())) {
127 Files.delete(p);
128 }
129 }
130 }
131
132 public static byte[] zipSha1(Path path) throws IOException {
133 MessageDigest digest;
134 try {
135 digest = MessageDigest.getInstance("SHA-1");
136 } catch (NoSuchAlgorithmException e) {
137 // Algorithm guaranteed to be supported
138 throw new RuntimeException(e);
139 }
140 try (ZipFile zip = new ZipFile(path.toFile())) {
141 List<? extends ZipEntry> entries = Collections.list(zip.entries());
142 // only compare classes (some implementations may not generate directory entries)
143 entries.removeIf(entry -> !entry.getName().toLowerCase(Locale.ROOT).endsWith(".class"));
144 // different implementations may add zip entries in a different order
145 entries.sort(Comparator.comparing(ZipEntry::getName));
146 byte[] buffer = new byte[8192];
147 for (ZipEntry entry : entries) {
148 digest.update(entry.getName().getBytes(StandardCharsets.UTF_8));
149 try (InputStream in = zip.getInputStream(entry)) {
150 int n;
151 while ((n = in.read(buffer)) != -1) {
152 digest.update(buffer, 0, n);
153 }
154 }
155 }
156 }
157 return digest.digest();
158 }
159
160 public static String caplisiseCamelCase(String input){
161 StringJoiner stringJoiner = new StringJoiner(" ");
162 for (String word : input.toLowerCase(Locale.ROOT).split("_")) {
163 stringJoiner.add(word.substring(0, 1).toUpperCase(Locale.ROOT) + word.substring(1));
164 }
165 return stringJoiner.toString();
166 }
167
168 public static boolean isBlank(String input) {
169 if (input == null) {
170 return true;
171 }
172 for (int i = 0; i < input.length(); i++) {
173 if (!Character.isWhitespace(input.charAt(i))) {
174 return false;
175 }
176 }
177 return true;
178 }
179}