-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support GeoPoint value in Datastore v1beta3
- Loading branch information
Ajay Kannan
committed
Oct 19, 2015
1 parent
af6ab8f
commit f6eeddd
Showing
12 changed files
with
384 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud.datastore; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* A Google Cloud Datastore GeoPoint (represented by latitude and longitude in degrees). | ||
* This class is immutable. | ||
* | ||
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore | ||
* Entities, Properties, and Keys</a> | ||
*/ | ||
public final class GeoPoint extends Serializable<com.google.type.LatLng> { | ||
|
||
private static final long serialVersionUID = 9077060962655752073L; | ||
|
||
private final transient double latitude; | ||
private final transient double longitude; | ||
|
||
GeoPoint(double latitude, double longitude) { | ||
checkArgument( | ||
latitude >= -90.0 && latitude <= 90.0, "latitude must be in the range [-90, 90] degrees"); | ||
checkArgument( | ||
longitude >= -180.0 && longitude <= 180.0, | ||
"latitude must be in the range [-180, 180] degrees"); | ||
this.latitude = latitude; | ||
this.longitude = longitude; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Double.toString(latitude) + ", " + Double.toString(longitude); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(latitude, longitude); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj == this | ||
|| (obj instanceof GeoPoint && new Double(this.latitude).equals(((GeoPoint) obj).latitude)) | ||
&& new Double(this.longitude).equals(((GeoPoint) obj).longitude); | ||
} | ||
|
||
public static GeoPoint of(double latitude, double longitude) { | ||
return new GeoPoint(latitude, longitude); | ||
} | ||
|
||
@Override | ||
protected com.google.type.LatLng toPb() { | ||
return com.google.type.LatLng.newBuilder() | ||
.setLatitude(latitude) | ||
.setLongitude(longitude) | ||
.build(); | ||
} | ||
|
||
@Override | ||
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException { | ||
com.google.type.LatLng parsedLatLng = com.google.type.LatLng.parseFrom(bytesPb); | ||
return new GeoPoint(parsedLatLng.getLatitude(), parsedLatLng.getLongitude()); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GeoPointValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2015 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gcloud.datastore; | ||
|
||
import static com.google.datastore.v1beta3.Value.GEO_POINT_VALUE_FIELD_NUMBER; | ||
|
||
public final class GeoPointValue extends Value<GeoPoint> { | ||
|
||
private static final long serialVersionUID = -5810614280642405898L; | ||
|
||
static final BaseMarshaller<GeoPoint, GeoPointValue, Builder> MARSHALLER = | ||
new BaseMarshaller<GeoPoint, GeoPointValue, Builder>() { | ||
|
||
private static final long serialVersionUID = -3550567536035178649L; | ||
|
||
@Override | ||
public int getProtoFieldId() { | ||
return GEO_POINT_VALUE_FIELD_NUMBER; | ||
} | ||
|
||
@Override | ||
public Builder newBuilder(GeoPoint value) { | ||
return builder(value); | ||
} | ||
|
||
@Override | ||
protected GeoPoint getValue(com.google.datastore.v1beta3.Value from) { | ||
return new GeoPoint( | ||
from.getGeoPointValue().getLatitude(), from.getGeoPointValue().getLongitude()); | ||
} | ||
|
||
@Override | ||
protected void setValue(GeoPointValue from, com.google.datastore.v1beta3.Value.Builder to) { | ||
to.setGeoPointValue(from.get().toPb()); | ||
} | ||
}; | ||
|
||
public static final class Builder extends Value.BaseBuilder<GeoPoint, GeoPointValue, Builder> { | ||
|
||
private Builder() { | ||
super(ValueType.GEO_POINT); | ||
} | ||
|
||
@Override | ||
public GeoPointValue build() { | ||
return new GeoPointValue(this); | ||
} | ||
} | ||
|
||
public GeoPointValue(GeoPoint value) { | ||
this(builder(value)); | ||
} | ||
|
||
private GeoPointValue(Builder builder) { | ||
super(builder); | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return new Builder().mergeFrom(this); | ||
} | ||
|
||
public static GeoPointValue of(GeoPoint value) { | ||
return new GeoPointValue(value); | ||
} | ||
|
||
public static Builder builder(GeoPoint value) { | ||
return new Builder().set(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.