geoloc: GeoLocation constructor should have Builder as sole paramter

Also remove that (broken) "Error and accuracy set" warning, but
mark (get|set)Error() as deprecated.
This commit is contained in:
Florian Schmaus 2020-04-13 19:59:22 +02:00
parent 9b20e2efd8
commit f5c412a98f
4 changed files with 62 additions and 63 deletions

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez, 2019 Florian Schmaus * Copyright 2015-2017 Ishan Khanna, Fernando Ramirez, 2019-2020 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,14 +19,11 @@ package org.jivesoftware.smackx.geoloc.packet;
import java.io.Serializable; import java.io.Serializable;
import java.net.URI; import java.net.URI;
import java.util.Date; import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.xdata.FormField; import org.jivesoftware.smackx.xdata.FormField;
@ -50,8 +47,6 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi
public static final GeoLocation EMPTY_GEO_LOCATION = GeoLocation.builder().build(); public static final GeoLocation EMPTY_GEO_LOCATION = GeoLocation.builder().build();
private static final Logger LOGGER = Logger.getLogger(GeoLocation.class.getName());
private final Double accuracy; private final Double accuracy;
private final Double alt; private final Double alt;
private final Double altAccuracy; private final Double altAccuracy;
@ -77,50 +72,31 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi
private final String tzo; private final String tzo;
private final URI uri; private final URI uri;
private GeoLocation(Double accuracy, Double alt, Double altAccuracy, String area, Double bearing, String building, String country, private GeoLocation(Builder builder) {
String countryCode, String datum, String description, Double error, String floor, Double lat, accuracy = builder.accuracy;
String locality, Double lon, String postalcode, String region, String room, Double speed, alt = builder.alt;
String street, String text, Date timestamp, String tzo, URI uri) { altAccuracy = builder.altAccuracy;
this.accuracy = accuracy; area = builder.area;
this.alt = alt; bearing = builder.bearing;
this.altAccuracy = altAccuracy; building = builder.building;
this.area = area; country = builder.country;
this.bearing = bearing; countryCode = builder.countryCode;
this.building = building; datum = builder.datum;
this.country = country; description = builder.description;
this.countryCode = countryCode; error = builder.error;
floor = builder.floor;
// If datum is not included, receiver MUST assume WGS84; receivers MUST implement WGS84; senders MAY use another lat = builder.lat;
// datum, but it is not recommended. locality = builder.locality;
lon = builder.lon;
if (StringUtils.isNullOrEmpty(datum)) { postalcode = builder.postalcode;
datum = "WGS84"; region = builder.region;
} room = builder.room;
speed = builder.speed;
this.datum = datum; street = builder.street;
this.description = description; text = builder.text;
timestamp = builder.timestamp;
// error element is deprecated in favor of accuracy tzo = builder.tzo;
if (accuracy != null) { uri = builder.uri;
error = null;
LOGGER.log(Level.WARNING,
"Error and accuracy set. Ignoring error as it is deprecated in favor of accuracy");
}
this.error = error;
this.floor = floor;
this.lat = lat;
this.locality = locality;
this.lon = lon;
this.postalcode = postalcode;
this.region = region;
this.room = room;
this.speed = speed;
this.street = street;
this.text = text;
this.timestamp = timestamp;
this.tzo = tzo;
this.uri = uri;
} }
public Double getAccuracy() { public Double getAccuracy() {
@ -163,6 +139,13 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi
return description; return description;
} }
/**
* Get the error.
*
* @return the error.
* @deprecated use {@link #getAccuracy()} instead.
*/
@Deprecated
public Double getError() { public Double getError() {
return error; return error;
} }
@ -318,7 +301,11 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi
private String building; private String building;
private String country; private String country;
private String countryCode; private String countryCode;
private String datum;
// If datum is not included, receiver MUST assume WGS84; receivers MUST implement WGS84; senders MAY use another
// datum, but it is not recommended.
private String datum = "WGS84";
private String description; private String description;
private Double error; private Double error;
private String floor; private String floor;
@ -453,7 +440,9 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi
* *
* @param error error in arc minutes * @param error error in arc minutes
* @return Builder * @return Builder
* @deprecated use {@link #setAccuracy(Double)} instead.
*/ */
@Deprecated
public Builder setError(Double error) { public Builder setError(Double error) {
this.error = error; this.error = error;
return this; return this;
@ -610,10 +599,7 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi
* @return GeoLocation * @return GeoLocation
*/ */
public GeoLocation build() { public GeoLocation build() {
return new GeoLocation(this);
return new GeoLocation(accuracy, alt, altAccuracy, area, bearing, building, country, countryCode, datum, description,
error, floor, lat, locality, lon, postalcode, region, room, speed, street, text, timestamp,
tzo, uri);
} }
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2015-2017 Ishan Khanna, Fernando Ramirez, 2019 Florian Schmaus * Copyright 2015-2017 Ishan Khanna, Fernando Ramirez, 2019-2020 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,7 +79,7 @@ public class GeoLocationProvider extends ExtensionElementProvider<GeoLocation> {
builder.setDescription(parser.nextText()); builder.setDescription(parser.nextText());
break; break;
case "error": case "error":
builder.setError(ParserUtils.getDoubleFromNextText(parser)); parseError(builder, parser);
break; break;
case "floor": case "floor":
builder.setFloor(parser.nextText()); builder.setFloor(parser.nextText());
@ -136,6 +136,12 @@ public class GeoLocationProvider extends ExtensionElementProvider<GeoLocation> {
return builder.build(); return builder.build();
} }
@SuppressWarnings("deprecation")
private static void parseError(GeoLocation.Builder builder, XmlPullParser parser) throws XmlPullParserException, IOException {
double error = ParserUtils.getDoubleFromNextText(parser);
builder.setError(error);
}
public static class GeoLocationFormFieldChildElementProvider extends FormFieldChildElementProvider<GeoLocation> { public static class GeoLocationFormFieldChildElementProvider extends FormFieldChildElementProvider<GeoLocation> {
public static final GeoLocationFormFieldChildElementProvider INSTANCE = new GeoLocationFormFieldChildElementProvider(); public static final GeoLocationFormFieldChildElementProvider INSTANCE = new GeoLocationFormFieldChildElementProvider();

View File

@ -121,6 +121,7 @@ public class GeoLocationTest extends SmackTestSuite {
assertNotNull(geoLocation); assertNotNull(geoLocation);
assertNotNull(geoLocation.toXML()); assertNotNull(geoLocation.toXML());
@SuppressWarnings("deprecation")
GeoLocation constructedGeoLocation = GeoLocation.builder().setAccuracy(23d).setAlt(1000d).setAltAccuracy(10d).setArea("Delhi").setBearing( GeoLocation constructedGeoLocation = GeoLocation.builder().setAccuracy(23d).setAlt(1000d).setAltAccuracy(10d).setArea("Delhi").setBearing(
10d).setBuilding("Small Building").setCountry("India").setCountryCode("IN").setDescription( 10d).setBuilding("Small Building").setCountry("India").setCountryCode("IN").setDescription(
"My Description").setError(90d).setFloor("top").setLat(25.098345d).setLocality("awesome").setLon( "My Description").setError(90d).setFloor("top").setLat(25.098345d).setLocality("awesome").setLon(

View File

@ -18,7 +18,6 @@ package org.jivesoftware.smackx.geoloc.provider;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.net.URI; import java.net.URI;
@ -82,7 +81,9 @@ public class GeoLocationProviderTest extends SmackTestSuite {
assertEquals("IN", geoLocation.getCountryCode()); assertEquals("IN", geoLocation.getCountryCode());
assertEquals("WGS84", geoLocation.getDatum()); assertEquals("WGS84", geoLocation.getDatum());
assertEquals("My Description", geoLocation.getDescription()); assertEquals("My Description", geoLocation.getDescription());
assertNull(geoLocation.getError()); @SuppressWarnings("deprecation")
Double error = geoLocation.getError();
assertEquals(90, error);
assertEquals("top", geoLocation.getFloor()); assertEquals("top", geoLocation.getFloor());
assertEquals((Double) 25.098345d, geoLocation.getLat()); assertEquals((Double) 25.098345d, geoLocation.getLat());
assertEquals("awesome", geoLocation.getLocality()); assertEquals("awesome", geoLocation.getLocality());
@ -150,7 +151,9 @@ public class GeoLocationProviderTest extends SmackTestSuite {
assertEquals("IN", geoLocation.getCountryCode()); assertEquals("IN", geoLocation.getCountryCode());
assertEquals("Test Datum", geoLocation.getDatum()); assertEquals("Test Datum", geoLocation.getDatum());
assertEquals("My Description", geoLocation.getDescription()); assertEquals("My Description", geoLocation.getDescription());
assertNull(geoLocation.getError()); @SuppressWarnings("deprecation")
Double error = geoLocation.getError();
assertEquals(90, error);
assertEquals("top", geoLocation.getFloor()); assertEquals("top", geoLocation.getFloor());
assertEquals((Double) 25.098345d, geoLocation.getLat()); assertEquals((Double) 25.098345d, geoLocation.getLat());
assertEquals("awesome", geoLocation.getLocality()); assertEquals("awesome", geoLocation.getLocality());
@ -183,7 +186,9 @@ public class GeoLocationProviderTest extends SmackTestSuite {
GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class); GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class);
assertEquals((Double) 90d, geoLocation.getError()); @SuppressWarnings("deprecation")
Double error = geoLocation.getError();
assertEquals((Double) 90d, error);
} }
@Test @Test
@ -223,8 +228,9 @@ public class GeoLocationProviderTest extends SmackTestSuite {
GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class); GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class);
assertEquals((Double) 90d, geoLocation.getAccuracy()); assertEquals((Double) 90d, geoLocation.getAccuracy());
assertNull(geoLocation.getError()); @SuppressWarnings("deprecation")
Double error = geoLocation.getError();
assertEquals(100, error);
} }
} }