Skip to content
Snippets Groups Projects
Commit 764152f1 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Fikset en del av det som ble rapportert av PMD og SpotBugs

parent 7ccfe11b
No related branches found
No related tags found
No related merge requests found
Pipeline #46503 passed
Showing
with 75 additions and 67 deletions
......@@ -20,7 +20,7 @@ import java.util.Collection;
*/
public abstract class AbstractDocumentStorageImpl<D, L> implements IDocumentStorage<L>, IDocumentPersistence<D, L> {
private L documentLocation;
private L documentLocation = null;
@Override
public L getDocumentLocation() {
......
......@@ -186,18 +186,15 @@ public class FileMenuController {
inputDialog.setHeaderText("Enter URL to import from");
inputDialog.setContentText("Enter URL: ");
// https://developer.garmin.com/downloads/connect-api/sample_file.gpx
URL url = null;
while (url == null) {
while (true) {
final Optional<String> result = inputDialog.showAndWait();
if (! result.isPresent()) {
return;
break;
}
try {
url = new URL(result.get());
if (handleURLImportAction(url)) {
return;
if (handleURLImportAction(new URL(result.get()))) {
break;
}
url = null;
inputDialog.setHeaderText("Problems reading it...");
inputDialog.setContentText("Enter another URL: ");
} catch (final MalformedURLException e1) {
......
......@@ -3,6 +3,7 @@ package fxutil.doc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
......@@ -34,7 +35,9 @@ public abstract class SimpleJsonFileStorageImpl<T> extends AbstractDocumentStora
@Override
public void saveDocument(final T document, final File documentLocation) throws Exception {
objectMapper.writeValue(new FileOutputStream(documentLocation, false), document);
try (OutputStream outputStream = new FileOutputStream(documentLocation, false)) {
objectMapper.writeValue(outputStream, document);
}
}
@Override
......
......@@ -2,7 +2,7 @@ package simpleex.core;
public class LatLong {
public final double latitude, longitude;
private final double latitude, longitude;
public LatLong(final double latitude, final double longitude) {
super();
......@@ -12,6 +12,14 @@ public class LatLong {
public final static String SEPARATOR = ",";
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
@Override
public String toString() {
return latitude + SEPARATOR + longitude;
......@@ -20,10 +28,8 @@ public class LatLong {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(latitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
long temp = Double.doubleToLongBits(latitude);
int result = prime + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
......
......@@ -47,17 +47,17 @@ public class LatLongs implements Iterable<LatLong> {
return pos;
}
public int addLatLongs(final Collection<LatLong> latLongs) {
public final int addLatLongs(final Collection<LatLong> latLongs) {
final int pos = this.latLongs.size();
this.latLongs.addAll(latLongs);
return pos;
}
public int addLatLongs(final LatLong... latLongs) {
public final int addLatLongs(final LatLong... latLongs) {
return addLatLongs(List.of(latLongs));
}
public int addLatLongs(final double... latLongsArray) {
public final int addLatLongs(final double... latLongsArray) {
final Collection<LatLong> latLongs = new ArrayList<>(latLongsArray.length / 2);
for (int i = 0; i < latLongsArray.length; i += 2) {
latLongs.add(new LatLong(latLongsArray[i], latLongsArray[i + 1]));
......
......@@ -14,6 +14,8 @@ import simpleex.core.LatLong;
public class LatLongDeserializer extends JsonDeserializer<LatLong> {
private static final int ARRAY_JSON_NODE_SIZE = 2;
@Override
public LatLong deserialize(final JsonParser jsonParser, final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
......@@ -28,7 +30,7 @@ public class LatLongDeserializer extends JsonDeserializer<LatLong> {
return new LatLong(latitude, longitude);
} else if (jsonNode instanceof ArrayNode) {
final ArrayNode locationArray = (ArrayNode) jsonNode;
if (locationArray.size() == 2) {
if (locationArray.size() == ARRAY_JSON_NODE_SIZE) {
final double latitude = locationArray.get(0).asDouble();
final double longitude = locationArray.get(1).asDouble();
return new LatLong(latitude, longitude);
......
......@@ -17,9 +17,9 @@ public class LatLongSerializer extends JsonSerializer<LatLong> {
public void serialize(final LatLong latLon, final JsonGenerator jsonGen, final SerializerProvider provider) throws IOException {
jsonGen.writeStartObject();
jsonGen.writeFieldName(LATITUDE_FIELD_NAME);
jsonGen.writeNumber(latLon.latitude);
jsonGen.writeNumber(latLon.getLatitude());
jsonGen.writeFieldName(LONGITUDE_FIELD_NAME);
jsonGen.writeNumber(latLon.longitude);
jsonGen.writeNumber(latLon.getLongitude());
jsonGen.writeEndObject();
}
}
package simpleex.ui;
import java.util.Optional;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Node;
......@@ -10,13 +12,13 @@ public class DraggableNodeController {
public DraggableNodeController() {
}
public DraggableNodeController(final NodeDraggedHandler nodeDraggedHandler) {
public DraggableNodeController(final Optional<NodeDraggedHandler> nodeDraggedHandler) {
setNodeDraggedHandler(nodeDraggedHandler);
}
private NodeDraggedHandler nodeDraggedHandler;
private Optional<NodeDraggedHandler> nodeDraggedHandler = Optional.empty();
public void setNodeDraggedHandler(final NodeDraggedHandler nodeDraggedHandler) {
public final void setNodeDraggedHandler(final Optional<NodeDraggedHandler> nodeDraggedHandler) {
this.nodeDraggedHandler = nodeDraggedHandler;
}
......@@ -26,7 +28,7 @@ public class DraggableNodeController {
this.immediate = immediate;
}
private Node currentNode = null;
private Optional<Node> currentNode = Optional.empty();
private Point2D startPoint = null;
private Point2D startTranslate = null;
......@@ -47,16 +49,16 @@ public class DraggableNodeController {
}
private void mousePressed(final MouseEvent mouseEvent) {
if (currentNode == null && mouseEvent.getSource() instanceof Node) {
currentNode = (Node) mouseEvent.getSource();
if (currentNode.isEmpty() && mouseEvent.getSource() instanceof Node) {
currentNode = Optional.of((Node) mouseEvent.getSource());
startPoint = new Point2D(mouseEvent.getSceneX(), mouseEvent.getSceneY());
startTranslate = new Point2D(currentNode.getTranslateX(), currentNode.getTranslateY());
startTranslate = new Point2D(currentNode.get().getTranslateX(), currentNode.get().getTranslateY());
mouseEvent.consume();
}
}
private void mouseDragged(final MouseEvent mouseEvent) {
if (currentNode != null && currentNode == mouseEvent.getSource()) {
if (currentNode.isPresent() && currentNode == mouseEvent.getSource()) {
final double dx = mouseEvent.getSceneX() - startPoint.getX();
final double dy = mouseEvent.getSceneY() - startPoint.getY();
updateNode(dx, dy);
......@@ -64,27 +66,27 @@ public class DraggableNodeController {
}
protected void updateNode(final double dx, final double dy) {
if (immediate && nodeDraggedHandler != null) {
nodeDraggedHandler.nodeDragged(currentNode, dx, dy);
if (immediate && nodeDraggedHandler.isPresent()) {
nodeDraggedHandler.get().nodeDragged(currentNode.get(), dx, dy);
startPoint = startPoint.add(dx, dy);
} else {
currentNode.setTranslateX(startTranslate.getX() + dx);
currentNode.setTranslateY(startTranslate.getY() + dy);
} else if (currentNode.isPresent()) {
currentNode.get().setTranslateX(startTranslate.getX() + dx);
currentNode.get().setTranslateY(startTranslate.getY() + dy);
}
}
private void mouseReleased(final MouseEvent mouseEvent) {
if (currentNode != null && currentNode == mouseEvent.getSource()) {
if (currentNode.isPresent() && currentNode.get() == mouseEvent.getSource()) {
final double dx = mouseEvent.getSceneX() - startPoint.getX();
final double dy = mouseEvent.getSceneY() - startPoint.getY();
if (! immediate) {
currentNode.setTranslateX(startTranslate.getX());
currentNode.setTranslateY(startTranslate.getY());
currentNode.get().setTranslateX(startTranslate.getX());
currentNode.get().setTranslateY(startTranslate.getY());
}
final Node node = currentNode;
currentNode = null;
if (nodeDraggedHandler != null) {
nodeDraggedHandler.nodeDragged(node, dx, dy);
final Node node = currentNode.get();
currentNode = Optional.empty();
if (nodeDraggedHandler.isPresent()) {
nodeDraggedHandler.get().nodeDragged(node, dx, dy);
}
}
}
......
package simpleex.ui;
import java.io.File;
import java.util.Optional;
import fxmapcontrol.Location;
import fxmapcontrol.MapBase;
......@@ -47,7 +48,7 @@ public class FxAppController extends FileMenuController implements IDocumentList
private MapBase mapView;
private MapItemsControl<MapNode> markersParent;
private MapMarker marker = null;
private Optional<MapMarker> marker = Optional.empty();
private DraggableNodeController draggableMapController = null;
private DraggableNodeController draggableMarkerController = null;
......@@ -64,10 +65,10 @@ public class FxAppController extends FileMenuController implements IDocumentList
zoomSlider.setValue(8);
markersParent = new MapItemsControl<MapNode>();
mapView.getChildren().add(markersParent);
draggableMapController = new DraggableNodeController(this::handleMapDragged);
draggableMapController = new DraggableNodeController(Optional.of(this::handleMapDragged));
draggableMapController.setImmediate(true);
draggableMapController.attach(mapView);
draggableMarkerController = new DraggableNodeController(this::handleMarkerDragged);
draggableMarkerController = new DraggableNodeController(Optional.of(this::handleMarkerDragged));
// the location list
locationListView.getSelectionModel().selectedIndexProperty().addListener((prop, oldValue, newValue) -> updateMapMarker(true));
}
......@@ -81,7 +82,7 @@ public class FxAppController extends FileMenuController implements IDocumentList
private void handleMarkerDragged(final Node node, final double dx, final double dy) {
final MapProjection projection = mapView.getProjection();
final Point2D point = projection.locationToViewportPoint(marker.getLocation());
final Point2D point = projection.locationToViewportPoint(marker.get().getLocation());
final Location newLocation = projection.viewportPointToLocation(point.add(dx, dy));
getLatLongs().setLatLong(locationListView.getSelectionModel().getSelectedIndex(), location2LatLong(newLocation));
updateLocationViewListSelection(false);
......@@ -96,22 +97,23 @@ public class FxAppController extends FileMenuController implements IDocumentList
if (num < 0 || num >= getLatLongs().getLatLongCount()) {
markersParent.getItems().clear();
if (draggableMarkerController != null) {
draggableMarkerController.detach(marker);
draggableMarkerController.detach(marker.get());
}
marker = null;
marker = Optional.empty();
} else {
final LatLong latLong = getLatLongs().getLatLong(num);
if (marker == null) {
marker = new MapMarker(latLong);
markersParent.getItems().add(marker);
if (marker.isEmpty()) {
final MapMarker aMarker = new MapMarker(latLong);
markersParent.getItems().add(aMarker);
if (draggableMarkerController != null) {
draggableMarkerController.attach(marker);
draggableMarkerController.attach(aMarker);
}
marker = Optional.of(aMarker);
} else {
marker.setLocation(latLong);
marker.get().setLocation(latLong);
}
if (centerOnMarker) {
mapView.setCenter(marker.getLocation());
mapView.setCenter(marker.get().getLocation());
}
}
}
......
......@@ -16,7 +16,7 @@ public class MapMarker extends MapItem<LatLong> {
getChildren().add(circle);
}
public void setLocation(final LatLong latLong) {
setLocation(new Location(latLong.latitude, latLong.longitude));
public final void setLocation(final LatLong latLong) {
setLocation(new Location(latLong.getLatitude(), latLong.getLongitude()));
}
}
......@@ -19,8 +19,8 @@ public class LatLongTest {
}
private void testLatLong(final LatLong latLong, final double lat, final double lon) {
Assert.assertEquals(lat, latLong.latitude, 0.0);
Assert.assertEquals(lon, latLong.longitude, 0.0);
Assert.assertEquals(lat, latLong.getLatitude(), 0.0);
Assert.assertEquals(lon, latLong.getLongitude(), 0.0);
}
@Test
......
......@@ -4,14 +4,12 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import simpleex.core.LatLongs;
public class LatLongsTest {
private LatLongs latLongs;
@Before
public void setup() {
public void setUp() {
latLongs = new LatLongs();
}
......
......@@ -19,7 +19,7 @@ public abstract class AbstractJsonTest {
return objectMapper;
}
public void setup() {
protected void setUp() {
objectMapper = createObjectMapper();
}
......
......@@ -7,15 +7,13 @@ import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import simpleex.core.LatLong;
import simpleex.json.LatLongDeserializer;
import simpleex.json.LatLongSerializer;
public class LatLongJsonTest extends AbstractJsonTest {
@Before
@Override
public void setup() {
super.setup();
public void setUp() {
super.setUp();
}
@Override
......
......@@ -72,7 +72,7 @@ public class FxAppTest extends ApplicationTest {
@Test
public void testController() {
Assert.assertTrue(this.controller instanceof FxAppController);
Assert.assertNotNull(this.controller);
}
@Test
......@@ -90,8 +90,8 @@ public class FxAppTest extends ApplicationTest {
// center of map view is approx. the first LatLong object
final Location center = mapView.getCenter();
final double epsilon = 0.000001; // round-off error
Assert.assertEquals(latLongList.get(0).latitude, center.getLatitude(), epsilon);
Assert.assertEquals(latLongList.get(0).longitude, center.getLongitude(), epsilon);
Assert.assertEquals(latLongList.get(0).getLatitude(), center.getLatitude(), epsilon);
Assert.assertEquals(latLongList.get(0).getLongitude(), center.getLongitude(), epsilon);
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment