Skip to content

Commit 33400da

Browse files
committed
Move State interface out of channels, move to core module and make it generic
1 parent 029fbe2 commit 33400da

8 files changed

Lines changed: 76 additions & 75 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud;
18+
19+
/**
20+
* A common interface for restorable states. Implementations of {@code RestorableState} are capable
21+
* of saving the state of an object to restore it for later use.
22+
*
23+
* Implementations of this class must implement {@link java.io.Serializable} to ensure that the
24+
* state of a the object can be correctly serialized.
25+
*/
26+
public interface RestorableState<T> {
27+
28+
/**
29+
* Returns an object whose internal state reflects the one saved in the invocation object.
30+
*/
31+
T restore();
32+
}

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import com.google.gcloud.RestorableState;
20+
1921
import java.io.Closeable;
2022
import java.io.IOException;
2123
import java.nio.channels.ReadableByteChannel;
@@ -48,24 +50,8 @@ public interface BlobReadChannel extends ReadableByteChannel, Closeable {
4850
/**
4951
* Saves the read channel state.
5052
*
51-
* @return an object that contains the read channel state and can restore it afterwards. State
52-
* object must implement {@link java.io.Serializable}.
53-
*/
54-
public State save();
55-
56-
/**
57-
* A common interface for all classes that implement the internal state of a
58-
* {@code BlobReadChannel}.
59-
*
60-
* Implementations of this class must implement {@link java.io.Serializable} to ensure that the
61-
* state of a channel can be correctly serialized.
53+
* @return a {@link RestorableState} object that contains the read channel state and can restore
54+
* it afterwards. State object must implement {@link java.io.Serializable}.
6255
*/
63-
public interface State {
64-
65-
/**
66-
* Returns a {@code BlobReadChannel} whose internal state reflects the one saved in the
67-
* invocation object.
68-
*/
69-
public BlobReadChannel restore();
70-
}
56+
public RestorableState<BlobReadChannel> save();
7157
}

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannelImpl.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.api.services.storage.model.StorageObject;
2222
import com.google.common.base.MoreObjects;
23+
import com.google.gcloud.RestorableState;
2324
import com.google.gcloud.RetryHelper;
2425
import com.google.gcloud.spi.StorageRpc;
2526

@@ -45,8 +46,8 @@ class BlobReadChannelImpl implements BlobReadChannel {
4546
private boolean endOfStream;
4647
private int chunkSize = DEFAULT_CHUNK_SIZE;
4748

48-
private StorageRpc storageRpc;
49-
private StorageObject storageObject;
49+
private final StorageRpc storageRpc;
50+
private final StorageObject storageObject;
5051
private int bufferPos;
5152
private byte[] buffer;
5253

@@ -56,11 +57,12 @@ class BlobReadChannelImpl implements BlobReadChannel {
5657
this.blob = blob;
5758
this.requestOptions = requestOptions;
5859
isOpen = true;
59-
initTransients();
60+
storageRpc = serviceOptions.storageRpc();
61+
storageObject = blob.toPb();
6062
}
6163

6264
@Override
63-
public State save() {
65+
public RestorableState<BlobReadChannel> save() {
6466
StateImpl.Builder builder = StateImpl.builder(serviceOptions, blob, requestOptions)
6567
.position(position)
6668
.isOpen(isOpen)
@@ -73,11 +75,6 @@ public State save() {
7375
return builder.build();
7476
}
7577

76-
private void initTransients() {
77-
storageRpc = serviceOptions.storageRpc();
78-
storageObject = blob.toPb();
79-
}
80-
8178
@Override
8279
public boolean isOpen() {
8380
return isOpen;
@@ -148,7 +145,7 @@ public byte[] call() {
148145
return toWrite;
149146
}
150147

151-
static class StateImpl implements BlobReadChannel.State, Serializable {
148+
static class StateImpl implements RestorableState<BlobReadChannel>, Serializable {
152149

153150
private static final long serialVersionUID = 3889420316004453706L;
154151

@@ -205,7 +202,7 @@ public Builder chunkSize(int chunkSize) {
205202
return this;
206203
}
207204

208-
public State build() {
205+
public RestorableState<BlobReadChannel> build() {
209206
return new StateImpl(this);
210207
}
211208
}

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannel.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import com.google.gcloud.RestorableState;
20+
1921
import java.io.Closeable;
2022
import java.nio.channels.WritableByteChannel;
2123

@@ -35,29 +37,12 @@ public interface BlobWriteChannel extends WritableByteChannel, Closeable {
3537
void chunkSize(int chunkSize);
3638

3739
/**
38-
* Saves the write channel state.
39-
*
40-
* @return an object that contains the write channel state and can restore it afterwards. State
41-
* object must implement {@link java.io.Serializable}.
42-
*/
43-
public State save();
44-
45-
/**
46-
* A common interface for all classes that implement the internal state of a
47-
* {@code BlobWriteChannel}.
40+
* Saves the write channel state so that it can be restored afterwards. The original
41+
* {@code BlobWriteChannel} and the restored one should not both be used. Closing one channel
42+
* causes the other channel to close, subsequent writes will fail.
4843
*
49-
* Implementations of this class must implement {@link java.io.Serializable} to ensure that the
50-
* state of a channel can be correctly serialized.
44+
* @return a {@link RestorableState} object that contains the write channel state and can restore
45+
* it afterwards. State object must implement {@link java.io.Serializable}.
5146
*/
52-
public interface State {
53-
54-
/**
55-
* Returns a {@code BlobWriteChannel} whose internal state reflects the one saved in the
56-
* invocation object.
57-
*
58-
* The original {@code BlobWriteChannel} and the restored one should not both be used. Closing
59-
* one channel causes the other channel to close, subsequent writes will fail.
60-
*/
61-
public BlobWriteChannel restore();
62-
}
47+
public RestorableState<BlobWriteChannel> save();
6348
}

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.api.services.storage.model.StorageObject;
2323
import com.google.common.base.MoreObjects;
24+
import com.google.gcloud.RestorableState;
2425
import com.google.gcloud.RetryHelper;
2526
import com.google.gcloud.spi.StorageRpc;
2627

@@ -49,26 +50,28 @@ class BlobWriteChannelImpl implements BlobWriteChannel {
4950
private boolean isOpen = true;
5051
private int chunkSize = DEFAULT_CHUNK_SIZE;
5152

52-
private StorageRpc storageRpc;
53-
private StorageObject storageObject;
53+
private final StorageRpc storageRpc;
54+
private final StorageObject storageObject;
5455

5556
BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo,
5657
Map<StorageRpc.Option, ?> optionsMap) {
5758
this.options = options;
5859
this.blobInfo = blobInfo;
59-
initTransients();
60+
storageRpc = options.storageRpc();
61+
storageObject = blobInfo.toPb();
6062
uploadId = storageRpc.open(storageObject, optionsMap);
6163
}
6264

6365
BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo, String uploadId) {
6466
this.options = options;
6567
this.blobInfo = blobInfo;
6668
this.uploadId = uploadId;
67-
initTransients();
69+
storageRpc = options.storageRpc();
70+
storageObject = blobInfo.toPb();
6871
}
6972

7073
@Override
71-
public State save() {
74+
public RestorableState<BlobWriteChannel> save() {
7275
if (isOpen) {
7376
flush(true);
7477
}
@@ -101,11 +104,6 @@ public void run() {
101104
}
102105
}
103106

104-
private void initTransients() {
105-
storageRpc = options.storageRpc();
106-
storageObject = blobInfo.toPb();
107-
}
108-
109107
private void validateOpen() throws IOException {
110108
if (!isOpen) {
111109
throw new IOException("stream is closed");
@@ -158,7 +156,7 @@ public void chunkSize(int chunkSize) {
158156
this.chunkSize = Math.max(MIN_CHUNK_SIZE, chunkSize);
159157
}
160158

161-
static class StateImpl implements State, Serializable {
159+
static class StateImpl implements RestorableState<BlobWriteChannel>, Serializable {
162160

163161
private static final long serialVersionUID = 8541062465055125619L;
164162

@@ -223,7 +221,7 @@ public Builder chunkSize(int chunkSize) {
223221
return this;
224222
}
225223

226-
public State build() {
224+
public RestorableState<BlobWriteChannel> build() {
227225
return new StateImpl(this);
228226
}
229227
}

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.fail;
2424

2525
import com.google.common.collect.ImmutableMap;
26+
import com.google.gcloud.RestorableState;
2627
import com.google.gcloud.RetryParams;
2728
import com.google.gcloud.spi.StorageRpc;
2829

@@ -198,7 +199,7 @@ public void testSaveAndRestore() throws IOException, ClassNotFoundException {
198199
EasyMock.replay(storageRpcMock);
199200
reader = new BlobReadChannelImpl(optionsMock, BLOB_ID, EMPTY_RPC_OPTIONS);
200201
reader.read(firstReadBuffer);
201-
BlobReadChannel.State readerState = reader.save();
202+
RestorableState<BlobReadChannel> readerState = reader.save();
202203
BlobReadChannel restoredReader = readerState.restore();
203204
restoredReader.read(secondReadBuffer);
204205
assertArrayEquals(Arrays.copyOf(firstResult, firstReadBuffer.capacity()),
@@ -213,8 +214,8 @@ public void testStateEquals() {
213214
EasyMock.replay(storageRpcMock);
214215
reader = new BlobReadChannelImpl(optionsMock, BLOB_ID, EMPTY_RPC_OPTIONS);
215216
BlobReadChannel secondReader = new BlobReadChannelImpl(optionsMock, BLOB_ID, EMPTY_RPC_OPTIONS);
216-
BlobReadChannel.State state = reader.save();
217-
BlobReadChannel.State secondState = secondReader.save();
217+
RestorableState<BlobReadChannel> state = reader.save();
218+
RestorableState<BlobReadChannel> secondState = secondReader.save();
218219
assertEquals(state, secondState);
219220
assertEquals(state.hashCode(), secondState.hashCode());
220221
assertEquals(state.toString(), secondState.toString());

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.fail;
2424

2525
import com.google.common.collect.ImmutableMap;
26+
import com.google.gcloud.RestorableState;
2627
import com.google.gcloud.RetryParams;
2728
import com.google.gcloud.spi.StorageRpc;
2829

@@ -212,7 +213,7 @@ public void testSaveAndRestore() throws IOException {
212213
assertEquals(DEFAULT_CHUNK_SIZE, writer.write(buffer1));
213214
assertArrayEquals(buffer1.array(), capturedBuffer.getValues().get(0));
214215
assertEquals(new Long(0L), capturedPosition.getValues().get(0));
215-
BlobWriteChannel.State writerState = writer.save();
216+
RestorableState<BlobWriteChannel> writerState = writer.save();
216217
BlobWriteChannel restoredWriter = writerState.restore();
217218
assertEquals(DEFAULT_CHUNK_SIZE, restoredWriter.write(buffer2));
218219
assertArrayEquals(buffer2.array(), capturedBuffer.getValues().get(1));
@@ -228,8 +229,8 @@ public void testStateEquals() {
228229
EasyMock.replay(storageRpcMock);
229230
writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
230231
BlobWriteChannel writer2 = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
231-
BlobWriteChannel.State state = writer.save();
232-
BlobWriteChannel.State state2 = writer2.save();
232+
RestorableState<BlobWriteChannel> state = writer.save();
233+
RestorableState<BlobWriteChannel> state2 = writer2.save();
233234
assertEquals(state, state2);
234235
assertEquals(state.hashCode(), state2.hashCode());
235236
assertEquals(state.toString(), state2.toString());

gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.gcloud.AuthCredentials;
24+
import com.google.gcloud.RestorableState;
2425
import com.google.gcloud.RetryParams;
2526
import com.google.gcloud.spi.StorageRpc;
2627
import com.google.gcloud.storage.Acl.Project.ProjectRole;
@@ -113,8 +114,8 @@ public void testReadChannelState() throws IOException, ClassNotFoundException {
113114
.build();
114115
BlobReadChannel reader =
115116
new BlobReadChannelImpl(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS);
116-
BlobReadChannel.State state = reader.save();
117-
BlobReadChannel.State deserializedState = serializeAndDeserialize(state);
117+
RestorableState<BlobReadChannel> state = reader.save();
118+
RestorableState<BlobReadChannel> deserializedState = serializeAndDeserialize(state);
118119
assertEquals(state, deserializedState);
119120
assertEquals(state.hashCode(), deserializedState.hashCode());
120121
assertEquals(state.toString(), deserializedState.toString());
@@ -129,8 +130,8 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException {
129130
.build();
130131
BlobWriteChannelImpl writer = new BlobWriteChannelImpl(
131132
options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id");
132-
BlobWriteChannel.State state = writer.save();
133-
BlobWriteChannel.State deserializedState = serializeAndDeserialize(state);
133+
RestorableState<BlobWriteChannel> state = writer.save();
134+
RestorableState<BlobWriteChannel> deserializedState = serializeAndDeserialize(state);
134135
assertEquals(state, deserializedState);
135136
assertEquals(state.hashCode(), deserializedState.hashCode());
136137
assertEquals(state.toString(), deserializedState.toString());

0 commit comments

Comments
 (0)