Skip to content

Commit 27cd1d7

Browse files
committed
Merge pull request #97 from eamonnmcmanus/master
Minor cleanups. No behaviour or API changes are intended, …
2 parents a1f2480 + a37d3d6 commit 27cd1d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+189
-186
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,9 @@ public static AuthCredentials createForComputeEngine()
181181
/**
182182
* Returns the Application Default Credentials.
183183
*
184-
* <p>
185-
* Returns the Application Default Credentials which are credentials that identify and authorize
186-
* the whole application. This is the built-in service account if running on Google Compute Engine
187-
* or the credentials file from the path in the environment variable
184+
* <p>Returns the Application Default Credentials which are credentials that identify and
185+
* authorize the whole application. This is the built-in service account if running on
186+
* Google Compute Engine or the credentials file from the path in the environment variable
188187
* GOOGLE_APPLICATION_CREDENTIALS.
189188
* </p>
190189
*

gcloud-java-core/src/main/java/com/google/gcloud/BaseService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package com.google.gcloud;
1818

19-
public abstract class BaseService<O extends ServiceOptions> implements Service<O> {
19+
public abstract class BaseService<OptionsT extends ServiceOptions<?, OptionsT>>
20+
implements Service<OptionsT> {
2021

21-
private final O options;
22+
private final OptionsT options;
2223

23-
protected BaseService(O options) {
24+
protected BaseService(OptionsT options) {
2425
this.options = options;
2526
}
2627

2728
@Override
28-
public O options() {
29+
public OptionsT options() {
2930
return options;
3031
}
3132
}

gcloud-java-core/src/main/java/com/google/gcloud/Service.java

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

1717
package com.google.gcloud;
1818

19-
public interface Service<O extends ServiceOptions> {
20-
O options();
19+
public interface Service<OptionsT extends ServiceOptions<?, OptionsT>> {
20+
OptionsT options();
2121
}

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
import java.util.regex.Matcher;
4646
import java.util.regex.Pattern;
4747

48-
public abstract class ServiceOptions<R, O extends ServiceOptions<R, O>> implements Serializable {
48+
public abstract class ServiceOptions<
49+
ServiceRpcT,
50+
OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>>
51+
implements Serializable {
4952

5053
private static final String DEFAULT_HOST = "https://www.googleapis.com";
5154
private static final long serialVersionUID = 1203687993961393350L;
@@ -56,7 +59,7 @@ public abstract class ServiceOptions<R, O extends ServiceOptions<R, O>> implemen
5659
private final HttpTransportFactory httpTransportFactory;
5760
private final AuthCredentials authCredentials;
5861
private final RetryParams retryParams;
59-
private final ServiceRpcFactory<R, O> serviceRpcFactory;
62+
private final ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
6063

6164
public interface HttpTransportFactory extends Serializable {
6265
HttpTransport create();
@@ -88,19 +91,21 @@ public HttpTransport create() {
8891

8992

9093

91-
protected abstract static class Builder<R, O extends ServiceOptions<R, O>,
92-
B extends Builder<R, O, B>> {
94+
protected abstract static class Builder<
95+
ServiceRpcT,
96+
OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>,
97+
B extends Builder<ServiceRpcT, OptionsT, B>> {
9398

9499
private String projectId;
95100
private String host;
96101
private HttpTransportFactory httpTransportFactory;
97102
private AuthCredentials authCredentials;
98103
private RetryParams retryParams;
99-
private ServiceRpcFactory<R, O> serviceRpcFactory;
104+
private ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
100105

101106
protected Builder() {}
102107

103-
protected Builder(ServiceOptions<R, O> options) {
108+
protected Builder(ServiceOptions<ServiceRpcT, OptionsT> options) {
104109
projectId = options.projectId;
105110
host = options.host;
106111
httpTransportFactory = options.httpTransportFactory;
@@ -109,7 +114,7 @@ protected Builder(ServiceOptions<R, O> options) {
109114
serviceRpcFactory = options.serviceRpcFactory;
110115
}
111116

112-
protected abstract ServiceOptions build();
117+
protected abstract ServiceOptions<ServiceRpcT, OptionsT> build();
113118

114119
@SuppressWarnings("unchecked")
115120
protected B self() {
@@ -141,13 +146,13 @@ public B retryParams(RetryParams retryParams) {
141146
return self();
142147
}
143148

144-
public B serviceRpcFactory(ServiceRpcFactory<R, O> serviceRpcFactory) {
149+
public B serviceRpcFactory(ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory) {
145150
this.serviceRpcFactory = serviceRpcFactory;
146151
return self();
147152
}
148153
}
149154

150-
protected ServiceOptions(Builder<R, O, ?> builder) {
155+
protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
151156
projectId = checkNotNull(builder.projectId != null ? builder.projectId : defaultProject());
152157
host = firstNonNull(builder.host, DEFAULT_HOST);
153158
httpTransportFactory =
@@ -222,7 +227,7 @@ protected static String googleCloudProjectId() {
222227
String section = null;
223228
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
224229
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
225-
while((line = reader.readLine()) != null) {
230+
while ((line = reader.readLine()) != null) {
226231
if (line.isEmpty() || line.startsWith(";")) {
227232
continue;
228233
}
@@ -245,7 +250,7 @@ protected static String googleCloudProjectId() {
245250
}
246251

247252
private static boolean isWindows() {
248-
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).indexOf("windows") > -1;
253+
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
249254
}
250255

251256
protected static String getAppEngineProjectId() {
@@ -288,7 +293,7 @@ public RetryParams retryParams() {
288293
return retryParams != null ? retryParams : RetryParams.noRetries();
289294
}
290295

291-
public ServiceRpcFactory<R, O> serviceRpcFactory() {
296+
public ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory() {
292297
return serviceRpcFactory;
293298
}
294299

@@ -297,13 +302,12 @@ public HttpRequestInitializer httpRequestInitializer() {
297302
return authCredentials().httpRequestInitializer(httpTransport, scopes());
298303
}
299304

300-
@Override
301-
public int hashCode() {
305+
protected int baseHashCode() {
302306
return Objects.hash(projectId, host, httpTransportFactory, authCredentials, retryParams,
303307
serviceRpcFactory);
304308
}
305309

306-
protected boolean isEquals(ServiceOptions other) {
310+
protected boolean baseEquals(ServiceOptions<?, ?> other) {
307311
return Objects.equals(projectId, other.projectId)
308312
&& Objects.equals(host, other.host)
309313
&& Objects.equals(httpTransportFactory, other.httpTransportFactory)
@@ -312,14 +316,17 @@ protected boolean isEquals(ServiceOptions other) {
312316
&& Objects.equals(serviceRpcFactory, other.serviceRpcFactory);
313317
}
314318

315-
public abstract Builder<R, O, ?> toBuilder();
319+
public abstract Builder<ServiceRpcT, OptionsT, ?> toBuilder();
316320

317321
/**
318322
* Creates a service RPC using a factory loaded by {@link ServiceLoader}.
319323
*/
320-
protected static <R, O extends ServiceOptions<R, O>> R createRpc(O options,
321-
Class<? extends ServiceRpcFactory<R, O>> factoryClass) {
322-
ServiceRpcFactory<R, O> factory = Iterables.getFirst(ServiceLoader.load(factoryClass), null);
324+
protected static
325+
<ServiceRpcT, OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>>
326+
ServiceRpcT createRpc(OptionsT options,
327+
Class<? extends ServiceRpcFactory<ServiceRpcT, OptionsT>> factoryClass) {
328+
ServiceRpcFactory<ServiceRpcT, OptionsT> factory =
329+
Iterables.getFirst(ServiceLoader.load(factoryClass), null);
323330
return factory == null ? null : factory.create(options);
324331
}
325332
}

gcloud-java-core/src/main/java/com/google/gcloud/spi/ServiceRpcFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
* A base interface for all service RPC factories.
2525
* Loading of a factory implementation is done via {@link java.util.ServiceLoader}.
2626
*/
27-
public interface ServiceRpcFactory<S, O extends ServiceOptions> extends Serializable {
27+
public interface ServiceRpcFactory<
28+
ServiceRpcT,
29+
OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>>
30+
extends Serializable {
2831

29-
S create(O options);
32+
ServiceRpcT create(OptionsT options);
3033
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@
4040

4141
/**
4242
* A base class for entities (key and properties).
43-
* An entity is Google Cloud Datastore persistent data object.
43+
* An entity is a Google Cloud Datastore persistent data object.
4444
* An entity holds one or more properties, represented by a name (as {@link String})
4545
* and a value (as {@link com.google.gcloud.datastore.Value}), and may be associated with a
4646
* key. For a list of possible values see {@link ValueType}.
4747
*
48-
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
48+
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore
49+
* Entities, Properties, and Keys</a>
4950
*/
5051
public abstract class BaseEntity<K extends IncompleteKey> extends Serializable<DatastoreV1.Entity> {
5152

@@ -54,7 +55,7 @@ public abstract class BaseEntity<K extends IncompleteKey> extends Serializable<D
5455
private final transient ImmutableSortedMap<String, Value<?>> properties;
5556
private final K key;
5657

57-
abstract static class Builder<K extends IncompleteKey, B extends Builder<K, B>> {
58+
public abstract static class Builder<K extends IncompleteKey, B extends Builder<K, B>> {
5859

5960
private K key;
6061
private final Map<String, Value<?>> properties = new HashMap<>();
@@ -127,7 +128,7 @@ public B remove(String name) {
127128
return self();
128129
}
129130

130-
public B set(String name, Value value) {
131+
public B set(String name, Value<?> value) {
131132
properties.put(name, value);
132133
return self();
133134
}
@@ -187,7 +188,7 @@ public B setNull(String name) {
187188
return self();
188189
}
189190

190-
public abstract BaseEntity build();
191+
public abstract BaseEntity<K> build();
191192
}
192193

193194
BaseEntity(Builder<K, ?> builder) {
@@ -213,7 +214,7 @@ public boolean equals(Object obj) {
213214
if (!(obj instanceof BaseEntity)) {
214215
return false;
215216
}
216-
BaseEntity other = (BaseEntity) obj;
217+
BaseEntity<?> other = (BaseEntity<?>) obj;
217218
return Objects.equals(key, other.key)
218219
&& Objects.equals(properties, other.properties);
219220
}
@@ -254,7 +255,7 @@ public <V extends Value<?>> V getValue(String name) {
254255
}
255256

256257
/**
257-
* Returns true if property is instanceof NullValue.
258+
* Returns true if property is an instance of NullValue.
258259
*
259260
* @throws DatastoreException if not such property.
260261
*/
@@ -375,12 +376,12 @@ ImmutableSortedMap<String, Value<?>> properties() {
375376

376377
@Override
377378
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
378-
Builder builder = emptyBuilder();
379+
Builder<?, ?> builder = emptyBuilder();
379380
builder.fill(DatastoreV1.Entity.parseFrom(bytesPb));
380381
return builder.build();
381382
}
382383

383-
protected abstract Builder emptyBuilder();
384+
protected abstract Builder<?, ?> emptyBuilder();
384385

385386
@Override
386387
protected final DatastoreV1.Entity toPb() {

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static class ResponseImpl implements Batch.Response {
3434

3535
private final DatastoreV1.CommitResponse response;
3636

37-
public ResponseImpl(DatastoreV1.CommitResponse response) {
37+
ResponseImpl(DatastoreV1.CommitResponse response) {
3838
this.response = response;
3939
}
4040

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public interface Datastore extends Service<DatastoreOptions>, DatastoreReaderWri
3434

3535

3636
/**
37-
* An Callback for running with a Transactional
37+
* A callback for running with a transactional
3838
* {@link com.google.gcloud.datastore.DatastoreReaderWriter}.
3939
* The associated transaction will be committed after a successful return from the {@code run}
4040
* method. Any propagated exception will cause the transaction to be rolled-back.

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class DatastoreException extends RuntimeException {
3737
/**
3838
* An error code to represent the failure.
3939
*
40-
* @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud Datastore error codes</a>
40+
* @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
41+
* Datastore error codes</a>
4142
*/
4243
public enum Code {
4344

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreImpl.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package com.google.gcloud.datastore;
1818

1919
import com.google.api.services.datastore.DatastoreV1;
20-
import com.google.common.base.Function;
2120
import com.google.common.base.MoreObjects;
2221
import com.google.common.base.Preconditions;
2322
import com.google.common.collect.AbstractIterator;
2423
import com.google.common.collect.ImmutableList;
25-
import com.google.common.collect.Iterators;
2624
import com.google.common.collect.Sets;
2725
import com.google.gcloud.BaseService;
2826
import com.google.gcloud.ExceptionHandler;
@@ -131,15 +129,11 @@ public List<Key> allocateId(IncompleteKey... keys) {
131129
requestPb.addKey(trimNameOrId(key).toPb());
132130
}
133131
DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build());
134-
Iterator<DatastoreV1.Key> keyIterator = responsePb.getKeyList().iterator();
135-
ImmutableList.Builder<Key> builder = ImmutableList.<Key>builder().addAll(
136-
Iterators.transform(keyIterator, new Function<DatastoreV1.Key, Key>() {
137-
@Override
138-
public Key apply(DatastoreV1.Key keyPb) {
139-
return Key.fromPb(keyPb);
140-
}
141-
}));
142-
return builder.build();
132+
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
133+
for (DatastoreV1.Key keyPb : responsePb.getKeyList()) {
134+
keyList.add(Key.fromPb(keyPb));
135+
}
136+
return keyList.build();
143137
}
144138

145139
DatastoreV1.AllocateIdsResponse allocateIds(final DatastoreV1.AllocateIdsRequest requestPb) {
@@ -256,9 +250,6 @@ private void loadResults() {
256250
@SuppressWarnings("unchecked")
257251
@Override
258252
protected Entity computeNext() {
259-
if (iter.hasNext()) {
260-
return Entity.fromPb(iter.next().getEntity());
261-
}
262253
while (!iter.hasNext()) {
263254
if (requestPb.getKeyCount() == 0) {
264255
return endOfData();

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public Builder toBuilder() {
165165

166166
@Override
167167
public int hashCode() {
168-
return super.hashCode() ^ Objects.hash(namespace, force, normalizeDataset);
168+
return baseHashCode() ^ Objects.hash(namespace, force, normalizeDataset);
169169
}
170170

171171
@Override
@@ -174,7 +174,7 @@ public boolean equals(Object obj) {
174174
return false;
175175
}
176176
DatastoreOptions other = (DatastoreOptions) obj;
177-
return isEquals(other) && Objects.equals(namespace, other.namespace)
177+
return baseEquals(other) && Objects.equals(namespace, other.namespace)
178178
&& Objects.equals(force, other.force)
179179
&& Objects.equals(normalizeDataset, other.normalizeDataset);
180180
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DateTime.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
* A Google Cloud Datastore timestamp (represented in micro-seconds).
3232
* This class is immutable.
3333
*
34-
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
34+
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore
35+
* Entities, Properties, and Keys</a>
3536
*/
3637
public final class DateTime extends Serializable<DatastoreV1.Value>
3738
implements Comparable<DateTime> {
@@ -61,8 +62,9 @@ public int compareTo(DateTime other) {
6162

6263
@Override
6364
public boolean equals(Object obj) {
64-
return obj == this || obj instanceof DateTime
65-
&& timestampMicroseconds == ((DateTime) obj).timestampMicroseconds;
65+
return obj == this
66+
|| (obj instanceof DateTime
67+
&& timestampMicroseconds == ((DateTime) obj).timestampMicroseconds);
6668
}
6769

6870
public long timestampMicroseconds() {

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Entity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public Entity build() {
7070
}
7171

7272
@Override
73-
protected BaseEntity.Builder emptyBuilder() {
73+
protected BaseEntity.Builder<Key, Builder> emptyBuilder() {
7474
return new Builder();
7575
}
7676

0 commit comments

Comments
 (0)