From f5c412a98f47ded4ba5610a67a5381cff35dcd2e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 13 Apr 2020 19:59:22 +0200 Subject: [PATCH] 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. --- .../smackx/geoloc/packet/GeoLocation.java | 96 ++++++++----------- .../geoloc/provider/GeoLocationProvider.java | 10 +- .../smackx/geoloc/packet/GeoLocationTest.java | 1 + .../provider/GeoLocationProviderTest.java | 18 ++-- 4 files changed, 62 insertions(+), 63 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/packet/GeoLocation.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/packet/GeoLocation.java index 3cf633ee8..61285a11f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/packet/GeoLocation.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/packet/GeoLocation.java @@ -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"); * 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.net.URI; import java.util.Date; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.xml.namespace.QName; import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; -import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; 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(); - private static final Logger LOGGER = Logger.getLogger(GeoLocation.class.getName()); - private final Double accuracy; private final Double alt; private final Double altAccuracy; @@ -77,50 +72,31 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi private final String tzo; private final URI uri; - private GeoLocation(Double accuracy, Double alt, Double altAccuracy, String area, Double bearing, String building, String country, - String countryCode, String datum, String description, Double error, String floor, Double lat, - String locality, Double lon, String postalcode, String region, String room, Double speed, - String street, String text, Date timestamp, String tzo, URI uri) { - this.accuracy = accuracy; - this.alt = alt; - this.altAccuracy = altAccuracy; - this.area = area; - this.bearing = bearing; - this.building = building; - this.country = country; - this.countryCode = countryCode; - - // If datum is not included, receiver MUST assume WGS84; receivers MUST implement WGS84; senders MAY use another - // datum, but it is not recommended. - - if (StringUtils.isNullOrEmpty(datum)) { - datum = "WGS84"; - } - - this.datum = datum; - this.description = description; - - // error element is deprecated in favor of accuracy - if (accuracy != null) { - 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; + private GeoLocation(Builder builder) { + accuracy = builder.accuracy; + alt = builder.alt; + altAccuracy = builder.altAccuracy; + area = builder.area; + bearing = builder.bearing; + building = builder.building; + country = builder.country; + countryCode = builder.countryCode; + datum = builder.datum; + description = builder.description; + error = builder.error; + floor = builder.floor; + lat = builder.lat; + locality = builder.locality; + lon = builder.lon; + postalcode = builder.postalcode; + region = builder.region; + room = builder.room; + speed = builder.speed; + street = builder.street; + text = builder.text; + timestamp = builder.timestamp; + tzo = builder.tzo; + uri = builder.uri; } public Double getAccuracy() { @@ -163,6 +139,13 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi return description; } + /** + * Get the error. + * + * @return the error. + * @deprecated use {@link #getAccuracy()} instead. + */ + @Deprecated public Double getError() { return error; } @@ -318,7 +301,11 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi private String building; private String country; 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 Double error; private String floor; @@ -453,7 +440,9 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi * * @param error error in arc minutes * @return Builder + * @deprecated use {@link #setAccuracy(Double)} instead. */ + @Deprecated public Builder setError(Double error) { this.error = error; return this; @@ -610,10 +599,7 @@ public final class GeoLocation implements Serializable, ExtensionElement, FormFi * @return GeoLocation */ public GeoLocation build() { - - 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); + return new GeoLocation(this); } } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProvider.java index cf6234aa4..08275293e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProvider.java @@ -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"); * you may not use this file except in compliance with the License. @@ -79,7 +79,7 @@ public class GeoLocationProvider extends ExtensionElementProvider { builder.setDescription(parser.nextText()); break; case "error": - builder.setError(ParserUtils.getDoubleFromNextText(parser)); + parseError(builder, parser); break; case "floor": builder.setFloor(parser.nextText()); @@ -136,6 +136,12 @@ public class GeoLocationProvider extends ExtensionElementProvider { 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 { public static final GeoLocationFormFieldChildElementProvider INSTANCE = new GeoLocationFormFieldChildElementProvider(); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/packet/GeoLocationTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/packet/GeoLocationTest.java index f8af5da0b..60003cb36 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/packet/GeoLocationTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/packet/GeoLocationTest.java @@ -121,6 +121,7 @@ public class GeoLocationTest extends SmackTestSuite { assertNotNull(geoLocation); assertNotNull(geoLocation.toXML()); + @SuppressWarnings("deprecation") GeoLocation constructedGeoLocation = GeoLocation.builder().setAccuracy(23d).setAlt(1000d).setAltAccuracy(10d).setArea("Delhi").setBearing( 10d).setBuilding("Small Building").setCountry("India").setCountryCode("IN").setDescription( "My Description").setError(90d).setFloor("top").setLat(25.098345d).setLocality("awesome").setLon( diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProviderTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProviderTest.java index 01738fa3e..ee18cd493 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProviderTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/geoloc/provider/GeoLocationProviderTest.java @@ -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.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; import java.net.URI; @@ -82,7 +81,9 @@ public class GeoLocationProviderTest extends SmackTestSuite { assertEquals("IN", geoLocation.getCountryCode()); assertEquals("WGS84", geoLocation.getDatum()); assertEquals("My Description", geoLocation.getDescription()); - assertNull(geoLocation.getError()); + @SuppressWarnings("deprecation") + Double error = geoLocation.getError(); + assertEquals(90, error); assertEquals("top", geoLocation.getFloor()); assertEquals((Double) 25.098345d, geoLocation.getLat()); assertEquals("awesome", geoLocation.getLocality()); @@ -150,7 +151,9 @@ public class GeoLocationProviderTest extends SmackTestSuite { assertEquals("IN", geoLocation.getCountryCode()); assertEquals("Test Datum", geoLocation.getDatum()); assertEquals("My Description", geoLocation.getDescription()); - assertNull(geoLocation.getError()); + @SuppressWarnings("deprecation") + Double error = geoLocation.getError(); + assertEquals(90, error); assertEquals("top", geoLocation.getFloor()); assertEquals((Double) 25.098345d, geoLocation.getLat()); assertEquals("awesome", geoLocation.getLocality()); @@ -183,7 +186,9 @@ public class GeoLocationProviderTest extends SmackTestSuite { GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class); - assertEquals((Double) 90d, geoLocation.getError()); + @SuppressWarnings("deprecation") + Double error = geoLocation.getError(); + assertEquals((Double) 90d, error); } @Test @@ -223,8 +228,9 @@ public class GeoLocationProviderTest extends SmackTestSuite { GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class); assertEquals((Double) 90d, geoLocation.getAccuracy()); - assertNull(geoLocation.getError()); - + @SuppressWarnings("deprecation") + Double error = geoLocation.getError(); + assertEquals(100, error); } }