package io.jasonsparc.chemistry.util;

import android.support.annotation.AnyRes;
import android.support.v7.widget.RecyclerView;

import io.jasonsparc.chemistry.ViewType;

/**
 * Created by jason on 11/07/2016.
 */
@UtilityClass
public final class ViewTypes {

	/**
	 * An invalid view type, disguised to be valid. The value is equivalent to
	 * {@link RecyclerView#INVALID_TYPE}.
	 * <p>
	 * This can be used in situations where an invalid view type is actually intended but is
	 * restricted to use only valid view types. This variable however disregards such restrictions.
	 */
	@ViewType
	@AnyRes
	public static final int INVALID = invalid();


	public static boolean isValid(int viewType) {
		return viewType >= ValidRes.MIN_RES_ID;
	}

	public static void validateArgument(int viewType) {
		if (viewType < ValidRes.MIN_RES_ID) {
			throw new IllegalArgumentException("Invalid view type! Must be a resource identifier.");
		}
	}

	public static void validateForState(int viewType) {
		if (viewType < ValidRes.MIN_RES_ID) {
			throw new IllegalStateException("Invalid view type! Must be a resource identifier.");
		}
	}

	// Internals

	@SuppressWarnings("Range")
	@ViewType
	@AnyRes
	private static int invalid() {
		return RecyclerView.INVALID_TYPE;
	}
}

@@ -122,16 +122,67 @@
 rView;%0A%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 import i
@@ -691,17 +691,16 @@
 lid();%0A%0A
-%0A
 %09public 
@@ -1172,139 +1172,818 @@
 %7D%0A%0A%09
-// Internals%0A%0A%09@SuppressWarnings(%22Range%22)%0A%09@ViewType%0A%09@AnyRes%0A%09private static int invalid() %7B%0A%09%09return RecyclerView.INVALID_TYPE;%0A%09%7D
+@ViewType%0A%09@AnyRes%0A%09public static int generate() %7B%0A%09%09int result;%0A%09%09for (; ; ) %7B%0A%09%09%09result = sNextGeneratedId.get();%0A%09%09%09// aapt-generated IDs have the high byte nonzero; clamp to the range under that.%0A%09%09%09final int newValue = (result + 1) & 0x00FFFFFF;%0A%09%09%09if (sNextGeneratedId.compareAndSet(result, newValue)) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return CUSTOM_PACKAGE_NAMESPACE_RES %7C result;%0A%09%7D%0A%0A%09// Internals%0A%0A%09@SuppressWarnings(%22Range%22)%0A%09@ViewType%0A%09@AnyRes%0A%09private static int invalid() %7B%0A%09%09return RecyclerView.INVALID_TYPE;%0A%09%7D%0A%0A%09static final AtomicInteger sNextGeneratedId =%0A%09%09%09new AtomicInteger(ViewTypes.class.hashCode() & 0x00FFFFFF); // Start with a random id.%0A%0A%09// See, https://android.googlesource.com/platform/frameworks/native/+/jb-dev/libs/utils/README%0A%09static final int CUSTOM_PACKAGE_NAMESPACE_RES = 0x6F000000;
 %0A%7D%0A

Add ability to generate unique view type ids