Commit 40403dcf authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Preliminary work a richer (than LatLong) GeoLocation class

parent 783fb4fe
package tdt4140.gr1800.app.core;
public class GeoLocation extends TimedTaggedImpl implements GeoLocated, Timed, Tagged {
private LatLong latLong;
public LatLong getLatLong() {
return latLong;
}
public void setLatLong(LatLong latLong) {
this.latLong = latLong;
}
private int elevation;
public int getElevation() {
return elevation;
}
private String name, description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
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);
}
}
}
package tdt4140.gr1800.app.core;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class GeoLocations implements Iterable<GeoLocated>, Tagged {
public class GeoLocations extends TimedTaggedImpl implements Iterable<GeoLocated>, Tagged, Timed {
private String name;
......@@ -21,7 +18,7 @@ public class GeoLocations implements Iterable<GeoLocated>, Tagged {
public void setName(String name) {
this.name = name;
}
private Collection<GeoLocated> locations = new ArrayList<GeoLocated>();
private boolean path = false;
......@@ -35,7 +32,7 @@ public class GeoLocations implements Iterable<GeoLocated>, Tagged {
this(latLongs);
setName(name);
}
public boolean isPath() {
return path;
}
......@@ -85,26 +82,4 @@ public class GeoLocations implements Iterable<GeoLocated>, Tagged {
public Iterator<GeoLocated> iterator() {
return locations.iterator();
}
//
private Set<String> tags = null;
@Override
public boolean hasTags(String... tags) {
return this.tags != null && this.tags.containsAll(Arrays.asList(tags));
}
public void addTags(String... tags) {
if (this.tags == null) {
this.tags = new HashSet<>();
}
this.tags.addAll(Arrays.asList(tags));
}
public void removeTags(String... tags) {
if (this.tags != null) {
this.tags.removeAll(Arrays.asList(tags));
}
}
}
......@@ -2,4 +2,6 @@ package tdt4140.gr1800.app.core;
public interface Tagged {
public boolean hasTags(String... tags);
public void addTags(String... tags);
public void removeTags(String... tags);
}
package tdt4140.gr1800.app.core;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class Tags implements Tagged {
private Collection<String> tags = null;
public Tags(String... tags) {
addTags(tags);
}
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));
}
public void addTags(String... tags) {
if (this.tags == null && tags != null && tags.length > 0) {
this.tags = new ArrayList<>();
}
for (int i = 0; i < tags.length; i++) {
if (! this.tags.contains(tags[i])) {
this.tags.add(tags[i]);
}
}
}
public void removeTags(String... tags) {
if (this.tags != null) {
this.tags.removeAll(Arrays.asList(tags));
}
}
}
package tdt4140.gr1800.app.core;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
public interface Timed {
public ZoneId getZone();
public void setZone(ZoneId zone);
default public void setZone(String zone) {
setZone(ZoneId.of(zone));
}
public LocalDate getDate();
public void setDate(LocalDate date);
default public void setDate(String date) {
setDate(LocalDate.parse(date));
}
public LocalTime getTime();
public void setTime(LocalTime time);
default public void setTime(String time) {
setTime(LocalTime.parse(time));
}
default public void setDateTime(LocalDateTime dateTime) {
setDate(dateTime.toLocalDate());
setTime(dateTime.toLocalTime());
}
}
package tdt4140.gr1800.app.core;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
public class TimedImpl implements Timed {
private ZoneId zone;
public ZoneId getZone() {
return zone;
}
public void setZone(ZoneId zone) {
this.zone = zone;
}
private LocalDate date;
private LocalTime time;
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public LocalTime getTime() {
return time;
}
public void setTime(LocalTime time) {
this.time = time;
}
}
package tdt4140.gr1800.app.core;
public class TimedTaggedImpl extends TimedImpl implements Tagged {
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);
if (this.tags.getTagCount() == 0) {
this.tags = null;
}
}
}
}
package tdt4140.gr1800.app.core;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class GeoLocationTest extends TimedTaggedTest {
private GeoLocation geoLocation;
@Before
public void setUp() {
setUp(geoLocation = new GeoLocation());
}
@Test
public void testSetLatLong() {
LatLong latLong = new LatLong(63, 10);
geoLocation.setLatLong(latLong);
Assert.assertEquals(latLong, geoLocation.getLatLong());
}
}
......@@ -7,13 +7,13 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class GeoLocationsTest {
public class GeoLocationsTest extends TimedTaggedTest {
private GeoLocations geoLocations;
@Before
public void setUp() {
geoLocations = new GeoLocations();
setUp(geoLocations = new GeoLocations());
}
@Test
......
package tdt4140.gr1800.app.core;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TimedTaggedTest {
private TimedTaggedImpl timedTagged;
protected void setUp(TimedTaggedImpl timedTagged) {
this.timedTagged = timedTagged;
}
@Before
public void setUp() {
setUp(new TimedTaggedImpl());
}
@Test
public void testTaggedSetTime() {
LocalTime now = LocalTime.now();
timedTagged.setTime(now);
Assert.assertEquals(now, timedTagged.getTime());
}
@Test
public void testTaggedSetDate() {
LocalDate now = LocalDate.now();
timedTagged.setDate(now);
Assert.assertEquals(now, timedTagged.getDate());
}
@Test
public void testTaggedSetDateTime() {
LocalDateTime now = LocalDateTime.now();
timedTagged.setDateTime(now);
Assert.assertEquals(now.toLocalDate(), timedTagged.getDate());
Assert.assertEquals(now.toLocalTime(), timedTagged.getTime());
}
@Test
public void testTaggedAdd() {
timedTagged.addTags("en", "to");
Assert.assertTrue(timedTagged.hasTags("en"));
Assert.assertTrue(timedTagged.hasTags("to"));
Assert.assertTrue(timedTagged.hasTags("en", "to"));
timedTagged.addTags("tre");
Assert.assertTrue(timedTagged.hasTags("en", "to"));
Assert.assertTrue(timedTagged.hasTags("tre"));
}
@Test
public void testTaggedRemove() {
timedTagged.addTags("en", "en", "to");
timedTagged.removeTags("en");
Assert.assertFalse(timedTagged.hasTags("en"));
Assert.assertTrue(timedTagged.hasTags("to"));
timedTagged.removeTags("to");
Assert.assertFalse(timedTagged.hasTags("to"));
timedTagged.removeTags("tre");
Assert.assertFalse(timedTagged.hasTags("tre"));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment