/** * Returns this field's value as a {@link Double}. This method should only be used if the * corresponding field has {@link Field.Type#floatingPoint()} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Double} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public double doubleValue() { return Double.parseDouble(stringValue()); }
/** * Returns this field's value as a {@code long}. This method should only be used if the * corresponding field has {@link Field.Type#integer()} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Integer} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public long longValue() { return Long.parseLong(stringValue()); }
/** * Returns this field's value as a {@code long}, representing a timestamp in microseconds since * epoch (UNIX time). This method should only be used if the corresponding field has * {@link Field.Type#timestamp()} type. * * @throws ClassCastException if the field is not a primitive type * @throws NumberFormatException if the field's value could not be converted to {@link Long} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public long timestampValue() { // timestamps are encoded in the format 1408452095.22 where the integer part is seconds since // epoch (e.g. 1408452095.22 == 2014-08-19 07:41:35.220 -05:00) return new Double(((Double.valueOf(stringValue())) * MICROSECONDS)).longValue(); }
/** * Returns this field's value as a byte array. This method should only be used if the * corresponding field has primitive type ({@link Field.Type#bytes()}. * * @throws ClassCastException if the field is not a primitive type * @throws NullPointerException if {@link #isNull()} returns {@code true} * @throws IllegalStateException if the field value is not encoded in base64 */ public byte[] bytesValue() { try { return BaseEncoding.base64().decode(stringValue()); } catch (IllegalArgumentException ex) { throw new IllegalStateException(ex); } }
/** * Returns this field's value as a {@link Boolean}. This method should only be used if the * corresponding field has {@link Field.Type#bool()} type. * * @throws ClassCastException if the field is not a primitive type * @throws IllegalStateException if the field's value could not be converted to {@link Boolean} * @throws NullPointerException if {@link #isNull()} returns {@code true} */ @SuppressWarnings("unchecked") public boolean booleanValue() { String stringValue = stringValue(); checkState(stringValue.equalsIgnoreCase("true") || stringValue.equalsIgnoreCase("false"), "Field value is not of boolean type"); return Boolean.parseBoolean(stringValue); }