Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava 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 com.google.common.primitives;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
Static utility methods pertaining to byte primitives that interpret values as signed. The corresponding methods that treat the values as unsigned are found in UnsignedBytes, and the methods for which signedness is not an issue are in Bytes.

See the Guava User Guide article on primitive utilities.

Author(s):
Kevin Bourrillion
Since:
1.0
 
 // TODO(kevinb): how to prevent warning on UnsignedBytes when building GWT
 // javadoc?
 public final class SignedBytes {
   private SignedBytes() {}

  
The largest power of two that can be represented as a signed byte.

Since:
10.0
 
   public static final byte MAX_POWER_OF_TWO = 1 << 6;
  
  
Returns the byte value that is equal to value, if possible.

Parameters:
value any value in the range of the byte type
Returns:
the byte value that equals value
Throws:
java.lang.IllegalArgumentException if value is greater than java.lang.Byte.MAX_VALUE or less than java.lang.Byte.MIN_VALUE
 
   public static byte checkedCast(long value) {
     byte result = (bytevalue;
     checkArgument(result == value"Out of range: %s"value);
     return result;
   }

  
Returns the byte nearest in value to value.

Parameters:
value any long value
Returns:
the same value cast to byte if it is in the range of the byte type, java.lang.Byte.MAX_VALUE if it is too large, or java.lang.Byte.MIN_VALUE if it is too small
 
   public static byte saturatedCast(long value) {
     if (value > .) {
       return .;
     }
     if (value < .) {
       return .;
     }
     return (bytevalue;
   }

  
Compares the two specified byte values. The sign of the value returned is the same as that of ((Byte) a).compareTo(b).

Parameters:
a the first byte to compare
b the second byte to compare
Returns:
a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
 
   public static int compare(byte abyte b) {
     return a - b// safe due to restricted range
   }

  
Returns the least value present in array.

Parameters:
array a nonempty array of byte values
Returns:
the value present in array that is less than or equal to every other value in the array
Throws:
java.lang.IllegalArgumentException if array is empty
  public static byte min(byte... array) {
    checkArgument(array.length > 0);
    byte min = array[0];
    for (int i = 1; i < array.lengthi++) {
      if (array[i] < min) {
        min = array[i];
      }
    }
    return min;
  }

  
Returns the greatest value present in array.

Parameters:
array a nonempty array of byte values
Returns:
the value present in array that is greater than or equal to every other value in the array
Throws:
java.lang.IllegalArgumentException if array is empty
  public static byte max(byte... array) {
    checkArgument(array.length > 0);
    byte max = array[0];
    for (int i = 1; i < array.lengthi++) {
      if (array[i] > max) {
        max = array[i];
      }
    }
    return max;
  }

  
Returns a string containing the supplied byte values separated by separator. For example, join(":", 0x01, 0x02, -0x01) returns the string "1:2:-1".

Parameters:
separator the text that should appear between consecutive values in the resulting string (but not at the start or end)
array an array of byte values, possibly empty
  public static String join(String separatorbyte... array) {
    checkNotNull(separator);
    if (array.length == 0) {
      return "";
    }
    // For pre-sizing a builder, just get the right order of magnitude
    StringBuilder builder = new StringBuilder(array.length * 5);
    builder.append(array[0]);
    for (int i = 1; i < array.lengthi++) {
      builder.append(separator).append(array[i]);
    }
    return builder.toString();
  }

  
Returns a comparator that compares two byte arrays lexicographically. That is, it compares, using compare(byte,byte)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example, [] < [0x01] < [0x01, 0x80] < [0x01, 0x7F] < [0x02]. Values are treated as signed.

The returned comparator is inconsistent with java.lang.Object.equals(java.lang.Object) (since arrays support only identity equality), but it is consistent with java.util.Arrays.equals(byte[],byte[]).

Since:
2.0
See also:
Lexicographical order article at Wikipedia
  public static Comparator<byte[]> lexicographicalComparator() {
  }
  private enum LexicographicalComparator implements Comparator<byte[]> {
    INSTANCE;
    @Override
    public int compare(byte[] leftbyte[] right) {
      int minLength = Math.min(left.lengthright.length);
      for (int i = 0; i < minLengthi++) {
        int result = SignedBytes.compare(left[i], right[i]);
        if (result != 0) {
          return result;
        }
      }
      return left.length - right.length;
    }
  }
New to GrepCode? Check out our FAQ X