aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/util
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main/java/com/juick/util')
-rw-r--r--juick-server/src/main/java/com/juick/util/TagUtils.java24
-rw-r--r--juick-server/src/main/java/com/juick/util/ThreadHelper.java37
-rw-r--r--juick-server/src/main/java/com/juick/util/UserUtils.java27
3 files changed, 88 insertions, 0 deletions
diff --git a/juick-server/src/main/java/com/juick/util/TagUtils.java b/juick-server/src/main/java/com/juick/util/TagUtils.java
new file mode 100644
index 00000000..157522ab
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/util/TagUtils.java
@@ -0,0 +1,24 @@
+package com.juick.util;
+
+import com.juick.Tag;
+import org.apache.commons.collections4.CollectionUtils;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Created by aalexeev on 11/13/16.
+ */
+public class TagUtils {
+ private TagUtils() {
+ throw new IllegalStateException();
+ }
+
+ public static String toString(final List<Tag> tags) {
+ if (CollectionUtils.isEmpty(tags))
+ return "";
+
+ return tags.stream().map(t -> " *" + t.getName())
+ .collect(Collectors.joining());
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/util/ThreadHelper.java b/juick-server/src/main/java/com/juick/util/ThreadHelper.java
new file mode 100644
index 00000000..64a56f7c
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/util/ThreadHelper.java
@@ -0,0 +1,37 @@
+package com.juick.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Created by aalexeev on 11/11/16.
+ */
+public class ThreadHelper {
+ private ThreadHelper() {
+ throw new IllegalStateException();
+ }
+
+ private static Logger logger = LoggerFactory.getLogger(ThreadHelper.class);
+
+ public static void shutdownAndAwaitTermination(ExecutorService pool) {
+ pool.shutdown(); // Disable new tasks from being submitted
+ try {
+ // Wait a while for existing tasks to terminate
+ if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
+ logger.info("Calls shutDownNow for pool");
+ pool.shutdownNow(); // Cancel currently executing tasks
+ // Wait a while for tasks to respond to being cancelled
+ if (!pool.awaitTermination(5, TimeUnit.SECONDS))
+ logger.error("Pool did not terminate");
+ }
+ } catch (InterruptedException ie) {
+ // (Re-)Cancel if current thread also interrupted
+ pool.shutdownNow();
+ // Preserve interrupt status
+ Thread.currentThread().interrupt();
+ }
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/util/UserUtils.java b/juick-server/src/main/java/com/juick/util/UserUtils.java
new file mode 100644
index 00000000..611a3b15
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/util/UserUtils.java
@@ -0,0 +1,27 @@
+package com.juick.util;
+
+import java.util.Random;
+
+/**
+ * Created by aalexeev on 11/14/16.
+ */
+public class UserUtils {
+ private UserUtils() {
+ throw new IllegalStateException();
+ }
+
+ private static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ public static String generateHash(final int len) {
+ Random rnd = new Random();
+ StringBuilder sb = new StringBuilder(len);
+ for (int i = 0; i < len; i++) {
+ sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length())));
+ }
+ return sb.toString();
+ }
+
+ public static boolean checkUserNameValid(final String uname) {
+ return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+");
+ }
+}