]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/util/Util.java
Use .isEmpty() for checking lists and strings
[signal-cli] / src / main / java / org / asamk / signal / util / Util.java
index 01a79dd10b52fc7811b6b9819715d33850c5ef59..e193d25330b7843fe9b67ef8c933fef8d7882045 100644 (file)
@@ -5,21 +5,30 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
-import org.whispersystems.libsignal.util.guava.Optional;
-import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 public class Util {
 
+    private final static Logger logger = LoggerFactory.getLogger(Util.class);
+
     private Util() {
     }
 
     public static String getStringIfNotBlank(Optional<String> value) {
-        var string = value.orNull();
+        var string = value.orElse(null);
         if (string == null || string.isBlank()) {
             return null;
         }
@@ -32,12 +41,12 @@ public class Util {
     }
 
     private static String toCamelCaseString(List<String> strings) {
-        if (strings.size() == 0) {
+        if (strings.isEmpty()) {
             return "";
         }
         return strings.get(0) + strings.stream()
                 .skip(1)
-                .filter(s -> s.length() > 0)
+                .filter(s -> !s.isEmpty())
                 .map(s -> Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(Locale.ROOT))
                 .collect(Collectors.joining());
     }
@@ -59,14 +68,37 @@ public class Util {
         return f.toString();
     }
 
-    public static String getLegacyIdentifier(final SignalServiceAddress address) {
-        return address.getNumber().or(() -> address.getUuid().get().toString());
-    }
-
     public static ObjectMapper createJsonObjectMapper() {
         var objectMapper = new ObjectMapper();
         objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
         objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
         return objectMapper;
     }
+
+    public static Map<String, String> getQueryMap(String query) {
+        var params = query.split("&");
+        var map = new HashMap<String, String>();
+        for (var param : params) {
+            final var paramParts = param.split("=");
+            var name = URLDecoder.decode(paramParts[0], StandardCharsets.UTF_8);
+            var value = paramParts.length == 1 ? null : URLDecoder.decode(paramParts[1], StandardCharsets.UTF_8);
+            map.put(name, value);
+        }
+        return map;
+    }
+
+    public static void closeExecutorService(ExecutorService executor) {
+        executor.shutdown();
+        try {
+            if (!executor.awaitTermination(5, TimeUnit.MINUTES)) {
+                executor.shutdownNow();
+                if (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
+                    logger.warn("Failed to shutdown executor service");
+                }
+            }
+        } catch (InterruptedException e) {
+            executor.shutdownNow();
+            Thread.currentThread().interrupt();
+        }
+    }
 }