Skip to content

Commit

Permalink
Merge pull request #97 from eamonnmcmanus/master
Browse files Browse the repository at this point in the history
Minor cleanups. No behaviour or API changes are intended, …
  • Loading branch information
aozarov committed Jun 8, 2015
2 parents a1f2480 + a37d3d6 commit 27cd1d7
Show file tree
Hide file tree
Showing 48 changed files with 189 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ public static AuthCredentials createForComputeEngine()
/**
* Returns the Application Default Credentials.
*
* <p>
* Returns the Application Default Credentials which are credentials that identify and authorize
* the whole application. This is the built-in service account if running on Google Compute Engine
* or the credentials file from the path in the environment variable
* <p>Returns the Application Default Credentials which are credentials that identify and
* authorize the whole application. This is the built-in service account if running on
* Google Compute Engine or the credentials file from the path in the environment variable
* GOOGLE_APPLICATION_CREDENTIALS.
* </p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package com.google.gcloud;

public abstract class BaseService<O extends ServiceOptions> implements Service<O> {
public abstract class BaseService<OptionsT extends ServiceOptions<?, OptionsT>>
implements Service<OptionsT> {

private final O options;
private final OptionsT options;

protected BaseService(O options) {
protected BaseService(OptionsT options) {
this.options = options;
}

@Override
public O options() {
public OptionsT options() {
return options;
}
}
4 changes: 2 additions & 2 deletions gcloud-java-core/src/main/java/com/google/gcloud/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

package com.google.gcloud;

public interface Service<O extends ServiceOptions> {
O options();
public interface Service<OptionsT extends ServiceOptions<?, OptionsT>> {
OptionsT options();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class ServiceOptions<R, O extends ServiceOptions<R, O>> implements Serializable {
public abstract class ServiceOptions<
ServiceRpcT,
OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>>
implements Serializable {

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

public interface HttpTransportFactory extends Serializable {
HttpTransport create();
Expand Down Expand Up @@ -88,19 +91,21 @@ public HttpTransport create() {



protected abstract static class Builder<R, O extends ServiceOptions<R, O>,
B extends Builder<R, O, B>> {
protected abstract static class Builder<
ServiceRpcT,
OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>,
B extends Builder<ServiceRpcT, OptionsT, B>> {

private String projectId;
private String host;
private HttpTransportFactory httpTransportFactory;
private AuthCredentials authCredentials;
private RetryParams retryParams;
private ServiceRpcFactory<R, O> serviceRpcFactory;
private ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;

protected Builder() {}

protected Builder(ServiceOptions<R, O> options) {
protected Builder(ServiceOptions<ServiceRpcT, OptionsT> options) {
projectId = options.projectId;
host = options.host;
httpTransportFactory = options.httpTransportFactory;
Expand All @@ -109,7 +114,7 @@ protected Builder(ServiceOptions<R, O> options) {
serviceRpcFactory = options.serviceRpcFactory;
}

protected abstract ServiceOptions build();
protected abstract ServiceOptions<ServiceRpcT, OptionsT> build();

@SuppressWarnings("unchecked")
protected B self() {
Expand Down Expand Up @@ -141,13 +146,13 @@ public B retryParams(RetryParams retryParams) {
return self();
}

public B serviceRpcFactory(ServiceRpcFactory<R, O> serviceRpcFactory) {
public B serviceRpcFactory(ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory) {
this.serviceRpcFactory = serviceRpcFactory;
return self();
}
}

protected ServiceOptions(Builder<R, O, ?> builder) {
protected ServiceOptions(Builder<ServiceRpcT, OptionsT, ?> builder) {
projectId = checkNotNull(builder.projectId != null ? builder.projectId : defaultProject());
host = firstNonNull(builder.host, DEFAULT_HOST);
httpTransportFactory =
Expand Down Expand Up @@ -222,7 +227,7 @@ protected static String googleCloudProjectId() {
String section = null;
Pattern projectPattern = Pattern.compile("^project\\s*=\\s*(.*)$");
Pattern sectionPattern = Pattern.compile("^\\[(.*)\\]$");
while((line = reader.readLine()) != null) {
while ((line = reader.readLine()) != null) {
if (line.isEmpty() || line.startsWith(";")) {
continue;
}
Expand All @@ -245,7 +250,7 @@ protected static String googleCloudProjectId() {
}

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

protected static String getAppEngineProjectId() {
Expand Down Expand Up @@ -288,7 +293,7 @@ public RetryParams retryParams() {
return retryParams != null ? retryParams : RetryParams.noRetries();
}

public ServiceRpcFactory<R, O> serviceRpcFactory() {
public ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory() {
return serviceRpcFactory;
}

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

@Override
public int hashCode() {
protected int baseHashCode() {
return Objects.hash(projectId, host, httpTransportFactory, authCredentials, retryParams,
serviceRpcFactory);
}

protected boolean isEquals(ServiceOptions other) {
protected boolean baseEquals(ServiceOptions<?, ?> other) {
return Objects.equals(projectId, other.projectId)
&& Objects.equals(host, other.host)
&& Objects.equals(httpTransportFactory, other.httpTransportFactory)
Expand All @@ -312,14 +316,17 @@ protected boolean isEquals(ServiceOptions other) {
&& Objects.equals(serviceRpcFactory, other.serviceRpcFactory);
}

public abstract Builder<R, O, ?> toBuilder();
public abstract Builder<ServiceRpcT, OptionsT, ?> toBuilder();

/**
* Creates a service RPC using a factory loaded by {@link ServiceLoader}.
*/
protected static <R, O extends ServiceOptions<R, O>> R createRpc(O options,
Class<? extends ServiceRpcFactory<R, O>> factoryClass) {
ServiceRpcFactory<R, O> factory = Iterables.getFirst(ServiceLoader.load(factoryClass), null);
protected static
<ServiceRpcT, OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>>
ServiceRpcT createRpc(OptionsT options,
Class<? extends ServiceRpcFactory<ServiceRpcT, OptionsT>> factoryClass) {
ServiceRpcFactory<ServiceRpcT, OptionsT> factory =
Iterables.getFirst(ServiceLoader.load(factoryClass), null);
return factory == null ? null : factory.create(options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
* A base interface for all service RPC factories.
* Loading of a factory implementation is done via {@link java.util.ServiceLoader}.
*/
public interface ServiceRpcFactory<S, O extends ServiceOptions> extends Serializable {
public interface ServiceRpcFactory<
ServiceRpcT,
OptionsT extends ServiceOptions<ServiceRpcT, OptionsT>>
extends Serializable {

S create(O options);
ServiceRpcT create(OptionsT options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@

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

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

abstract static class Builder<K extends IncompleteKey, B extends Builder<K, B>> {
public abstract static class Builder<K extends IncompleteKey, B extends Builder<K, B>> {

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

public B set(String name, Value value) {
public B set(String name, Value<?> value) {
properties.put(name, value);
return self();
}
Expand Down Expand Up @@ -187,7 +188,7 @@ public B setNull(String name) {
return self();
}

public abstract BaseEntity build();
public abstract BaseEntity<K> build();
}

BaseEntity(Builder<K, ?> builder) {
Expand All @@ -213,7 +214,7 @@ public boolean equals(Object obj) {
if (!(obj instanceof BaseEntity)) {
return false;
}
BaseEntity other = (BaseEntity) obj;
BaseEntity<?> other = (BaseEntity<?>) obj;
return Objects.equals(key, other.key)
&& Objects.equals(properties, other.properties);
}
Expand Down Expand Up @@ -254,7 +255,7 @@ public <V extends Value<?>> V getValue(String name) {
}

/**
* Returns true if property is instanceof NullValue.
* Returns true if property is an instance of NullValue.
*
* @throws DatastoreException if not such property.
*/
Expand Down Expand Up @@ -375,12 +376,12 @@ ImmutableSortedMap<String, Value<?>> properties() {

@Override
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
Builder builder = emptyBuilder();
Builder<?, ?> builder = emptyBuilder();
builder.fill(DatastoreV1.Entity.parseFrom(bytesPb));
return builder.build();
}

protected abstract Builder emptyBuilder();
protected abstract Builder<?, ?> emptyBuilder();

@Override
protected final DatastoreV1.Entity toPb() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static class ResponseImpl implements Batch.Response {

private final DatastoreV1.CommitResponse response;

public ResponseImpl(DatastoreV1.CommitResponse response) {
ResponseImpl(DatastoreV1.CommitResponse response) {
this.response = response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface Datastore extends Service<DatastoreOptions>, DatastoreReaderWri


/**
* An Callback for running with a Transactional
* A callback for running with a transactional
* {@link com.google.gcloud.datastore.DatastoreReaderWriter}.
* The associated transaction will be committed after a successful return from the {@code run}
* method. Any propagated exception will cause the transaction to be rolled-back.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class DatastoreException extends RuntimeException {
/**
* An error code to represent the failure.
*
* @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud Datastore error codes</a>
* @see <a href="https://cloud.google.com/datastore/docs/concepts/errors#Error_Codes">Google Cloud
* Datastore error codes</a>
*/
public enum Code {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package com.google.gcloud.datastore;

import com.google.api.services.datastore.DatastoreV1;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
import com.google.gcloud.BaseService;
import com.google.gcloud.ExceptionHandler;
Expand Down Expand Up @@ -131,15 +129,11 @@ public List<Key> allocateId(IncompleteKey... keys) {
requestPb.addKey(trimNameOrId(key).toPb());
}
DatastoreV1.AllocateIdsResponse responsePb = allocateIds(requestPb.build());
Iterator<DatastoreV1.Key> keyIterator = responsePb.getKeyList().iterator();
ImmutableList.Builder<Key> builder = ImmutableList.<Key>builder().addAll(
Iterators.transform(keyIterator, new Function<DatastoreV1.Key, Key>() {
@Override
public Key apply(DatastoreV1.Key keyPb) {
return Key.fromPb(keyPb);
}
}));
return builder.build();
ImmutableList.Builder<Key> keyList = ImmutableList.builder();
for (DatastoreV1.Key keyPb : responsePb.getKeyList()) {
keyList.add(Key.fromPb(keyPb));
}
return keyList.build();
}

DatastoreV1.AllocateIdsResponse allocateIds(final DatastoreV1.AllocateIdsRequest requestPb) {
Expand Down Expand Up @@ -256,9 +250,6 @@ private void loadResults() {
@SuppressWarnings("unchecked")
@Override
protected Entity computeNext() {
if (iter.hasNext()) {
return Entity.fromPb(iter.next().getEntity());
}
while (!iter.hasNext()) {
if (requestPb.getKeyCount() == 0) {
return endOfData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public Builder toBuilder() {

@Override
public int hashCode() {
return super.hashCode() ^ Objects.hash(namespace, force, normalizeDataset);
return baseHashCode() ^ Objects.hash(namespace, force, normalizeDataset);
}

@Override
Expand All @@ -174,7 +174,7 @@ public boolean equals(Object obj) {
return false;
}
DatastoreOptions other = (DatastoreOptions) obj;
return isEquals(other) && Objects.equals(namespace, other.namespace)
return baseEquals(other) && Objects.equals(namespace, other.namespace)
&& Objects.equals(force, other.force)
&& Objects.equals(normalizeDataset, other.normalizeDataset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
* A Google Cloud Datastore timestamp (represented in micro-seconds).
* This class is immutable.
*
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore Entities, Properties, and Keys</a>
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore
* Entities, Properties, and Keys</a>
*/
public final class DateTime extends Serializable<DatastoreV1.Value>
implements Comparable<DateTime> {
Expand Down Expand Up @@ -61,8 +62,9 @@ public int compareTo(DateTime other) {

@Override
public boolean equals(Object obj) {
return obj == this || obj instanceof DateTime
&& timestampMicroseconds == ((DateTime) obj).timestampMicroseconds;
return obj == this
|| (obj instanceof DateTime
&& timestampMicroseconds == ((DateTime) obj).timestampMicroseconds);
}

public long timestampMicroseconds() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Entity build() {
}

@Override
protected BaseEntity.Builder emptyBuilder() {
protected BaseEntity.Builder<Key, Builder> emptyBuilder() {
return new Builder();
}

Expand Down
Loading

0 comments on commit 27cd1d7

Please sign in to comment.