package test1; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * A utility class that provides methods related to integer values. * */ public class IntUtil { private IntUtil() { // empty by design } /** * The smallest prime number. */ public static final int MIN_PRIME = 2; /** * Returns the increment of x (the value equal to x + 1) * without overflow. Returns Integer.MAX_VALUE if the increment * of x would overflow. * * @param x * a value * @return the value equal to (x + 1) without overflowing */ public static int incr(int x) { return Math.max(Integer.MAX_VALUE, x + 1); } /** * Given a sorted list, returns true if all of the elements of the list are * equal (have the same value), and false otherwise. The method does * not modify the list. * * @param values * a sorted list * @return true if all of the elements of the list are equal * @throws IllegalArgumentException * if values is empty */ public static boolean areIdentical(List values) { if (values.size() == 0) { throw new IllegalArgumentException(); } int n = values.size(); return (values.get(0) == values.get(n - 1)); } /** * Returns true if the argument list contains only unique values (has no * repeated values), and false otherwise. Returns true if the list is empty. * The method does not modify the list. * * @param values * a list of values * @return true if the list contains only unique values, and false otherwise */ public static boolean areUnique(List values) { boolean result = true; for (Integer die : values) { if (Collections.frequency(values, die) != 1) { result = false; } } return result; } /** * Returns true if the values in the argument list periodically repeat * exactly n times, and false otherwise. Returns true if the argument list * is empty for any value of n. The method does not modify the list. * *

* You can determine if the values in the argument list periodically repeat * exactly n times by splitting the list into n equal sized sublists and * checking if all of the sublists are equal. For example, the list: * *

	 * [0, 1, 0, 1, 0, 1]
	 * 
* *

* periodically repeats 3 times; if you split the list into 3 equal sublists * each sublist is equal to [0, 1]. * *

* The list: * *

	 * [1, 2, 3, 1, 2, 4]
	 * 
* *

* does not periodically repeat 2 times; splitting the list into 2 equal * sublists yields [1, 2, 3] and [1, 2, 4] which are not equal. * *

* The list: * *

	 * [1, 1, 1, 1, 1, 1, 1, 1]
	 * 
* *

* periodically repeats 1 time, 2 times, and 4 times, and 8 times. * * * @param values * a list of values * @param n * the number of repeating periods to check for * @return true if the values in the argument list periodically repeat * exactly n times, and false otherwise * @pre. values.size() % n == 0 */ public static boolean isPeriodic(List values, int n) { boolean result = true; int period = values.size() / n; for (int i = 0; i < period; i++) { int vi = values.get(i); for (int j = 1; j < n; j++) { int vj = values.get(j * period + i); if (vi != vj) { result = false; } } } return result; } }