-
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.
Merge pull request #270 from ajkannan/geopoint-value
Add GeoPoint value
- Loading branch information
Showing
10 changed files
with
393 additions
and
53 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
90 changes: 90 additions & 0 deletions
90
gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLng.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,90 @@ | ||
/* | ||
* 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 LatLng (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 LatLng extends Serializable<com.google.type.LatLng> { | ||
|
||
private static final long serialVersionUID = 9077060962655752073L; | ||
|
||
private final transient double latitude; | ||
private final transient double longitude; | ||
|
||
LatLng(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; | ||
} | ||
|
||
public double latitude() { | ||
return latitude; | ||
} | ||
|
||
public double longitude() { | ||
return 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 LatLng && this.latitude == ((LatLng) obj).latitude | ||
&& this.longitude == ((LatLng) obj).longitude); | ||
} | ||
|
||
public static LatLng of(double latitude, double longitude) { | ||
return new LatLng(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 LatLng(parsedLatLng.getLatitude(), parsedLatLng.getLongitude()); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/LatLngValue.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 LatLngValue extends Value<LatLng> { | ||
|
||
private static final long serialVersionUID = -5810614280642405898L; | ||
|
||
static final BaseMarshaller<LatLng, LatLngValue, Builder> MARSHALLER = | ||
new BaseMarshaller<LatLng, LatLngValue, Builder>() { | ||
|
||
private static final long serialVersionUID = -3550567536035178649L; | ||
|
||
@Override | ||
public int getProtoFieldId() { | ||
return GEO_POINT_VALUE_FIELD_NUMBER; | ||
} | ||
|
||
@Override | ||
public Builder newBuilder(LatLng value) { | ||
return builder(value); | ||
} | ||
|
||
@Override | ||
protected LatLng getValue(com.google.datastore.v1beta3.Value from) { | ||
return new LatLng( | ||
from.getGeoPointValue().getLatitude(), from.getGeoPointValue().getLongitude()); | ||
} | ||
|
||
@Override | ||
protected void setValue(LatLngValue from, com.google.datastore.v1beta3.Value.Builder to) { | ||
to.setGeoPointValue(from.get().toPb()); | ||
} | ||
}; | ||
|
||
public static final class Builder extends Value.BaseBuilder<LatLng, LatLngValue, Builder> { | ||
|
||
private Builder() { | ||
super(ValueType.LAT_LNG); | ||
} | ||
|
||
@Override | ||
public LatLngValue build() { | ||
return new LatLngValue(this); | ||
} | ||
} | ||
|
||
public LatLngValue(LatLng value) { | ||
this(builder(value)); | ||
} | ||
|
||
private LatLngValue(Builder builder) { | ||
super(builder); | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return new Builder().mergeFrom(this); | ||
} | ||
|
||
public static LatLngValue of(LatLng value) { | ||
return new LatLngValue(value); | ||
} | ||
|
||
public static Builder builder(LatLng 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
Oops, something went wrong.