Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion google-cloud-core/src/main/java/com/google/cloud/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.google.api.core.BetaApi;
import com.google.common.base.Preconditions;

import java.io.Serializable;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -26,10 +28,11 @@
* Represents a Date without time, such as 2017-03-17. Date is timezone independent.
*/
@BetaApi
public final class Date implements Comparable<Date> {
public final class Date implements Comparable<Date>, Serializable {

// Date format "yyyy-mm-dd"
private static final Pattern FORMAT_REGEXP = Pattern.compile("(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)");
private static final long serialVersionUID = 8067099123096783929L;
private final int year;
private final int month;
private final int dayOfMonth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud;

import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.testing.EqualsTester;
Expand Down Expand Up @@ -65,6 +66,11 @@ public void validOrdering() {
assertDescending(d5, d4, d3, d2, d1);
}

@Test
public void serialization() {
reserializeAndAssert(Date.fromYearMonthDay(2017, 4, 20));
}

private void assertDescending(Date... dates) {
for (int i = 0; i < dates.length - 1; i++) {
assertThat(dates[i]).isEquivalentAccordingToCompareTo(dates[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud;

import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.testing.EqualsTester;
Expand Down Expand Up @@ -175,4 +176,9 @@ public void comparable() {
assertThat(Timestamp.ofTimeSecondsAndNanos(101, 0))
.isAtLeast(Timestamp.ofTimeSecondsAndNanos(100, 1000));
}

@Test
public void serialization() throws Exception {
reserializeAndAssert(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999Z"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import com.google.protobuf.Value;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -41,10 +43,11 @@
*
* <p>{@code Key} instances are immutable.
*/
public final class Key {
public final class Key implements Serializable {
private static final Joiner joiner = Joiner.on(',').useForNull("<null>");
private static final com.google.protobuf.Value NULL_PROTO =
Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
private static final long serialVersionUID = 4433485671785063530L;

private final List<Object> parts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import java.io.Serializable;
import java.util.Objects;

/**
Expand Down Expand Up @@ -123,7 +124,9 @@
*
* <p>{@code KeyRange} instances are immutable.
*/
public final class KeyRange {
public final class KeyRange implements Serializable {
private static final long serialVersionUID = 100894273141111331L;

/** Defines whether a range includes or excludes its endpoint keys. */
public enum Endpoint {
/** Ranges include the endpoint key. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.google.cloud.spanner;

import com.google.common.collect.ImmutableList;

import java.io.Serializable;
import java.util.Objects;

/**
Expand All @@ -29,7 +31,8 @@
*
* <p>{@code KeySet} instances are immutable.
*/
public final class KeySet {
public final class KeySet implements Serializable {
private static final long serialVersionUID = -542201151451064347L;
private final boolean all;
private final ImmutableList<Key> keys;
private final ImmutableList<KeyRange> ranges;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import com.google.common.collect.ImmutableList;
import com.google.protobuf.ListValue;

import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -47,7 +49,9 @@
*
* <p>{@code Mutation} instances are immutable.
*/
public final class Mutation {
public final class Mutation implements Serializable {
private static final long serialVersionUID = 1784900828296918555L;

/** Enumerates the types of mutation that can be applied. */
public enum Op {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.spanner.v1.TypeCode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -39,7 +41,7 @@
* <p>{@code Type} instances are immutable.
*/
@Immutable
public final class Type {
public final class Type implements Serializable {
private static final Type TYPE_BOOL = new Type(Code.BOOL, null, null);
private static final Type TYPE_INT64 = new Type(Code.INT64, null, null);
private static final Type TYPE_FLOAT64 = new Type(Code.FLOAT64, null, null);
Expand All @@ -56,6 +58,7 @@ public final class Type {
private static final Type TYPE_ARRAY_DATE = new Type(Code.ARRAY, TYPE_DATE, null);

private static final int AMBIGUOUS_FIELD = -1;
private static final long serialVersionUID = -3076152125004114582L;

/** Returns the descriptor for the {@code BOOL type}. */
public static Type bool() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.google.common.collect.Lists;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
Expand All @@ -50,13 +52,14 @@
* <p>{@code Value} instances are immutable.
*/
@Immutable
public abstract class Value {
public abstract class Value implements Serializable {
private static final int MAX_DEBUG_STRING_LENGTH = 32;
private static final String ELLIPSIS = "...";
private static final String NULL_STRING = "NULL";
private static final char LIST_SEPERATOR = ',';
private static final char LIST_OPEN = '[';
private static final char LIST_CLOSE = ']';
private static final long serialVersionUID = -5289864325087675338L;

/**
* Returns a {@code BOOL} value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

package com.google.cloud.spanner;

import static com.google.cloud.spanner.KeyRange.Endpoint.CLOSED;
import static com.google.cloud.spanner.KeyRange.Endpoint.OPEN;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.testing.EqualsTester;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static com.google.cloud.spanner.KeyRange.Endpoint.CLOSED;
import static com.google.cloud.spanner.KeyRange.Endpoint.OPEN;
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;

/** Unit tests for {@link com.google.cloud.spanner.KeyRange}. */
@RunWith(JUnit4.class)
public class KeyRangeTest {
Expand Down Expand Up @@ -123,4 +124,13 @@ public void testToString() {
assertThat(KeyRange.openClosed(Key.of("a"), Key.of("b")).toString()).isEqualTo("([a],[b]]");
assertThat(KeyRange.closedClosed(Key.of(), Key.of()).toString()).isEqualTo("[[],[]]");
}

@Test
public void serialization() throws Exception {
reserializeAndAssert(KeyRange.closedOpen(Key.of(1), Key.of(2)));
reserializeAndAssert(
KeyRange.closedClosed(Key.of(1), Key.of(2)));
reserializeAndAssert(KeyRange.openOpen(Key.of(1), Key.of(2)));
reserializeAndAssert(KeyRange.openClosed(Key.of(1), Key.of(2)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.ByteArray;
Expand Down Expand Up @@ -262,6 +263,15 @@ public void serializationMultiWithAll() {
+ " all:true");
}

@Test
public void javaSerialization() throws Exception {
reserializeAndAssert(KeySet.all()
.toBuilder()
.addKey(Key.of("a", 1))
.addRange(KeyRange.closedOpen(Key.of("m"), Key.of("p")))
.build());
}

private static void checkProto(KeySet keySet, String proto) {
com.google.spanner.v1.KeySet.Builder builder = com.google.spanner.v1.KeySet.newBuilder();
keySet.appendToProto(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.ByteArray;
Expand Down Expand Up @@ -174,4 +175,18 @@ public void equalsAndHashCode() {

tester.testEquals();
}

@Test
public void serialization() throws Exception {
reserializeAndAssert(Key.of());
reserializeAndAssert(Key.of(new Object[] {null}));
reserializeAndAssert(Key.of(true));
reserializeAndAssert(Key.of(32));
reserializeAndAssert(Key.of(2.0));
reserializeAndAssert(Key.of("xyz"));
reserializeAndAssert(Key.of(ByteArray.copyFrom("xyz")));
reserializeAndAssert(Key.of(Timestamp.parseTimestamp("2015-09-15T00:00:00Z")));
reserializeAndAssert(Key.of(Date.parseDate("2015-09-15")));
reserializeAndAssert(Key.of(1, 2, 3));
}
}
Loading