package vg.civcraft.mc.civmodcore.utilities;

import com.google.common.base.Strings;
import javax.annotation.Nonnull;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;

/**
 * Utility class to make dealing with namespace keys easier.
 */
@UtilityClass
public final class KeyedUtils {

	/**
	 * Converts a stringified namespace back into a {@link NamespacedKey}.
	 *
	 * @param key The stringified namespace, which MUST be formatted as {@code namespace:name}
	 * @return Returns a valid {@link NamespacedKey}, or null.
	 *
	 * @exception IllegalArgumentException Will throw if the stringified key fails a "[a-z0-9._-]+" check, or if the
	 *     total length is longer than 256.
	 */
	public static NamespacedKey fromString(final String key) {
		if (Strings.isNullOrEmpty(key)) {
			return null;
		}
		final String[] parts = StringUtils.split(key, ":");
		if (parts.length != 2) {
			return null;
		}
		return fromParts(parts[0], parts[1]);
	}

	/**
	 * Converts a namespace and a key into a {@link NamespacedKey}.
	 *
	 * @param namespace The namespace name.
	 * @param key The namespaced key.
	 * @return Returns a valid {@link NamespacedKey}, or null.
	 *
	 * @exception IllegalArgumentException Will throw if either part fails a "[a-z0-9._-]+" check, or if the total
	 *     combined length is longer than 256.
	 */
	@SuppressWarnings("deprecation")
	public static NamespacedKey fromParts(final String namespace, final String key) {
		if (Strings.isNullOrEmpty(namespace) || Strings.isNullOrEmpty(key)) {
			return null;
		}
		return new NamespacedKey(namespace, key);
	}

	/**
	 * Converts a {@link NamespacedKey} into a string.
	 *
	 * @param key The {@link NamespacedKey} to convert.
	 * @return Returns the stringified {@link NamespacedKey}, or null.
	 */
	public static String getString(final NamespacedKey key) {
		if (key == null) {
			return null;
		}
		return key.toString();
	}

	/**
	 * Retrieves a {@link Keyed}'s {@link NamespacedKey} and converts it to a string.
	 *
	 * @param keyed The {@link Keyed} instance.
	 * @return Returns the stringified {@link Keyed}'s {@link NamespacedKey}, or null.
	 */
	public static String getString(final Keyed keyed) {
		if (keyed == null) {
			return null;
		}
		return getString(keyed.getKey());
	}

	/**
	 * @param key The namespace'd key.
	 * @return Returns a new {@link NamespacedKey} for testing purposes.
	 */
	@SuppressWarnings("deprecation")
	@Nonnull
	public static NamespacedKey testKey(@Nonnull final String key) {
		return new NamespacedKey("test", key);
	}

}

@@ -50,38 +50,32 @@
 ort 
-com.google.common.base.Strings
+javax.annotation.Nonnull
 ;%0Aim
@@ -89,38 +89,39 @@
 vax.annotation.N
-onn
 ull
+able
 ;%0Aimport lombok.
@@ -151,53 +151,8 @@
 ss;%0A
-import org.apache.commons.lang3.StringUtils;%0A
 impo
@@ -363,16 +363,21 @@
 amespace
+d-key
  back in
@@ -448,16 +448,20 @@
 amespace
+-key
 , which 
@@ -681,24 +681,31 @@
 r if the%0A%09 *
+       
      total l
@@ -731,24 +731,35 @@
 n 256.%0A%09 */%0A
+%09@Nullable%0A
 %09public stat
@@ -782,24 +782,34 @@
  fromString(
+@Nullable 
 final String
@@ -822,200 +822,64 @@
 %7B%0A%09%09
-if (Strings.isNullOrEmpty(key)) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09final String%5B%5D parts = StringUtils.split(key, %22:%22);%0A%09%09if (parts.length != 2) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return fromParts(parts%5B0%5D, parts%5B1%5D
+return key == null ? null : NamespacedKey.fromString(key
 );%0A%09
@@ -1290,16 +1290,26 @@
 ation%22)%0A
+%09@Nonnull%0A
 %09public 
@@ -1339,16 +1339,25 @@
 omParts(
+@Nonnull 
 final St
@@ -1372,16 +1372,25 @@
 espace, 
+@Nonnull 
 final St
@@ -1398,107 +1398,15 @@
 ing 
-key) %7B%0A%09%09if (Strings.isNullOrEmpty(namespace) %7C%7C Strings.isNullOrEmpty(
 key)
-)
  %7B%0A
-%09%09%09return null;%0A%09%09%7D%0A
 %09%09re
@@ -1628,32 +1628,43 @@
 , or null.%0A%09 */%0A
+%09@Nullable%0A
 %09public static S
@@ -1675,24 +1675,34 @@
 g getString(
+@Nullable 
 final Namesp
@@ -1718,20 +1718,23 @@
 ey) %7B%0A%09%09
-if (
+return 
 key == n
@@ -1740,40 +1740,17 @@
 null
-) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return
+ ? null :
  key
@@ -1992,16 +1992,27 @@
 l.%0A%09 */%0A
+%09@Nullable%0A
 %09public 
@@ -2035,16 +2035,26 @@
 tString(
+@Nullable 
 final Ke
@@ -2072,12 +2072,15 @@
 %7B%0A%09%09
-if (
+return 
 keye
@@ -2092,40 +2092,17 @@
 null
-) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return
+ ? null :
  get
@@ -2166,11 +2166,10 @@
 pace
-'d 
+d-
 key.

Update KeyedUtils