fork download
  1. class EqualityUtils {
  2.  
  3. private EqualityUtils() {}
  4.  
  5. public static boolean pairsEqual(Object... objects) {
  6. if (objects.length < 2) {
  7. throw new IllegalArgumentException("At least two arguments must be provided.");
  8. }
  9. if (objects.length % 2 == 1) {
  10. throw new IllegalArgumentException("Even number of arguments is required.");
  11. }
  12. int i = 0;
  13. while (i < objects.length) {
  14. if (!objectsEqual(objects[i], objects[i + 1])) {
  15. return false;
  16. }
  17. i += 2;
  18. }
  19. return true;
  20. }
  21.  
  22. public static boolean objectsEqual(Object a, Object b) {
  23. return a == b || (a != null && a.equals(b));
  24. }
  25.  
  26. public static Object println(String message) {
  27. System.out.println(message);
  28. return message;
  29. }
  30.  
  31. public static void main(String... args) {
  32. System.out.println(pairsEqual(1, 2, println("Hello"), println("Hello")));
  33. System.out.println(pairsEqual(1, 1, println("Hello"), println("Hello")));
  34. }
  35.  
  36. }
  37.  
Success #stdin #stdout 0.08s 212416KB
stdin
Standard input is empty
stdout
Hello
Hello
false
Hello
Hello
true