From 2dc31dcd188d031a6704a613decee88b074aad8f Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Wed, 14 May 2008 23:46:11 +0000 Subject: Moved utility comparators to util package. --- .../com/amazon/carbonado/util/Comparators.java | 315 +++++++++++++++++++++ 1 file changed, 315 insertions(+) create mode 100644 src/main/java/com/amazon/carbonado/util/Comparators.java (limited to 'src/main/java/com/amazon/carbonado/util') diff --git a/src/main/java/com/amazon/carbonado/util/Comparators.java b/src/main/java/com/amazon/carbonado/util/Comparators.java new file mode 100644 index 0000000..c3ba418 --- /dev/null +++ b/src/main/java/com/amazon/carbonado/util/Comparators.java @@ -0,0 +1,315 @@ +/* + * Copyright 2007 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * 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 com.amazon.carbonado.util; + +import java.util.Comparator; + +import org.cojen.classfile.TypeDesc; + +/** + * Collection of utility comparators. + * + * @author Brian S O'Neill + */ +public class Comparators { + /** + * Returns a comparator which can sort single or multi-dimensional arrays + * of primitves or Comparables. + * + * @param unsigned applicable only to arrays of bytes, shorts, ints, or longs + * @return null if unsupported + */ + public static Comparator arrayComparator(Class arrayType, boolean unsigned) { + if (!arrayType.isArray()) { + throw new IllegalArgumentException(); + } + + Comparator c; + + TypeDesc componentType = TypeDesc.forClass(arrayType.getComponentType()); + switch (componentType.getTypeCode()) { + case TypeDesc.BYTE_CODE: + c = unsigned ? UnsignedByteArray : SignedByteArray; + break; + case TypeDesc.SHORT_CODE: + c = unsigned ? UnsignedShortArray : SignedShortArray; + break; + case TypeDesc.INT_CODE: + c = unsigned ? UnsignedIntArray : SignedIntArray; + break; + case TypeDesc.LONG_CODE: + c = unsigned ? UnsignedLongArray : SignedLongArray; + break; + case TypeDesc.BOOLEAN_CODE: + c = BooleanArray; + break; + case TypeDesc.CHAR_CODE: + c = CharArray; + break; + case TypeDesc.FLOAT_CODE: + c = FloatArray; + break; + case TypeDesc.DOUBLE_CODE: + c = DoubleArray; + break; + case TypeDesc.OBJECT_CODE: default: + if (componentType.isArray()) { + c = new ComparatorArray(arrayComparator(componentType.toClass(), unsigned)); + } else if (Comparable.class.isAssignableFrom(componentType.toClass())) { + c = ComparableArray; + } else { + c = null; + } + break; + } + + return (Comparator) c; + } + + private static final Comparator SignedByteArray = new Comparator() { + public int compare(byte[] a, byte[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i UnsignedByteArray = new Comparator() { + public int compare(byte[] a, byte[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i SignedShortArray = new Comparator() { + public int compare(short[] a, short[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i UnsignedShortArray = new Comparator() { + public int compare(short[] a, short[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i SignedIntArray = new Comparator() { + public int compare(int[] a, int[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i UnsignedIntArray = new Comparator() { + public int compare(int[] a, int[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i SignedLongArray = new Comparator() { + public int compare(long[] a, long[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i UnsignedLongArray = new Comparator() { + public int compare(long[] a, long[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i>> 1; + long sbi = bi >>> 1; + if (sai < sbi) { + return -1; + } + if (sai > sbi) { + return 1; + } + return (ai & 1) == 0 ? -1 : 1; + } + } + return a.length - b.length; + } + }; + + private static final Comparator BooleanArray = new Comparator() { + public int compare(boolean[] a, boolean[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i CharArray = new Comparator() { + public int compare(char[] a, char[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i FloatArray = new Comparator() { + public int compare(float[] a, float[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i DoubleArray = new Comparator() { + public int compare(double[] a, double[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i ComparableArray = new Comparator() + { + public int compare(Comparable[] a, Comparable[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i { + private final Comparator mComparator; + + ComparatorArray(Comparator comparator) { + mComparator = comparator; + } + + public int compare(Object[] a, Object[] b) { + int len = Math.min(a.length, b.length); + for (int i=0; i