@Override public int compare(long[] left, long[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = Longs.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
private void writeLong(OutputStream out, long longValue) throws IOException { out.write(Longs.toByteArray(longValue)); }
/** Returns an unspecified hash code for the contents of this immutable array. */ @Override public int hashCode() { int hash = 1; for (int i = start; i < end; i++) { hash *= 31; hash += Longs.hashCode(array[i]); } return hash; }
@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; }
private static List<?> primitiveArrayToList(Object obj) { if (obj instanceof boolean[]) { return Booleans.asList((boolean[])obj); } else if (obj instanceof char[]) { return Chars.asList((char[])obj); } else if (obj instanceof byte[]) { return Bytes.asList((byte[])obj); } else if (obj instanceof short[]) { return Shorts.asList((short[])obj); } else if (obj instanceof int[]) { return Ints.asList((int[])obj); } else if (obj instanceof long[]) { return Longs.asList((long[])obj); } else if (obj instanceof float[]) { return Floats.asList((float[])obj); } else if (obj instanceof double[]) { return Doubles.asList((double[])obj); } return Collections.<Object>emptyList(); }
return Longs.compare(n, value.longValue()); } else if (value instanceof Double || value instanceof Float) { return compareDoubleToLong(n, value.longValue()); } else if (value instanceof Double || value instanceof Float) { return Doubles.compare(n, value.doubleValue()); } else { throw new ISE("Number was[%s]?!?", value.getClass().getName());
/** * This method converts this FunctionProperties instance to FlatBuffers representation * @param bufferBuilder * @return */ public int asFlatProperties(FlatBufferBuilder bufferBuilder) { int iname = bufferBuilder.createString(name); int ii = FlatProperties.createIVector(bufferBuilder, Ints.toArray(i)); int il = FlatProperties.createLVector(bufferBuilder, Longs.toArray(l)); int id = FlatProperties.createDVector(bufferBuilder, Doubles.toArray(d)); int arrays[] = new int[a.size()]; int cnt = 0; for (val array: a) { int off = array.toFlatArray(bufferBuilder); arrays[cnt++] = off; } int ia = FlatProperties.createAVector(bufferBuilder, arrays); return FlatProperties.createFlatProperties(bufferBuilder, iname, ii, il, id, ia); }
@Override public Object apply(@NullableDecl Object input) { if (input != null && input.getClass().isArray()) { Iterable<?> iterable; if (input.getClass() == boolean[].class) { iterable = Booleans.asList((boolean[]) input); } else if (input.getClass() == int[].class) { iterable = Ints.asList((int[]) input); } else if (input.getClass() == long[].class) { iterable = Longs.asList((long[]) input); } else if (input.getClass() == short[].class) { iterable = Shorts.asList((short[]) input); } else if (input.getClass() == byte[].class) { iterable = Bytes.asList((byte[]) input); } else if (input.getClass() == double[].class) { iterable = doubleArrayAsString((double[]) input); } else if (input.getClass() == float[].class) { iterable = floatArrayAsString((float[]) input); } else if (input.getClass() == char[].class) { iterable = Chars.asList((char[]) input); } else { iterable = Arrays.asList((Object[]) input); } return Iterables.transform(iterable, STRINGIFY); } return input; } };
public void testAsList_subList_toArray_roundTrip() { long[] array = {(long) 0, (long) 1, (long) 2, (long) 3}; List<Long> list = Longs.asList(array); assertTrue(Arrays.equals(new long[] {(long) 1, (long) 2}, Longs.toArray(list.subList(1, 3)))); assertTrue(Arrays.equals(new long[] {}, Longs.toArray(list.subList(2, 2)))); }
@Override public Iterable<ObjectStoreDeleteOperation> convertRecord(Class<?> outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException { Optional<Object> fieldValue = AvroUtils.getFieldValue(inputRecord, this.objectIdField); byte[] objectId; if (fieldValue.isPresent()) { if (fieldValue.get() instanceof Utf8) { objectId = ((Utf8) fieldValue.get()).getBytes(); } else if (fieldValue.get() instanceof String) { objectId = ((String) fieldValue.get()).getBytes(Charsets.UTF_8); } else if (fieldValue.get() instanceof Long) { objectId = Longs.toByteArray((Long) fieldValue.get()); } else if (fieldValue.get() instanceof Integer) { objectId = Ints.toByteArray((Integer) fieldValue.get()); } else { objectId = (byte[]) fieldValue.get(); } return new SingleRecordIterable<ObjectStoreDeleteOperation>(ObjectStoreOperationBuilder.deleteBuilder() .withObjectId(objectId).build()); } else { throw new DataConversionException(String.format("Object Id field %s not found in record %s", this.objectIdField, inputRecord)); } } }
/** * Attempts to convert the provided string value to a numeric type, * trying Integer, Long and Double in order until successful. */ @Override public Object convert(String value) { if (value == null || value.isEmpty()) { return value; } Object result = Ints.tryParse(value); if (result != null) { return result; } result = Longs.tryParse(value); if (result != null) { return result; } result = Doubles.tryParse(value); if (result != null) { return result; } return value; }
public void testByteArrayRoundTrips() { Random r = new Random(5); byte[] b = new byte[Longs.BYTES]; for (int i = 0; i < 1000; i++) { long num = r.nextLong(); assertEquals(num, Longs.fromByteArray(Longs.toByteArray(num))); r.nextBytes(b); long value = Longs.fromByteArray(b); assertTrue("" + value, Arrays.equals(b, Longs.toByteArray(value))); } }
/** * Arrays.asList()的加强版, 返回一个底层为原始类型long的List * * 与保存Long相比节约空间,同时只在读取数据时AutoBoxing. * * @see java.util.Arrays#asList(Object...) * @see com.google.common.primitives.Longs#asList(long...) */ public static List<Long> longAsList(long... backingArray) { return Longs.asList(backingArray); }
.isWithin(ALLOWED_ERROR) .of(INTEGER_MANY_VALUES_MEAN); assertThat(Stats.meanOf(Ints.toArray(INTEGER_MANY_VALUES))) .isWithin(ALLOWED_ERROR) .of(INTEGER_MANY_VALUES_MEAN); assertThat(Stats.meanOf(LONG_MANY_VALUES)).isWithin(ALLOWED_ERROR).of(LONG_MANY_VALUES_MEAN); assertThat(Stats.meanOf(Longs.toArray(LONG_MANY_VALUES))) .isWithin(ALLOWED_ERROR) .of(LONG_MANY_VALUES_MEAN);
aggregations.add(new Pair<>( QueryRunnerTestHelper.rowsCount, Longs.asList(186L, 186L, 837L) )); Pair<AggregatorFactory, List<?>> indexAggregation = new Pair<>( QueryRunnerTestHelper.indexDoubleSum, Doubles.asList(215679.82879638672D, 192046.1060180664D, 95606.57232284546D) ); aggregations.add(indexAggregation); aggregations.add(new Pair<>( QueryRunnerTestHelper.qualityUniques, Doubles.asList(QueryRunnerTestHelper.UNIQUES_2, QueryRunnerTestHelper.UNIQUES_2, QueryRunnerTestHelper.UNIQUES_9) )); aggregations.add(new Pair<>( new DoubleMaxAggregatorFactory("maxIndex", "index"), Doubles.asList(1743.92175D, 1870.061029D, 277.273533D) )); aggregations.add(new Pair<>( aggregations.add(new Pair<>( new LongSumAggregatorFactory("qlLong", "qualityLong"), Longs.asList(279000L, 279000L, 1171800L) )); aggregations.add(new Pair<>( aggregations.add(new Pair<>( new LongSumAggregatorFactory("qfLong", "qualityFloat"), Longs.asList(2790000L, 2790000L, 11718000L) ));
/** Returns an immutable array containing the given values, in order. */ public static ImmutableLongArray copyOf(Collection<Long> values) { return values.isEmpty() ? EMPTY : new ImmutableLongArray(Longs.toArray(values)); }
/** * To fix semantic difference of Longs.tryParse() from Long.parseLong (Longs.tryParse() returns null for '+' started value) */ @Nullable public static Long tryParseLong(@Nullable String string) { return Strings.isNullOrEmpty(string) ? null : Longs.tryParse(string.charAt(0) == '+' ? string.substring(1) : string); }
/** * convert deprecated parquet int96 nanosecond timestamp to a long, based on * https://github.com/prestodb/presto/blob/master/presto-hive/src/main/java/com/facebook/presto/hive/parquet/ParquetTimestampUtils.java#L56 */ private static long convertInt96BinaryToTimestamp(Binary value) { // based on prestodb parquet int96 timestamp conversion byte[] bytes = value.getBytes(); // little endian encoding - need to invert byte order long timeOfDayNanos = Longs.fromBytes(bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0]); int julianDay = Ints.fromBytes(bytes[11], bytes[10], bytes[9], bytes[8]); long ts = ((julianDay - JULIAN_EPOCH_OFFSET_DAYS) * MILLIS_IN_DAY) + (timeOfDayNanos / NANOS_PER_MILLISECOND); return ts; }
public static long toLong(byte[] bytes) { return Longs.fromByteArray(bytes); }