All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.revenj.Guards Maven / Gradle / Ivy

The newest version!
package org.revenj;

import org.w3c.dom.Element;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Stream;

public abstract class Guards {
	public static  void checkNulls(final Iterable values) {
		if (values == null) return;

		int i = 0;
		for (final T value : values) {
			if (value == null)
				throw new IllegalArgumentException("Element at index " + i + " was a null value, which is not permitted.");
			i++;
		}
	}

	public static  void checkNulls(final T[] values) {
		if (values == null) return;

		for (int i = 0; i < values.length; i++) {
			if (values[i] == null)
				throw new IllegalArgumentException("Element at index " + i + " was a null value, which is not permitted.");
		}
	}

	public static  void checkNulls(final List values) {
		if (values == null) return;

		for (int i = 0; i < values.size(); i++) {
			if (values.get(i) == null)
				throw new IllegalArgumentException("Element at index " + i + " was a null value, which is not permitted.");
		}
	}

	public static  void checkNulls(final LinkedList values) {
		if (values == null) return;

		int i = 0;
		for (final T value : values) {
			if (value == null)
				throw new IllegalArgumentException("Element at index " + i + " was a null value, which is not permitted.");
			i++;
		}
	}

	public static void checkScale(final BigDecimal value, final int scale) {
		if (value == null) return;
		try {
			value.setScale(scale);
		} catch (final ArithmeticException e) {
			throw new IllegalArgumentException("Decimal places allowed: " + scale + ". Value: " + value, e);
		}
	}

	public static void checkScale(final BigDecimal[] values, final int scale) {
		if (values == null) return;

		for (int i = 0; i < values.length; i++) {
			final BigDecimal value = values[i];
			if (value == null) continue;
			try {
				value.setScale(scale);
			} catch (final ArithmeticException e) {
				throw new IllegalArgumentException("Invalid value for element at index " + i + ". Decimal places allowed: " + scale + ". Value: " + value, e);
			}
		}
	}

	public static void checkScale(final List values, final int scale) {
		if (values == null) return;

		for (int i = 0; i < values.size(); i++) {
			final BigDecimal value = values.get(i);
			if (value == null) continue;
			try {
				value.setScale(scale);
			} catch (final ArithmeticException e) {
				throw new IllegalArgumentException("Invalid value for element at index " + i + ". Decimal places allowed: " + scale + ". Value: " + value, e);
			}
		}
	}

	public static void checkScale(final LinkedList values, final int scale) {
		if (values == null) return;

		int i = 0;
		for (final BigDecimal value : values) {
			try {
				if (value != null) value.setScale(scale);
			} catch (final ArithmeticException e) {
				throw new IllegalArgumentException(
						"Invalid value for element at index " + i + ". Decimal places allowed: " + scale + ". Value: " + value, e);
			}
			i++;
		}
	}

	public static void checkScale(final Iterable values, final int scale) {
		if (values == null) return;

		int i = 0;
		for (final BigDecimal value : values) {
			try {
				if (value != null) value.setScale(scale);
			} catch (final ArithmeticException e) {
				throw new IllegalArgumentException(
						"Invalid value for element at index " + i + ". Decimal places allowed: " + scale + ". Value: " + value, e);
			}
			i++;
		}
	}

	public static BigDecimal setScale(final BigDecimal value, final int scale) {
		return value.setScale(scale, BigDecimal.ROUND_HALF_UP);
	}

	public static Set setScale(final Set values, final int scale) {
		if (values == null) return null;

		final HashSet result = new HashSet(values.size());
		for (final BigDecimal value : values) result.add(value != null ? setScale(value, scale) : null);
		return result;
	}

	public static BigDecimal[] setScale(final BigDecimal[] values, final int scale) {
		if (values == null) return null;

		final BigDecimal[] result = new BigDecimal[values.length];
		for (int i = 0; i < values.length; i++) {
			final BigDecimal value = values[i];
			result[i] = value != null ? setScale(value, scale) : null;
		}
		return result;
	}

	public static List setScale(final List values, final int scale) {
		if (values == null) return null;

		final ArrayList result = new ArrayList(values.size());
		for (int i = 0; i < values.size(); i++) {
			final BigDecimal value = values.get(i);
			result.add(value != null ? setScale(value, scale) : null);
		}
		return result;
	}

	public static Stack setScale(final Stack values, final int scale) {
		if (values == null) return null;

		final Stack result = new Stack();
		for (int i = 0; i < values.size(); i++) {
			final BigDecimal value = values.get(i);
			result.add(value != null ? setScale(value, scale) : null);
		}
		return result;
	}

	public static Vector setScale(final Vector values, final int scale) {
		if (values == null) return null;

		final Vector result = new Vector();
		for (int i = 0; i < values.size(); i++) {
			final BigDecimal value = values.get(i);
			result.add(value != null ? setScale(value, scale) : null);
		}
		return result;
	}

	public static Queue setScale(final Queue values, final int scale) {
		if (values == null) return null;

		final Queue result = new ArrayDeque();
		for (final BigDecimal value : values) {
			if (value == null)
				throw new NullPointerException("Default Queue implementation (java.util.ArrayDeque) does not support null elements!");
			result.add(setScale(value, scale));
		}
		return result;
	}

	public static LinkedList setScale(final LinkedList values, final int scale) {
		if (values == null) return null;

		final LinkedList result = new LinkedList();
		for (final BigDecimal value : values) result.add(value != null ? setScale(value, scale) : null);
		return result;
	}

	public static void checkLength(final String value, final int length) {
		if (value != null && value.length() > length) throw new IllegalArgumentException(
				"Maximum length allowed: " + length + ". Value: " + value);
	}

	public static void checkLength(final Iterable values, final int length) {
		if (values == null) return;

		int i = 0;
		for (final String value : values) {
			if (value != null && value.length() > length) throw new IllegalArgumentException(
					"Invalid value for element at index " + i + ". Maximum length allowed: " + length + ". Value: " + value);
			i++;
		}
	}

	public static void checkLength(final String[] values, final int length) {
		if (values == null) return;

		for (int i = 0; i < values.length; i++) {
			final String value = values[i];
			if (value != null && value.length() > length) throw new IllegalArgumentException(
					"Invalid value for element at index " + i + ". Maximum length allowed: " + length + ". Value: " + value);
		}
	}

	public static void checkLength(final List values, final int length) {
		if (values == null) return;

		for (int i = 0; i < values.size(); i++) {
			final String value = values.get(i);
			if (value != null && value.length() > length) throw new IllegalArgumentException(
					"Invalid value for element at index " + i + ". Maximum length allowed: " + length + ". Value: " + value);
		}
	}

	public static void checkLength(final LinkedList values, final int length) {
		if (values == null) return;

		int i = 0;
		for (final String value : values) {
			if (value != null && value.length() > length) throw new IllegalArgumentException(
					"Invalid value for element at index " + i + ". Maximum length allowed: " + length + ". Value: " + value);
			i++;
		}
	}

	public static boolean compareBigDecimal(final Iterable left, final Iterable right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		final Iterator leftIterator = left.iterator();
		final Iterator rightIterator = right.iterator();

		while (leftIterator.hasNext() && rightIterator.hasNext()) {
			final BigDecimal l = leftIterator.next();
			final BigDecimal r = rightIterator.next();
			if (!(l == r || l != null && r != null && l.compareTo(r) == 0)) return false;
		}

		return leftIterator.hasNext() == rightIterator.hasNext();
	}

	public static boolean compareBigDecimal(final BigDecimal[] left, final BigDecimal[] right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		if (left.length != right.length) return false;
		for (int i = 0; i < left.length; i++) {
			final BigDecimal l = left[i];
			final BigDecimal r = right[i];
			if (!(l == r || l != null && r != null && l.compareTo(r) == 0)) return false;
		}
		return true;
	}

	private static final Comparator bigDecimalComparator = new Comparator() {
		@Override
		public int compare(final BigDecimal left, final BigDecimal right) {
			return left == null && right == null ? 0
					: left == null ? -1
					: right == null ? 1
					: left.compareTo(right);
		}
	};

	public static boolean compareBigDecimal(final Set left, final Set right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		if (left.size() != right.size()) return false;

		final BigDecimal[] leftSorted = left.toArray(new BigDecimal[left.size()]);
		Arrays.sort(leftSorted, bigDecimalComparator);
		final BigDecimal[] rightSorted = right.toArray(new BigDecimal[right.size()]);
		Arrays.sort(rightSorted, bigDecimalComparator);

		return compareBigDecimal(leftSorted, rightSorted);
	}

	public static boolean compareBinary(final Iterable left, final Iterable right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		final Iterator leftIterator = left.iterator();
		final Iterator rightIterator = right.iterator();

		while (leftIterator.hasNext() && rightIterator.hasNext()) {
			final byte[] l = leftIterator.next();
			final byte[] r = rightIterator.next();
			if (!Arrays.equals(l, r)) return false;
		}

		return leftIterator.hasNext() == rightIterator.hasNext();
	}

	public static boolean compareBinary(final byte[][] left, final byte[][] right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		if (left.length != right.length) return false;

		for (int i = 0; i < left.length; i++) {
			if (!Arrays.equals(left[i], right[i])) return false;
		}
		return true;
	}

	public static boolean compareBinary(final Set left, final Set right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		if (left.size() != right.size()) return false;

		loop:
		for (final byte[] l : left) {
			for (final byte[] r : right) {
				if (Arrays.equals(l, r)) continue loop;
			}
			return false;
		}
		return true;
	}

	public static  boolean compareQueue(final Queue left, final Queue right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		final Iterator leftIterator = left.iterator();
		final Iterator rightIterator = right.iterator();

		while (leftIterator.hasNext() && rightIterator.hasNext()) {
			final T l = leftIterator.next();
			final T r = rightIterator.next();
			if (!(l == r || l != null && r != null && l.equals(r))) return false;
		}

		return leftIterator.hasNext() == rightIterator.hasNext();
	}

	public static boolean compareXml(final Iterable left, final Iterable right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		final Iterator leftIterator = left.iterator();
		final Iterator rightIterator = right.iterator();

		while (leftIterator.hasNext() && rightIterator.hasNext()) {
			final Element l = leftIterator.next();
			final Element r = rightIterator.next();
			if (!(l == r || l != null && r != null && l.isEqualNode(r))) return false;
		}

		return leftIterator.hasNext() == rightIterator.hasNext();
	}

	public static boolean compareXml(final Element[] left, final Element[] right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		if (left.length != right.length) return false;
		for (int i = 0; i < left.length; i++) {
			final Element l = left[i];
			final Element r = right[i];
			if (!(l == r || l != null && r != null && l.isEqualNode(r))) return false;
		}
		return true;
	}

	public static boolean compareXml(final Set left, final Set right) {
		if (left == null && right == null) return true;
		if (left == null || right == null) return false;

		if (left.size() != right.size()) return false;

		loop:
		for (final Element l : left) {
			for (final Element r : right) {
				if (l == r || l != null && r != null && l.isEqualNode(r)) continue loop;
			}
			return false;
		}
		return true;
	}

	public static boolean[] toBooleanArray(List items) {
		if (items == null) return null;
		boolean[] result = new boolean[items.size()];
		for (int i = 0; i < items.size(); i++) {
			result[i] = items.get(i);
		}
		return result;
	}

	public static int[] toInt32Array(List items) {
		if (items == null) return null;
		int[] result = new int[items.size()];
		for (int i = 0; i < items.size(); i++) {
			result[i] = items.get(i);
		}
		return result;
	}

	public static long[] toInt64Array(List items) {
		if (items == null) return null;
		long[] result = new long[items.size()];
		for (int i = 0; i < items.size(); i++) {
			result[i] = items.get(i);
		}
		return result;
	}

	public static float[] toFloatArray(List items) {
		if (items == null) return null;
		float[] result = new float[items.size()];
		for (int i = 0; i < items.size(); i++) {
			result[i] = items.get(i);
		}
		return result;
	}

	public static double[] toDoubleArray(List items) {
		if (items == null) return null;
		double[] result = new double[items.size()];
		for (int i = 0; i < items.size(); i++) {
			result[i] = items.get(i);
		}
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy