Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97cb28c

Browse files
committed
checkstyle
1 parent 417cb4c commit 97cb28c

File tree

10 files changed

+747
-557
lines changed

10 files changed

+747
-557
lines changed

src/main/java/de/taimos/gpsd4java/backend/AbstractResultParser.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@
2020
* #L%
2121
*/
2222

23+
import java.text.DateFormat;
24+
import java.text.SimpleDateFormat;
2325
import java.util.ArrayList;
26+
import java.util.Date;
2427
import java.util.List;
28+
import java.util.TimeZone;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
2531

2632
import org.json.JSONArray;
2733
import org.json.JSONException;
@@ -32,10 +38,22 @@
3238

3339
/**
3440
*
35-
* @author irakli
41+
* @author irakli, thoeger
3642
*/
3743
public abstract class AbstractResultParser {
3844

45+
protected static final Logger LOG = Logger.getLogger(ResultParser.class.getName());
46+
47+
protected final DateFormat dateFormat; // Don't make this static!
48+
49+
/**
50+
* Create new ResultParser
51+
*/
52+
public AbstractResultParser() {
53+
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
54+
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
55+
}
56+
3957
/**
4058
* Parse a received line into a {@link IGPSObject}
4159
*
@@ -64,19 +82,38 @@ public IGPSObject parse(final String line) throws ParseException {
6482
/**
6583
* parse a whole JSONArray into a list of IGPSObjects
6684
*/
67-
@SuppressWarnings("unchecked")
68-
protected <T extends IGPSObject> List<T> parseObjectArray(final JSONArray array, final Class<T> componentType) throws ParseException {
85+
@SuppressWarnings({ "unchecked", "unused" })
86+
protected <T extends IGPSObject> List<T> parseObjectArray(final JSONArray array, final Class<T> type) throws ParseException {
6987
try {
7088
if (array == null) {
7189
return new ArrayList<T>(10);
7290
}
7391
final List<T> objects = new ArrayList<T>(10);
7492
for (int i = 0; i < array.length(); i++) {
75-
objects.add((T)this.parse(array.getJSONObject(i)));
93+
objects.add((T) this.parse(array.getJSONObject(i)));
7694
}
7795
return objects;
7896
} catch (final JSONException e) {
7997
throw new ParseException("Parsing failed", e);
8098
}
8199
}
100+
101+
protected double parseTimestamp(final JSONObject json, final String fieldName) {
102+
try {
103+
final String text = json.optString(fieldName, null);
104+
AbstractResultParser.LOG.log(Level.FINE, fieldName + ": {0}", text);
105+
106+
if (text != null) {
107+
final Date date = this.dateFormat.parse(text);
108+
if (AbstractResultParser.LOG.isLoggable(Level.FINE)) {
109+
final String ds = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(date);
110+
AbstractResultParser.LOG.log(Level.FINE, "Date: {0}", ds);
111+
}
112+
return date.getTime() / 1000.0;
113+
}
114+
} catch (final Exception ex) {
115+
AbstractResultParser.LOG.log(Level.INFO, "Failed to parse time", ex);
116+
}
117+
return Double.NaN;
118+
}
82119
}

src/main/java/de/taimos/gpsd4java/backend/GISTool.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
*
2828
* @author thoeger
2929
*/
30-
public class GISTool {
30+
public final class GISTool {
3131

3232
private static final int EARTH_RADIUS_KILOMETERS = 6371;
3333

34+
private GISTool() {
35+
//
36+
}
37+
3438
/**
3539
* calculates the distance between two {@link TPVObject} in kilometers<br>
3640
* the method used is the great-circle-distance with hypersine formula
@@ -59,19 +63,19 @@ public static double getDistance(final TPVObject tpv1, final TPVObject tpv2) {
5963
* - latitude of position 2
6064
* @return distance in kilometers
6165
*/
62-
public static double getDistance(double x1, double x2, double y1, double y2) {
66+
public static double getDistance(final double x1, final double x2, final double y1, final double y2) {
6367
// transform to radian
6468
final double deg2rad = Math.PI / 180;
6569

66-
x1 = x1 * deg2rad;
67-
x2 = x2 * deg2rad;
68-
y1 = y1 * deg2rad;
69-
y2 = y2 * deg2rad;
70+
final double x1rad = x1 * deg2rad;
71+
final double x2rad = x2 * deg2rad;
72+
final double y1rad = y1 * deg2rad;
73+
final double y2rad = y2 * deg2rad;
7074

7175
// great-circle-distance with hypersine formula
72-
final double dlong = x1 - x2;
73-
final double dlat = y1 - y2;
74-
final double a = Math.pow(Math.sin(dlat / 2), 2) + (Math.cos(y1) * Math.cos(y2) * Math.pow(Math.sin(dlong / 2), 2));
76+
final double dlong = x1rad - x2rad;
77+
final double dlat = y1rad - y2rad;
78+
final double a = Math.pow(Math.sin(dlat / 2), 2) + (Math.cos(y1rad) * Math.cos(y2rad) * Math.pow(Math.sin(dlong / 2), 2));
7579
final double c = 2 * Math.asin(Math.sqrt(a));
7680

7781
return GISTool.EARTH_RADIUS_KILOMETERS * c;

src/main/java/de/taimos/gpsd4java/backend/GPSdEndpoint.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
*/
5656
public class GPSdEndpoint {
5757

58-
private static final Logger log = Logger.getLogger(GPSdEndpoint.class.getName());
58+
private static final Logger LOG = Logger.getLogger(GPSdEndpoint.class.getName());
5959

6060
private final Socket socket;
6161

@@ -114,7 +114,7 @@ public void start() {
114114
try {
115115
Thread.sleep(500);
116116
} catch (final InterruptedException e) {
117-
GPSdEndpoint.log.log(Level.FINE, null, e);
117+
GPSdEndpoint.LOG.log(Level.FINE, null, e);
118118
}
119119
}
120120

@@ -125,7 +125,7 @@ public void stop() {
125125
try {
126126
this.listenThread.halt();
127127
} catch (final Exception e) {
128-
GPSdEndpoint.log.log(Level.FINE, null, e);
128+
GPSdEndpoint.LOG.log(Level.FINE, null, e);
129129
}
130130
this.listenThread = null;
131131
}
@@ -255,7 +255,7 @@ private IGPSObject waitForResult() {
255255
try {
256256
this.asyncWaitMutex.wait(1000);
257257
} catch (final InterruptedException e) {
258-
GPSdEndpoint.log.log(Level.INFO, null, e);
258+
GPSdEndpoint.LOG.log(Level.INFO, null, e);
259259
}
260260
if (this.asnycResult != null) {
261261
return this.asnycResult;

0 commit comments

Comments
 (0)