Commit 25704dea authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Extend and refactor domain model.

parent 40403dcf
......@@ -21,7 +21,7 @@ import tdt4140.gr1800.app.json.GeoLocationsJsonPersistence;
public class App {
private GeoLocationsPersistence geoLocationsPersistence = new GeoLocationsJsonPersistence();
private GeoLocationsStreamPersistence geoLocationsPersistence = new GeoLocationsJsonPersistence();
private Collection<GeoLocations> geoLocations = null;
......
......@@ -35,26 +35,4 @@ public class GeoLocation extends TimedTaggedImpl implements GeoLocated, Timed, T
public void setDescription(String description) {
this.description = description;
}
//
private Tags tags = null;
@Override
public boolean hasTags(String... tags) {
return this.tags != null && this.tags.hasTags(tags);
}
public void addTags(String... tags) {
if (this.tags == null) {
this.tags = new Tags();
}
this.tags.addTags(tags);
}
public void removeTags(String... tags) {
if (this.tags != null) {
this.tags.removeTags(tags);
}
}
}
......@@ -9,6 +9,16 @@ import java.util.stream.Collectors;
public class GeoLocations extends TimedTaggedImpl implements Iterable<GeoLocated>, Tagged, Timed {
private final GeoLocationsOwner owner;
public GeoLocations(GeoLocationsOwner owner) {
this.owner = owner;
}
public GeoLocationsOwner getOwner() {
return owner;
}
private String name;
public String getName() {
......@@ -19,12 +29,23 @@ public class GeoLocations extends TimedTaggedImpl implements Iterable<GeoLocated
this.name = name;
}
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
private Collection<GeoLocated> locations = new ArrayList<GeoLocated>();
private boolean path = false;
public GeoLocations(LatLong...latLongs) {
for (int i = 0; i < latLongs.length; i++) {
addLocation(latLongs[i]);
public GeoLocations(GeoLocated...geoLocs) {
this((GeoLocationsOwner) null);
for (int i = 0; i < geoLocs.length; i++) {
addLocation(geoLocs[i]);
}
}
......@@ -60,7 +81,7 @@ public class GeoLocations extends TimedTaggedImpl implements Iterable<GeoLocated
//
public void addLocation(LatLong geoLoc) {
public void addLocation(GeoLocated geoLoc) {
locations.add(geoLoc);
}
......
package tdt4140.gr1800.app.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GeoLocationsOwner {
private Collection<GeoLocations> geoLocations = null;
public Iterable<String> getGeoLocationsNames() {
Collection<String> names = new ArrayList<String>(geoLocations != null ? geoLocations.size() : 0);
if (geoLocations != null) {
for (GeoLocations geoLocations : geoLocations) {
names.add(geoLocations.getName());
}
}
return names;
}
public boolean hasGeoLocations(String... names) {
if (geoLocations != null) {
outer: for (String name : names) {
for (GeoLocations geoLocations : geoLocations) {
if (name.equals(geoLocations.getName())) {
continue outer;
}
}
return false;
}
return true;
}
return names.length == 0;
}
public GeoLocations getGeoLocations(String name) {
if (geoLocations != null) {
for (GeoLocations geoLocations : geoLocations) {
if (name.equals(geoLocations.getName())) {
return geoLocations;
}
}
}
return null;
}
public Collection<GeoLocations> getGeoLocations(String... names) {
Collection<GeoLocations> result = new ArrayList<GeoLocations>();
if (geoLocations != null) {
if (names != null) {
for (GeoLocations geoLocations : geoLocations) {
for (String name : names) {
if (name.equals(geoLocations.getName())) {
result.add(geoLocations);
}
}
}
} else {
result.addAll(geoLocations);
}
}
return result;
}
//
public void removeGeolocations(String... names) {
if (geoLocations != null) {
Iterator<GeoLocations> it = geoLocations.iterator();
while (it.hasNext()) {
GeoLocations next = it.next();
if (names != null) {
for (String name : names) {
if (name.equals(next.getName())) {
it.remove();
break;
}
}
} else {
it.remove();
}
}
}
}
public void addGeolocations(GeoLocations geoLocations) {
if (this.geoLocations == null) {
this.geoLocations = new ArrayList<>();
}
if (! this.geoLocations.contains(geoLocations)) {
this.geoLocations.add(geoLocations);
}
}
public void removeGeolocations(GeoLocations geoLocations) {
if (this.geoLocations != null) {
this.geoLocations.remove(geoLocations);
}
}
}
......@@ -4,7 +4,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
public interface GeoLocationsPersistence {
public interface GeoLocationsStreamPersistence {
public Collection<GeoLocations> loadLocations(InputStream inputStream) throws Exception;
public void saveLocations(Collection<GeoLocations> geoLocations, OutputStream outputStream) throws Exception;
}
......@@ -2,6 +2,9 @@ package tdt4140.gr1800.app.core;
public interface Tagged {
public boolean hasTags(String... tags);
public String[] getTags();
public String getTags(String prefix, String separator, String suffix);
public void setTags(String... tags);
public void addTags(String... tags);
public void removeTags(String... tags);
}
......@@ -9,18 +9,66 @@ public class Tags implements Tagged {
private Collection<String> tags = null;
public Tags(String... tags) {
addTags(tags);
setTags(tags);
}
public Tags(Tagged tags) {
setTags(tags.getTags());
}
public static Tags valueOf(String tags) {
return valueOf(tags, ",");
}
public static Tags valueOf(String tags, String separator) {
return new Tags(tags.split(separator));
}
public int getTagCount() {
return (tags == null ? 0 : tags.size());
}
@Override
public boolean hasTags(String... tags) {
return this.tags != null && this.tags.containsAll(Arrays.asList(tags));
return (tags.length == 0 || (this.tags != null && this.tags.containsAll(Arrays.asList(tags))));
}
final static String[] EMPTY_STRINGS = {};
@Override
public String[] getTags() {
return (tags != null ? tags.toArray(new String[tags.size()]) : EMPTY_STRINGS);
}
@Override
public String getTags(String prefix, String separator, String suffix) {
StringBuilder buffer = new StringBuilder();
append(buffer, prefix);
int tagNum = 0;
for (String tag : tags) {
if (tagNum > 0 && separator != null) {
buffer.append(separator);
}
buffer.append(tag);
tagNum++;
}
append(buffer, suffix);
return buffer.toString();
}
static StringBuilder append(StringBuilder buffer, String s) {
if (s != null) {
buffer.append(s);
}
return buffer;
}
@Override
public void setTags(String... tags) {
this.tags = new ArrayList<>();
addTags(tags);
}
public void addTags(String... tags) {
if (this.tags == null && tags != null && tags.length > 0) {
this.tags = new ArrayList<>();
......
......@@ -4,11 +4,47 @@ public class TimedTaggedImpl extends TimedImpl implements Tagged {
private Tags tags = null;
public TimedTaggedImpl() {
}
public TimedTaggedImpl(Tagged tags) {
setTags(tags.getTags());
}
private static TimedTaggedImpl valueOf(Tags tags) {
TimedTaggedImpl timedTags = new TimedTaggedImpl();
timedTags.tags = tags;
return timedTags;
}
public static TimedTaggedImpl valueOf(String tags) {
return valueOf(Tags.valueOf(tags));
}
public static TimedTaggedImpl valueOf(String tags, String separator) {
return valueOf(Tags.valueOf(tags, separator));
}
@Override
public boolean hasTags(String... tags) {
return this.tags != null && this.tags.hasTags(tags);
}
@Override
public String[] getTags() {
return (tags != null ? tags.getTags() : Tags.EMPTY_STRINGS);
}
@Override
public String getTags(String prefix, String separator, String suffix) {
return (tags != null ? tags.getTags(prefix, separator, suffix) : Tags.append(Tags.append(new StringBuilder(), prefix), suffix).toString());
}
@Override
public void setTags(String... tags) {
this.tags = (tags != null && tags.length > 0 ? new Tags(tags) : null);
}
public void addTags(String... tags) {
if (this.tags == null) {
this.tags = new Tags();
......
package tdt4140.gr1800.app.geojson;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import org.geojson.GeoJsonObject;
import org.geojson.LngLatAlt;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
import tdt4140.gr1800.app.doc.IDocumentLoader;
public class GeoJsonDocumentConverter implements IDocumentLoader<Collection<GeoLocations>> {
private GeoJsonDocumentLoader geoJsonLoader = new GeoJsonDocumentLoader();
@Override
public Collection<GeoLocations> loadDocument(InputStream inputStream) throws Exception {
GeoJsonObject geoJson = geoJsonLoader.loadDocument(inputStream);
return convert(geoJson);
}
private String trackNameFormat = "%s";
private String trackCountFormat = "Track %s";
private String trackSegmentFormat = "%s.%s";
public void setTrackNameFormat(String trackNameFormat) {
this.trackNameFormat = trackNameFormat;
}
public void setTrackCountFormat(String trackCountFormat) {
this.trackCountFormat = trackCountFormat;
}
public void setTrackSegmentFormat(String trackSegmentFormat) {
this.trackSegmentFormat = trackSegmentFormat;
}
private String routeNameFormat = "%s";
private String routeCountFormat = "Route %s";
public void setRouteNameFormat(String routeNameFormat) {
this.routeNameFormat = routeNameFormat;
}
public void setRouteCountFormat(String routeCountFormat) {
this.routeCountFormat = routeCountFormat;
}
public Collection<GeoLocations> convert(GeoJsonObject geoJson) throws Exception {
Collection<GeoLocations> geoLocations = new ArrayList<GeoLocations>();
return geoLocations;
}
private LatLong[] convert(Collection<LngLatAlt> points) {
Collection<LatLong> latLongs = points.stream()
.map(point -> convert(point))
.collect(Collectors.toList());
return latLongs.toArray(new LatLong[latLongs.size()]);
}
private LatLong convert(LngLatAlt point) {
return new LatLong(point.getLatitude(), point.getLongitude());
}
}
package tdt4140.gr1800.app.geojson;
import java.io.InputStream;
import org.geojson.GeoJsonObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import tdt4140.gr1800.app.doc.IDocumentLoader;
public class GeoJsonDocumentLoader implements IDocumentLoader<GeoJsonObject> {
@Override
public GeoJsonObject loadDocument(InputStream inputStream) throws Exception {
return new ObjectMapper().readValue(inputStream, GeoJsonObject.class);
}
}
......@@ -9,9 +9,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.GeoLocationsPersistence;
import tdt4140.gr1800.app.core.GeoLocationsStreamPersistence;
public class GeoLocationsJsonPersistence implements GeoLocationsPersistence {
public class GeoLocationsJsonPersistence implements GeoLocationsStreamPersistence {
private final ObjectMapper objectMapper;
......
......@@ -35,7 +35,7 @@ public class AppTest {
Assert.fail("Couldn't open " + file);
}
Assert.assertEquals(file, documentStorage.getDocumentLocation());
GeoLocationsPersistenceTest.testGeoLocationsDotJson(app.getGeoLocations((String[]) null));
GeoLocationsStreamPersistenceTest.testGeoLocationsDotJson(app.getGeoLocations((String[]) null));
}
@Test
......
package tdt4140.gr1800.app.core;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class GeoLocationsOwnerTest {
private GeoLocationsOwner owner;
@Before
public void setUp() {
this.owner = new GeoLocationsOwner();
}
private static <T> void check(Iterable<T> iterable, T... ts) {
check(iterable, false, ts);
}
private static <T> void check(Iterable<T> iterable, boolean anyOrder, T... ts) {
Collection<T> all = (anyOrder ? Arrays.asList(ts) : null);
int num = 0;
for (Iterator<T> it = iterable.iterator(); it.hasNext(); num++) {
Assert.assertTrue(num < ts.length);
T next = it.next();
if (anyOrder) {
assertTrue(all.contains(next));
} else {
Assert.assertEquals(ts[num], next);
}
}
Assert.assertTrue(num == ts.length);
}
private static int size(Iterable<?> iterable) {
int size = 0;
for (Iterator<?> it = iterable.iterator(); it.hasNext(); it.next()) {
size++;
}
return size;
}
@Test
public void testGetGeoLocationNames() {
Assert.assertEquals(0, size(owner.getGeoLocationsNames()));
owner.addGeolocations(new GeoLocations("test1"));
check(owner.getGeoLocationsNames(), "test1");
owner.addGeolocations(new GeoLocations("test2"));
check(owner.getGeoLocationsNames(), "test1", "test2");
}
@Test
public void testHasGeoLocations() {
Assert.assertTrue(owner.hasGeoLocations());
Assert.assertFalse(owner.hasGeoLocations("test1"));
owner.addGeolocations(new GeoLocations("test1"));
Assert.assertTrue(owner.hasGeoLocations());
Assert.assertTrue(owner.hasGeoLocations("test1"));
owner.addGeolocations(new GeoLocations("test2"));
Assert.assertTrue(owner.hasGeoLocations());
Assert.assertTrue(owner.hasGeoLocations("test1"));
Assert.assertTrue(owner.hasGeoLocations("test2"));
Assert.assertTrue(owner.hasGeoLocations("test1", "test2"));
}
@Test
public void testGetGeoLocations1() {
Assert.assertNull(owner.getGeoLocations("test1"));
GeoLocations geoLocations11 = new GeoLocations("test1");
owner.addGeolocations(geoLocations11);
Assert.assertSame(geoLocations11, owner.getGeoLocations("test1"));
GeoLocations geoLocations2 = new GeoLocations("test2");
owner.addGeolocations(geoLocations2);
Assert.assertSame(geoLocations11, owner.getGeoLocations("test1"));
Assert.assertSame(geoLocations2, owner.getGeoLocations("test2"));
GeoLocations geoLocations12 = new GeoLocations("test1");
owner.addGeolocations(geoLocations12);
Assert.assertNotNull(owner.getGeoLocations("test1"));
Assert.assertSame(geoLocations2, owner.getGeoLocations("test2"));
}
@Test
public void testGetGeoLocationsN() {
GeoLocations geoLocations11 = new GeoLocations("test1");
owner.addGeolocations(geoLocations11);
Assert.assertEquals(0, owner.getGeoLocations().size());
check(owner.getGeoLocations(new String[]{"test1"}), geoLocations11);
check(owner.getGeoLocations((String[]) null), geoLocations11);
GeoLocations geoLocations2 = new GeoLocations("test2");
owner.addGeolocations(geoLocations2);
Assert.assertEquals(0, owner.getGeoLocations().size());
check(owner.getGeoLocations(new String[]{"test1"}), geoLocations11);
check(owner.getGeoLocations(new String[]{"test2"}), geoLocations2);
check(owner.getGeoLocations((String[]) null), true, geoLocations11, geoLocations2);
GeoLocations geoLocations12 = new GeoLocations("test1");
owner.addGeolocations(geoLocations12);
Assert.assertEquals(0, owner.getGeoLocations().size());
check(owner.getGeoLocations(new String[]{"test1"}), true, geoLocations11, geoLocations12);
Assert.assertEquals(1, owner.getGeoLocations(new String[]{"test2"}).size());
check(owner.getGeoLocations((String[]) null), true, geoLocations11, geoLocations2, geoLocations12);
}
@Test
public void testRemoveGeoLocationsN() {
GeoLocations geoLocations11 = new GeoLocations("test1"), geoLocations2 = new GeoLocations("test2"), geoLocations12 = new GeoLocations("test1");
owner.addGeolocations(geoLocations11);
owner.addGeolocations(geoLocations2);
owner.addGeolocations(geoLocations12);
Assert.assertEquals(3, owner.getGeoLocations((String[]) null).size());
owner.removeGeolocations("test1");
check(owner.getGeoLocations((String[]) null