From c97690893f37b61e73bf809c47560fb0292781a6 Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 28 Nov 2018 14:31:26 -0800 Subject: [PATCH] Add new java.util.Comparator implementation supporting Object identity comparisons. Add new java.util.Comparator implementation supporting Object identity hash code comparisons. Add new java.util.Comparator implementation supporting (Serializable Object to) byte array comparisons. --- .../tests/util/ByteArrayComparator.java | 65 +++++++++++++++++++ .../tests/util/IdentityComparator.java | 36 ++++++++++ .../util/IdentityHashCodeComparator.java | 41 ++++++++++++ .../util/ObjectToByteArrayComparator.java | 64 ++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ByteArrayComparator.java create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityComparator.java create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityHashCodeComparator.java create mode 100644 spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ObjectToByteArrayComparator.java diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ByteArrayComparator.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ByteArrayComparator.java new file mode 100644 index 0000000..d6191a0 --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ByteArrayComparator.java @@ -0,0 +1,65 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.data.gemfire.tests.util; + +import java.util.Comparator; + +import org.apache.shiro.util.Assert; + +/** + * The ByteArrayComparator class... + * + * @author John Blum + * @since 1.0.0 + */ +public class ByteArrayComparator implements Comparator { + + public static final ByteArrayComparator INSTANCE = new ByteArrayComparator(); + + @Override + public int compare(byte[] bytesOne, byte[] bytesTwo) { + + int bytesOneLength = bytesOne.length; + int bytesTwoLength = bytesTwo.length; + + // Cannot use subtraction; Must be careful of overflow/underflow. + return bytesOneLength < bytesTwoLength ? -1 + : bytesOneLength > bytesTwoLength ? 1 + : compareByteByByte(bytesOne, bytesTwo); + } + + private int compareByteByByte(byte[] bytesOne, byte[] bytesTwo) { + + String errorMessage = + "The length of the byte arrays do no match; byte array 1 is [%d] and byte array 2 is [%d]"; + + Assert.isTrue(bytesOne.length == bytesTwo.length, + String.format(errorMessage, bytesOne.length, bytesTwo.length)); + + for (int index = 0; index < bytesOne.length; index++) { + + // Subtraction is OK here since a byte cannot cause an int to either overflow or underflow. + int diff = (int) bytesOne[index] - (int) bytesTwo[index]; + + if (diff != 0) { + return diff; + } + } + + return 0; + } +} diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityComparator.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityComparator.java new file mode 100644 index 0000000..c49f16f --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityComparator.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.data.gemfire.tests.util; + +import java.util.Comparator; + +/** + * The IdentityComparator class... + * + * @author John Blum + * @since 1.0.0 + */ +@SuppressWarnings("unused") +public class IdentityComparator implements Comparator { + + public static final IdentityComparator INSTANCE = new IdentityComparator(); + + @Override + public int compare(Object objectOne, Object objectTwo) { + return objectOne == objectTwo ? 0 : ObjectToByteArrayComparator.INSTANCE.compare(objectOne, objectTwo); + } +} diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityHashCodeComparator.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityHashCodeComparator.java new file mode 100644 index 0000000..90fefe0 --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/IdentityHashCodeComparator.java @@ -0,0 +1,41 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.data.gemfire.tests.util; + +import java.util.Comparator; + +/** + * The IdentityHashCodeComparator class... + * + * @author John Blum + * @since 1.0.0 + */ +@SuppressWarnings("unused") +public class IdentityHashCodeComparator implements Comparator { + + @Override + public int compare(Object objectOne, Object objectTwo) { + + int objectOneHashCode = System.identityHashCode(objectOne); + int objectTwoHashCode = System.identityHashCode(objectTwo); + + // Cannot use subtraction; Must be careful of overflow/underflow. + return objectOneHashCode < objectTwoHashCode ? -1 + : objectOneHashCode > objectTwoHashCode ? 1 + : 0; + } +} diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ObjectToByteArrayComparator.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ObjectToByteArrayComparator.java new file mode 100644 index 0000000..7e8a15d --- /dev/null +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/util/ObjectToByteArrayComparator.java @@ -0,0 +1,64 @@ +/* + * Copyright 2018 the original author or authors. + * + * 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 org.springframework.data.gemfire.tests.util; + +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Comparator; + +import org.apache.shiro.util.Assert; + +/** + * The ObjectToByteArrayComparator class... + * + * @author John Blum + * @since 1.0.0 + */ +public class ObjectToByteArrayComparator implements Comparator { + + public static final ObjectToByteArrayComparator INSTANCE = new ObjectToByteArrayComparator(); + + @Override + public int compare(Object objectOne, Object objectTwo) { + + return ByteArrayComparator.INSTANCE + .compare(fromSerializableToByteArray(objectOne), fromSerializableToByteArray(objectTwo)); + } + + private Serializable assertSerializable(Object target) { + + Assert.isInstanceOf(Serializable.class, target); + + return (Serializable) target; + } + + private byte[] fromSerializableToByteArray(Object target) { + return toByteArray(assertSerializable(target)); + } + + private byte[] toByteArray(Object target) { + + try { + return IOUtils.serializeObject(assertSerializable(target)); + } + catch (IOException cause) { + throw newIllegalArgumentException(cause, "Object [%s] could not be serialized", target); + } + } +}