/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.datakernel.util;

public class Preconditions {
	private Preconditions() {}

	public static void check(boolean expression) {
		if (!expression) {
			throw new RuntimeException();
		}
	}

	public static void check(boolean expression, Object message) {
		if (!expression) {
			throw new RuntimeException(String.valueOf(message));
		}
	}

	public static void check(boolean expression, String template, Object... args) {
		if (!expression) {
			throw new RuntimeException(format(template, args));
		}
	}

	public static <T> T checkNotNull(T reference) {
		if (reference == null) {
			throw new NullPointerException();
		}
		return reference;
	}

	public static <T> T checkNotNull(T reference, Object message) {
		if (reference == null) {
			throw new NullPointerException(String.valueOf(message));
		}
		return reference;
	}

	public static <T> T checkNotNull(T reference, String template, Object... args) {
		if (reference == null) {
			throw new NullPointerException(format(template, args));
		}
		return reference;
	}

	static String format(String template, Object... args) {
		template = String.valueOf(template);

		StringBuilder sb = new StringBuilder(template.length() + 16 * args.length);
		int templateStart = 0;
		int i = 0;
		while (i < args.length) {
			int holderStart = template.indexOf("%s", templateStart);
			if (holderStart == -1) {
				break;
			}
			sb.append(template.substring(templateStart, holderStart));
			sb.append(args[(i++)]);
			templateStart = holderStart + 2;
		}
		sb.append(template.substring(templateStart));
		if (i < args.length) {
			sb.append(" {");
			sb.append(args[(i++)]);
			while (i < args.length) {
				sb.append(", ");
				sb.append(args[(i++)]);
			}
			sb.append('}');
		}
		return sb.toString();
	}
}

@@ -628,16 +628,22 @@
 %0Apublic 
+final 
 class Pr
@@ -1068,32 +1068,39 @@
 untimeException(
+String.
 format(template,
@@ -1580,16 +1580,23 @@
 ception(
+String.
 format(t
@@ -1645,77 +1645,252 @@
 %7D%0A%0A%09
-static String format(String template, Object... args) %7B%0A%09%09template = 
+public static void checkState(boolean expression) %7B%0A%09%09if (!expression) %7B%0A%09%09%09throw new IllegalStateException();%0A%09%09%7D%0A%09%7D%0A%0A%09public static void checkState(boolean expression, Object message) %7B%0A%09%09if (!expression) %7B%0A%09%09%09throw new IllegalStateException(
 Stri
@@ -1904,103 +1904,84 @@
 eOf(
-templat
+messag
 e)
+)
 ;%0A
-%0A%09%09StringBuilder sb = new StringBuilder(template.length() + 16 * args.length);%0A%09%09int
+%09%09%7D%0A%09%7D%0A%0A%09public static void checkState(boolean expression, String
  tem
@@ -1989,332 +1989,484 @@
 late
-Start = 0;%0A%09%09int i = 0;%0A%09%09while (i %3C args.length) %7B%0A%09%09%09int holderStart = template.indexOf(%22%25s%22, templateStart);%0A%09%09%09if (holderStart == -1) %7B%0A%09%09%09%09break
+, Object... args) %7B%0A%09%09if (!expression) %7B%0A%09%09%09throw new IllegalStateException(String.format(template, args));%0A%09%09%7D%0A%09%7D%0A%0A%09public static void checkArgument(boolean expression) %7B%0A%09%09if (!expression) %7B%0A%09%09%09throw new IllegalArgumentException()
 ;%0A%09%09
-%09
 %7D%0A%09
-%09%09sb.append(template.substring(templateStart, holderStart));%0A%09%09%09sb.append(args%5B(i++)%5D);%0A%09%09%09templateStart = holderStart + 2;%0A%09%09%7D%0A%09%09sb.append(template.subs
+%7D%0A%0A%09public static void checkArgument(boolean expression, Object message) %7B%0A%09%09if (!expression) %7B%0A%09%09%09throw new IllegalArgumentException(String.valueOf(message));%0A%09%09%7D%0A%09%7D%0A%0A%09public static void checkArgument(boolean expression, S
 tring
-(
+ 
 template
 Star
@@ -2465,218 +2465,122 @@
 late
-Start));%0A%09%09if (i %3C args.length) %7B%0A%09%09%09sb.append(%22 %7B%22);%0A%09%09%09sb.append(args%5B(i++)%5D);%0A%09%09%09while (i %3C args.length) %7B%0A%09%09%09%09sb.append(%22, %22);%0A%09%09%09%09sb.append(args%5B(i++)%5D);%0A%09%09%09%7D%0A%09%09%09sb.append('%7D');%0A%09%09%7D%0A%09%09return sb.toString();
+, Object... args) %7B%0A%09%09if (!expression) %7B%0A%09%09%09throw new IllegalArgumentException(String.format(template, args));%0A%09%09%7D
 %0A%09%7D%0A

upgrade preconditions in eventloop