1bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor/* 2bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Copyright (C) 2009 Google Inc. 3bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 4bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Licensed under the Apache License, Version 2.0 (the "License"); 5bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * you may not use this file except in compliance with the License. 6bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * You may obtain a copy of the License at 7bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 8bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * http://www.apache.org/licenses/LICENSE-2.0 9bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 10bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Unless required by applicable law or agreed to in writing, software 11bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * distributed under the License is distributed on an "AS IS" BASIS, 12bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * See the License for the specific language governing permissions and 14bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * limitations under the License. 15bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 16bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 17bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorpackage com.google.common.primitives; 18bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 19bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport com.google.common.annotations.GwtCompatible; 20bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 21bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport java.util.Comparator; 22bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 23bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport static com.google.common.base.Preconditions.checkArgument; 24bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorimport static com.google.common.base.Preconditions.checkNotNull; 25bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 26bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor/** 27bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Static utility methods pertaining to {@code byte} primitives that 28bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * interpret values as signed. The corresponding methods that treat the values 29bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * as unsigned are found in {@link UnsignedBytes}, and the methods for which 30bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * signedness is not an issue are in {@link Bytes}. 31bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 32bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @author Kevin Bourrillion 33bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @since 2009.09.15 <b>tentative</b> 34bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 35bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor@GwtCompatible 36bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorpublic final class SignedBytes { 37bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor private SignedBytes() {} 38bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 39bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 40bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the {@code byte} value that is equal to {@code value}, if possible. 41bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 42bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param value any value in the range of the {@code byte} type 43bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return the {@code byte} value that equals {@code value} 44bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @throws IllegalArgumentException if {@code value} is greater than {@link 45bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Byte#MAX_VALUE} or less than {@link Byte#MIN_VALUE} 46bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 47bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static byte checkedCast(long value) { 48bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor byte result = (byte) value; 49bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkArgument(result == value, "Out of range: %s", value); 50bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return result; 51bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 52bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 53bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 54bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the {@code byte} nearest in value to {@code value}. 55bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 56bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param value any {@code long} value 57bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return the same value cast to {@code byte} if it is in the range of the 58bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * {@code byte} type, {@link Byte#MAX_VALUE} if it is too large, 59bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * or {@link Byte#MIN_VALUE} if it is too small 60bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 61bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static byte saturatedCast(long value) { 62bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (value > Byte.MAX_VALUE) { 63bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return Byte.MAX_VALUE; 64bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 65bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (value < Byte.MIN_VALUE) { 66bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return Byte.MIN_VALUE; 67bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 68bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return (byte) value; 69bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 70bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 71bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 72bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Compares the two specified {@code byte} values. The sign of the value 73bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * returned is the same as that of {@code ((Byte) a).compareTo(b)}. 74bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 75bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param a the first {@code byte} to compare 76bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param b the second {@code byte} to compare 77bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return a negative value if {@code a} is less than {@code b}; a positive 78bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * value if {@code a} is greater than {@code b}; or zero if they are equal 79bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 80bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static int compare(byte a, byte b) { 81bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return a - b; // safe due to restricted range 82bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 83bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 84bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 85bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the least value present in {@code array}. 86bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 87bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param array a <i>nonempty</i> array of {@code byte} values 88bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return the value present in {@code array} that is less than or equal to 89bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * every other value in the array 90bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @throws IllegalArgumentException if {@code array} is empty 91bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 92bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static byte min(byte... array) { 93bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkArgument(array.length > 0); 94bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor byte min = array[0]; 95bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor for (int i = 1; i < array.length; i++) { 96bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (array[i] < min) { 97bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor min = array[i]; 98bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 99bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 100bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return min; 101bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 102bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 103bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 104bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns the greatest value present in {@code array}. 105bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 106bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param array a <i>nonempty</i> array of {@code byte} values 107bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @return the value present in {@code array} that is greater than or equal to 108bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * every other value in the array 109bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @throws IllegalArgumentException if {@code array} is empty 110bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 111bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static byte max(byte... array) { 112bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkArgument(array.length > 0); 113bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor byte max = array[0]; 114bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor for (int i = 1; i < array.length; i++) { 115bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (array[i] > max) { 116bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor max = array[i]; 117bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 118bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 119bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return max; 120bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 121bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 122bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 123bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns a string containing the supplied {@code byte} values separated 124bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * by {@code separator}. For example, {@code join(":", 0x01, 0x02, -0x01)} 125bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * returns the string {@code "1:2:-1"}. 126bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 127bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param separator the text that should appear between consecutive values in 128bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * the resulting string (but not at the start or end) 129bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @param array an array of {@code byte} values, possibly empty 130bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 131bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static String join(String separator, byte... array) { 132bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor checkNotNull(separator); 133bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (array.length == 0) { 134bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return ""; 135bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 136bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 137bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor // For pre-sizing a builder, just get the right order of magnitude 138bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor StringBuilder builder = new StringBuilder(array.length * 5); 139bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor builder.append(array[0]); 140bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor for (int i = 1; i < array.length; i++) { 141bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor builder.append(separator).append(array[i]); 142bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 143bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return builder.toString(); 144bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 145bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 146bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor /** 147bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Returns a comparator that compares two {@code byte} arrays 148bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * lexicographically. That is, it compares, using {@link 149bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * #compare(byte, byte)}), the first pair of values that follow any common 150bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * prefix, or when one array is a prefix of the other, treats the shorter 151bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * array as the lesser. For example, {@code [] < [0x01] < [0x01, 0x80] < 152bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * [0x01, 0x7F] < [0x02]}. Values are treated as signed. 153bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 154bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * <p>The returned comparator is inconsistent with {@link 155bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Object#equals(Object)} (since arrays support only identity equality), but 156bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * it is consistent with {@link java.util.Arrays#equals(byte[], byte[])}. 157bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * 158bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @see <a href="http://en.wikipedia.org/wiki/Lexicographical_order"> 159bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * Lexicographical order</a> article at Wikipedia 160bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor * @since 2010.01.04 <b>tentative</b> 161bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor */ 162bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public static Comparator<byte[]> lexicographicalComparator() { 163bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return LexicographicalComparator.INSTANCE; 164bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 165bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 166bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor private enum LexicographicalComparator implements Comparator<byte[]> { 167bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor INSTANCE; 168bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor 169bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor public int compare(byte[] left, byte[] right) { 170bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor int minLength = Math.min(left.length, right.length); 171bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor for (int i = 0; i < minLength; i++) { 172bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor int result = SignedBytes.compare(left[i], right[i]); 173bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor if (result != 0) { 174bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return result; 175bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 176bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 177bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor return left.length - right.length; 178bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 179bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor } 180bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor} 181