Yatopia/patches/server/0004-Utilities.patch

80 lines
2.6 KiB
Diff
Raw Normal View History

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: YatopiaMC <yatopiamc@gmail.com>
Date: Fri, 23 Oct 2020 09:20:01 -0700
Subject: [PATCH] Utilities
This patch includes all utilities required by the Yatopia project and its patches.
Co-authored-by: Mykyta Komarnytskyy <nkomarn@hotmail.com>
Co-authored-by: Ivan Pekov <ivan@mrivanplays.com>
diff --git a/src/main/java/net/yatopia/server/util/Constants.java b/src/main/java/net/yatopia/server/util/Constants.java
new file mode 100644
index 0000000000000000000000000000000000000000..5b2ac2bd00c49e44f5692be42e483409a3b70a5c
--- /dev/null
+++ b/src/main/java/net/yatopia/server/util/Constants.java
@@ -0,0 +1,7 @@
+package net.yatopia.server.util;
+
+public class Constants {
+
+ public static final int[] EMPTY_ARRAY = new int[0];
+ public static final int[] ZERO_ARRAY = new int[]{0};
+}
diff --git a/src/main/java/net/yatopia/server/util/MathUtils.java b/src/main/java/net/yatopia/server/util/MathUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..b1d277d831a36a0f9e5c33f1957e1739de18abb5
--- /dev/null
+++ b/src/main/java/net/yatopia/server/util/MathUtils.java
@@ -0,0 +1,17 @@
+package net.yatopia.server.util;
+
+public class MathUtils {
+
+ /**
+ * An efficient modulo implementation that avoids the use of loops
+ * and instead uses faster bitwise operations to achieve the fastest
+ * possible execution time.
+ *
+ * @param dividend the dividend
+ * @param divisor the divisor
+ * @return the remainder
+ */
+ public static int fastMod(int dividend, int divisor) {
+ return dividend & (divisor - 1);
+ }
+}
diff --git a/src/main/java/net/yatopia/server/util/TimeUtils.java b/src/main/java/net/yatopia/server/util/TimeUtils.java
new file mode 100644
index 0000000000000000000000000000000000000000..d68e8ec871b99f0e6fe1c52948bedf38bd449b27
--- /dev/null
+++ b/src/main/java/net/yatopia/server/util/TimeUtils.java
@@ -0,0 +1,27 @@
+package net.yatopia.server.util;
+
+import java.util.concurrent.TimeUnit;
+
+public class TimeUtils {
+
+ public static String getFriendlyName(TimeUnit unit) {
+ switch (unit) {
+ case NANOSECONDS:
+ return "ns";
+ case MILLISECONDS:
+ return "ms";
+ case MICROSECONDS:
+ return "micros";
+ case SECONDS:
+ return "s";
+ case MINUTES:
+ return "m";
+ case DAYS:
+ return "d";
+ case HOURS:
+ return "h";
+ default:
+ throw new AssertionError();
+ }
+ }
+}