Java源码示例:com.google.common.primitives.SignedBytes
示例1
@Override
public void writeBlock(Block block)
{
checkState(!closed);
checkArgument(block.getPositionCount() > 0, "Block is empty");
// record nulls
for (int position = 0; position < block.getPositionCount(); position++) {
presentStream.writeBoolean(!block.isNull(position));
}
// record values
for (int position = 0; position < block.getPositionCount(); position++) {
if (!block.isNull(position)) {
dataStream.writeByte(SignedBytes.checkedCast(type.getLong(block, position)));
nonNullValueCount++;
}
}
}
示例2
public static Long currentTokenAsTinyint(JsonParser parser)
throws IOException
{
switch (parser.currentToken()) {
case VALUE_NULL:
return null;
case VALUE_STRING:
case FIELD_NAME:
return VarcharOperators.castToTinyint(Slices.utf8Slice(parser.getText()));
case VALUE_NUMBER_FLOAT:
return DoubleOperators.castToTinyint(parser.getDoubleValue());
case VALUE_NUMBER_INT:
return (long) SignedBytes.checkedCast(parser.getLongValue());
case VALUE_TRUE:
return BooleanOperators.castToTinyint(true);
case VALUE_FALSE:
return BooleanOperators.castToTinyint(false);
default:
throw new JsonCastException(format("Unexpected token when cast to %s: %s", StandardTypes.TINYINT, parser.getText()));
}
}
示例3
@UsedByGeneratedCode
public static long shortDecimalToTinyint(long decimal, long precision, long scale, long tenToScale)
{
// this rounds the decimal value to the nearest integral value
long longResult = (decimal + tenToScale / 2) / tenToScale;
if (decimal < 0) {
longResult = -((-decimal + tenToScale / 2) / tenToScale);
}
try {
return SignedBytes.checkedCast(longResult);
}
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", longResult));
}
}
示例4
private static long normalizeIntegerValue(Type type, long value)
{
if (type.equals(BIGINT)) {
return value;
}
if (type.equals(INTEGER)) {
return Ints.saturatedCast(value);
}
if (type.equals(SMALLINT)) {
return Shorts.saturatedCast(value);
}
if (type.equals(TINYINT)) {
return SignedBytes.saturatedCast(value);
}
throw new IllegalArgumentException("Unexpected type: " + type);
}
示例5
/**
* Writes this {@code BloomFilter} to an output stream, with a custom format (not Java
* serialization). This has been measured to save at least 400 bytes compared to regular
* serialization.
*
* <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter.
*/
public void writeTo(OutputStream out) throws IOException {
// Serial form:
// 1 signed byte for the strategy
// 1 unsigned byte for the number of hash functions
// 1 big endian int, the number of longs in our bitset
// N big endian longs of our bitset
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(strategy.ordinal()));
dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor
dout.writeInt(bits.data.length);
for (long value : bits.data) {
dout.writeLong(value);
}
}
示例6
/**
* Writes this {@code BloomFilter} to an output stream, with a custom format (not Java
* serialization). This has been measured to save at least 400 bytes compared to regular
* serialization.
*
* <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter.
*/
public void writeTo(OutputStream out) throws IOException {
// Serial form:
// 1 signed byte for the strategy
// 1 unsigned byte for the number of hash functions
// 1 big endian int, the number of longs in our bitset
// N big endian longs of our bitset
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(strategy.ordinal()));
dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor
dout.writeInt(bits.data.length);
for (long value : bits.data) {
dout.writeLong(value);
}
}
示例7
/**
* Writes this {@code BloomFilter} to an output stream, with a custom format (not Java
* serialization). This has been measured to save at least 400 bytes compared to regular
* serialization.
*
* <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter.
*/
public void writeTo(OutputStream out) throws IOException {
// Serial form:
// 1 signed byte for the strategy
// 1 unsigned byte for the number of hash functions
// 1 big endian int, the number of longs in our bitset
// N big endian longs of our bitset
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(strategy.ordinal()));
dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor
dout.writeInt(bits.data.length);
for (long value : bits.data) {
dout.writeLong(value);
}
}
示例8
/**
* Writes this {@code BloomFilter} to an output stream, with a custom format (not Java
* serialization). This has been measured to save at least 400 bytes compared to regular
* serialization.
*
* <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter.
*/
public void writeTo(OutputStream out) throws IOException {
// Serial form:
// 1 signed byte for the strategy
// 1 unsigned byte for the number of hash functions
// 1 big endian int, the number of longs in our bitset
// N big endian longs of our bitset
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(strategy.ordinal()));
dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor
dout.writeInt(bits.data.length);
for (long value : bits.data) {
dout.writeLong(value);
}
}
示例9
@ExpectWarning(value="RV_CHECK_COMPARETO_FOR_SPECIFIC_RETURN_VALUE", num = 9)
public static int testGuavaPrimitiveCompareCalls() {
int count = 0;
if (Booleans.compare(false, true) == -1)
count++;
if (Chars.compare('a', 'b') == -1)
count++;
if (Doubles.compare(1, 2) == -1)
count++;
if (Floats.compare(1, 2) == -1)
count++;
if (Ints.compare(1, 2) == -1)
count++;
if (Longs.compare(1, 2) == -1)
count++;
if (Shorts.compare((short) 1, (short) 2) == -1)
count++;
if (SignedBytes.compare((byte) 1, (byte) 2) == -1)
count++;
if (UnsignedBytes.compare((byte) 1, (byte) 2) == -1)
count++;
return count;
}
示例10
@Test
public void testGetId_2DSpatialLexicographicOrdering() throws Exception {
final int LATITUDE_BITS = 31;
final int LONGITUDE_BITS = 31;
final double[] minValue = new double[] {-90, -180};
final double[] maxValue = new double[] {90, 180};
final SFCDimensionDefinition[] SPATIAL_DIMENSIONS =
new SFCDimensionDefinition[] {
new SFCDimensionDefinition(new LatitudeDefinition(), LATITUDE_BITS),
new SFCDimensionDefinition(new LongitudeDefinition(), LONGITUDE_BITS)};
final SpaceFillingCurve hilbertSFC =
SFCFactory.createSpaceFillingCurve(SPATIAL_DIMENSIONS, SFCType.HILBERT);
Assert.assertTrue(
SignedBytes.lexicographicalComparator().compare(
hilbertSFC.getId(minValue),
hilbertSFC.getId(maxValue)) < 0);
}
示例11
@Description("Round to nearest integer")
@ScalarFunction("round")
@SqlType(StandardTypes.TINYINT)
public static long roundTinyint(@SqlType(StandardTypes.TINYINT) long num, @SqlType(StandardTypes.INTEGER) long decimals)
{
long rounded = roundLong(num, decimals);
try {
return SignedBytes.checkedCast(rounded);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + rounded, e);
}
}
示例12
@ScalarOperator(ADD)
@SqlType(StandardTypes.TINYINT)
public static long add(@SqlType(StandardTypes.TINYINT) long left, @SqlType(StandardTypes.TINYINT) long right)
{
try {
return SignedBytes.checkedCast(left + right);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("tinyint addition overflow: %s + %s", left, right), e);
}
}
示例13
@ScalarOperator(SUBTRACT)
@SqlType(StandardTypes.TINYINT)
public static long subtract(@SqlType(StandardTypes.TINYINT) long left, @SqlType(StandardTypes.TINYINT) long right)
{
try {
return SignedBytes.checkedCast(left - right);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("tinyint subtraction overflow: %s - %s", left, right), e);
}
}
示例14
@ScalarOperator(MULTIPLY)
@SqlType(StandardTypes.TINYINT)
public static long multiply(@SqlType(StandardTypes.TINYINT) long left, @SqlType(StandardTypes.TINYINT) long right)
{
try {
return SignedBytes.checkedCast(left * right);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("tinyint multiplication overflow: %s * %s", left, right), e);
}
}
示例15
@ScalarOperator(NEGATION)
@SqlType(StandardTypes.TINYINT)
public static long negate(@SqlType(StandardTypes.TINYINT) long value)
{
try {
return SignedBytes.checkedCast(-value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "tinyint negation overflow: " + value, e);
}
}
示例16
@UsedByGeneratedCode
public static long longDecimalToTinyint(Slice decimal, long precision, long scale, BigInteger tenToScale)
{
try {
return SignedBytes.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, DecimalConversions.intScale(-scale))));
}
catch (ArithmeticException | IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", Decimals.toString(decimal, DecimalConversions.intScale(scale))));
}
}
示例17
@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)
public static long castToTinyint(@SqlType(StandardTypes.BIGINT) long value)
{
try {
return SignedBytes.checkedCast(value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
}
}
示例18
@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)
public static long castToTinyint(@SqlType(StandardTypes.SMALLINT) long value)
{
try {
return SignedBytes.checkedCast(value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
}
}
示例19
@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)
public static long castToTinyint(@SqlType(StandardTypes.REAL) long value)
{
float floatValue = intBitsToFloat((int) value);
if (Float.isNaN(floatValue)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast real NaN to tinyint");
}
try {
return SignedBytes.checkedCast((long) MathFunctions.round((double) floatValue));
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + floatValue, e);
}
}
示例20
@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)
public static long castToTinyint(@SqlType(StandardTypes.INTEGER) long value)
{
try {
return SignedBytes.checkedCast(value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
}
}
示例21
@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)
public static long castToTinyint(@SqlType(StandardTypes.DOUBLE) double value)
{
if (Double.isNaN(value)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast double NaN to tinyint");
}
try {
return SignedBytes.checkedCast((long) MathFunctions.round(value));
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
}
}
示例22
private static Optional<Object> translateValue(Object prestoNativeValue, Type type)
{
requireNonNull(prestoNativeValue, "prestoNativeValue is null");
requireNonNull(type, "type is null");
checkArgument(Primitives.wrap(type.getJavaType()).isInstance(prestoNativeValue), "%s (%s) is not a valid representation for %s", prestoNativeValue, prestoNativeValue.getClass(), type);
if (type == TINYINT) {
return Optional.of((long) SignedBytes.checkedCast(((Long) prestoNativeValue)));
}
if (type == SMALLINT) {
return Optional.of((long) Shorts.checkedCast(((Long) prestoNativeValue)));
}
if (type == IntegerType.INTEGER) {
return Optional.of((long) toIntExact(((Long) prestoNativeValue)));
}
if (type == BIGINT) {
return Optional.of(prestoNativeValue);
}
if (type instanceof ObjectIdType) {
return Optional.of(new ObjectId(((Slice) prestoNativeValue).getBytes()));
}
if (type instanceof VarcharType) {
return Optional.of(((Slice) prestoNativeValue).toStringUtf8());
}
return Optional.empty();
}
示例23
/** Compare two byte arrays by lexicographical order. */
public static int compareBytes(byte[] left, byte[] right) {
if (left == null) {
left = EMPTY_BYTES;
}
if (right == null) {
right = EMPTY_BYTES;
}
return SignedBytes.lexicographicalComparator().compare(left, right);
}
示例24
@Override
public int compare(INode left, INode right) {
if (left == null) {
return right == null ? 0 : -1;
} else {
if (right == null) {
return 1;
} else {
int cmp = compare(left.getParent(), right.getParent());
return cmp == 0 ? SignedBytes.lexicographicalComparator().compare(
left.getLocalNameBytes(), right.getLocalNameBytes()) : cmp;
}
}
}
示例25
/**
* Writes this cuckoo filter to an output stream, with a custom format (not Java serialization).
* This has been measured to save at least 400 bytes compared to regular serialization. <p/>
*
* Use {@link #readFrom(InputStream, Funnel)} to reconstruct the written CuckooFilter.
*/
public void writeTo(OutputStream out) throws IOException {
/*
* Serial form:
* 1 signed byte for the strategy
* 1 IEEE 754 floating-point double, the expected FPP
* 1 big endian long, the number of entries
* 1 big endian long, the checksum of entries
* 1 big endian long for the number of buckets
* 1 big endian int for the number of entries per bucket
* 1 big endian int for the fingerprint size in bits
* 1 big endian int, the number of longs in the filter table's data
* N big endian longs of the filter table's data
*/
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(cuckooStrategy.ordinal()));
dout.writeDouble(fpp);
dout.writeLong(table.size());
dout.writeLong(table.checksum());
dout.writeLong(table.numBuckets());
dout.writeInt(table.numEntriesPerBucket());
dout.writeInt(table.numBitsPerEntry());
dout.writeInt(table.data().length);
for (long value : table.data()) {
dout.writeLong(value);
}
}
示例26
/** Compare two byte arrays by lexicographical order. */
public static int compareBytes(byte[] left, byte[] right) {
if (left == null) {
left = EMPTY_BYTES;
}
if (right == null) {
right = EMPTY_BYTES;
}
return SignedBytes.lexicographicalComparator().compare(left, right);
}
示例27
@Override
public int compare(INode left, INode right) {
if (left == null) {
return right == null ? 0 : -1;
} else {
if (right == null) {
return 1;
} else {
int cmp = compare(left.getParent(), right.getParent());
return cmp == 0 ? SignedBytes.lexicographicalComparator().compare(
left.getLocalNameBytes(), right.getLocalNameBytes()) : cmp;
}
}
}
示例28
/**
* Writes this {@code BloomFilter} to an output stream, with a custom format (not Java
* serialization). This has been measured to save at least 400 bytes compared to regular
* serialization.
*
* <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter.
*/
public void writeTo(OutputStream out) throws IOException {
// Serial form:
// 1 signed byte for the strategy
// 1 unsigned byte for the number of hash functions
// 1 big endian int, the number of longs in our bitset
// N big endian longs of our bitset
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(strategy.ordinal()));
dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor
dout.writeInt(bits.data.length);
for (long value : bits.data) {
dout.writeLong(value);
}
}
示例29
/**
* Writes this cuckoo filter to an output stream, with a custom format (not Java serialization).
* This has been measured to save at least 400 bytes compared to regular serialization. <p/>
*
* Use {@link #readFrom(InputStream, Funnel)} to reconstruct the written CuckooFilter.
*/
public void writeTo(OutputStream out) throws IOException {
/*
* Serial form:
* 1 signed byte for the strategy
* 1 IEEE 754 floating-point double, the expected FPP
* 1 big endian long, the number of entries
* 1 big endian long, the checksum of entries
* 1 big endian long for the number of buckets
* 1 big endian int for the number of entries per bucket
* 1 big endian int for the fingerprint size in bits
* 1 big endian int, the number of longs in the filter table's data
* N big endian longs of the filter table's data
*/
DataOutputStream dout = new DataOutputStream(out);
dout.writeByte(SignedBytes.checkedCast(cuckooStrategy.ordinal()));
dout.writeDouble(fpp);
dout.writeLong(table.size());
dout.writeLong(table.checksum());
dout.writeLong(table.numBuckets());
dout.writeInt(table.numEntriesPerBucket());
dout.writeInt(table.numBitsPerEntry());
dout.writeInt(table.data().length);
for (long value : table.data()) {
dout.writeLong(value);
}
}
示例30
/**
* Convert Presto native value (stack representation) of given type to Accumulo equivalent.
*
* @param value Object to convert
* @param type Destination Presto type
*/
private static Object convert(Object value, Type type)
{
if (value == null) {
return null;
}
checkArgument(Primitives.wrap(type.getJavaType()).isInstance(value), "Invalid representation for %s: %s [%s]", type, value, value.getClass().getName());
// Array? Better be a block!
if (Types.isArrayType(type)) {
// Block
return value;
}
// Map? Better be a block!
if (Types.isMapType(type)) {
// Block
return value;
}
// And now for the plain types
if (type.equals(BIGINT)) {
// long
return value;
}
if (type.equals(INTEGER)) {
return toIntExact((long) value);
}
if (type.equals(BOOLEAN)) {
// boolean
return value;
}
if (type.equals(DATE)) {
return Date.valueOf(LocalDate.ofEpochDay((long) value));
}
if (type.equals(DOUBLE)) {
// double
return value;
}
if (type.equals(REAL)) {
return intBitsToFloat(toIntExact((long) value));
}
if (type.equals(SMALLINT)) {
return Shorts.checkedCast((long) value);
}
if (type.equals(TIME)) {
// TODO this likely is incorrect, passing the millis value interpreted in UTC into millis value interpreted in JVM's zone
// TODO account for non-legacy timestamp
return new Time((long) value);
}
if (type.equals(TIMESTAMP)) {
// TODO this likely is incorrect, passing the millis value interpreted in UTC into millis value interpreted in JVM's zone
// TODO account for non-legacy timestamp
return new Timestamp((long) value);
}
if (type.equals(TINYINT)) {
return SignedBytes.checkedCast((long) value);
}
if (type.equals(VARBINARY)) {
return ((Slice) value).getBytes();
}
if (type instanceof VarcharType) {
return ((Slice) value).toStringUtf8();
}
throw new PrestoException(NOT_SUPPORTED, "Unsupported PrestoType " + type);
}