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.
This commit is contained in:
John Blum
2018-11-28 14:31:26 -08:00
parent 76f11970f5
commit c97690893f
4 changed files with 206 additions and 0 deletions

View File

@@ -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<byte[]> {
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;
}
}

View File

@@ -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<Object> {
public static final IdentityComparator INSTANCE = new IdentityComparator();
@Override
public int compare(Object objectOne, Object objectTwo) {
return objectOne == objectTwo ? 0 : ObjectToByteArrayComparator.INSTANCE.compare(objectOne, objectTwo);
}
}

View File

@@ -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<Object> {
@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;
}
}

View File

@@ -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<Object> {
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);
}
}
}