Commit 1f996e76 authored by Riegel's avatar Riegel
Browse files

Refactor: Remove downcasting with instanceof

parent 8e340a75
......@@ -6,11 +6,9 @@ import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
import de.hft.stuttgart.citydoctor2.utils.CopyHandler;
import de.hft.stuttgart.citydoctor2.utils.Copyable;
import org.citygml4j.core.model.transportation.AbstractTransportationSpace;
import org.citygml4j.core.model.transportation.Intersection;
import org.citygml4j.core.model.deprecated.transportation.TransportationComplex;
import org.citygml4j.core.model.transportation.Railway;
import org.citygml4j.core.model.transportation.Road;
import org.citygml4j.core.model.transportation.Section;
import org.citygml4j.core.model.transportation.Square;
import org.citygml4j.core.model.transportation.Track;
import org.citygml4j.core.model.transportation.Waterway;
......@@ -24,25 +22,39 @@ public class TopLevelTransportFeature extends TransportationSpace {
private final List<TransportSection> sections = new ArrayList<>();
private final List<TransportSection> intersections = new ArrayList<>();
public static TopLevelTransportFeature from(AbstractTransportationSpace abs) {
TransportationType type = null;
if (abs instanceof Track) {
type = TransportationType.TRACK;
} else if (abs instanceof Road) {
type = TransportationType.ROAD;
} else if (abs instanceof Waterway) {
type = TransportationType.WATERWAY;
} else if (abs instanceof Railway) {
type = TransportationType.RAILWAY;
} else if (abs instanceof Square) {
type = TransportationType.SQUARE;
} else if (abs instanceof Section || abs instanceof Intersection) {
return null;
} else {
throw new IllegalArgumentException("TransportationType: " + abs.getClass() + " is not a TopLevelTransportFeature");
}
TopLevelTransportFeature top = new TopLevelTransportFeature(type);
top.setGmlObject(abs);
public static TopLevelTransportFeature from(Track t) {
TopLevelTransportFeature top = new TopLevelTransportFeature(TransportationType.TRACK);
top.setGmlObject(t);
return top;
}
public static TopLevelTransportFeature from(Road r) {
TopLevelTransportFeature top = new TopLevelTransportFeature(TransportationType.ROAD);
top.setGmlObject(r);
return top;
}
public static TopLevelTransportFeature from(Waterway w) {
TopLevelTransportFeature top = new TopLevelTransportFeature(TransportationType.WATERWAY);
top.setGmlObject(w);
return top;
}
public static TopLevelTransportFeature from(Railway r) {
TopLevelTransportFeature top = new TopLevelTransportFeature(TransportationType.RAILWAY);
top.setGmlObject(r);
return top;
}
public static TopLevelTransportFeature from(Square s) {
TopLevelTransportFeature top = new TopLevelTransportFeature(TransportationType.SQUARE);
top.setGmlObject(s);
return top;
}
public static TopLevelTransportFeature from(TransportationComplex tc) {
TopLevelTransportFeature top = new TopLevelTransportFeature(TransportationType.TRANSPORTATION_COMPLEX);
top.setGmlObject(tc);
return top;
}
......
......@@ -44,6 +44,7 @@ import org.citygml4j.core.model.building.Storey;
import org.citygml4j.core.model.building.*;
import org.citygml4j.core.model.construction.AbstractConstruction;
import org.citygml4j.core.model.core.*;
import org.citygml4j.core.model.deprecated.transportation.TransportationComplex;
import org.citygml4j.core.model.landuse.LandUse;
import org.citygml4j.core.model.transportation.*;
import org.citygml4j.core.model.tunnel.Tunnel;
......@@ -656,16 +657,52 @@ public class Citygml3FeatureMapper extends ObjectWalker {
}
@Override
public void visit(AbstractTransportationSpace ats) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(ats);
if (top == null) {
return;
}
top.setGmlObject(ats);
mapAbstractTransportationSpace(ats, top);
model.addTransportation(top);
public void visit(Track track) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(track);
mapSectionsAndIntersections(track.getSections(), track.getIntersections(), top);
mapAbstractTransportationSpace(track, top);
finishTransportationMapping(top);
}
@Override
public void visit(Road road) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(road);
mapSectionsAndIntersections(road.getSections(), road.getIntersections(), top);
mapAbstractTransportationSpace(road, top);
finishTransportationMapping(top);
}
@Override
public void visit(Waterway waterway) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(waterway);
mapSectionsAndIntersections(waterway.getSections(), waterway.getIntersections(), top);
mapAbstractTransportationSpace(waterway, top);
finishTransportationMapping(top);
}
@Override
public void visit(Railway railway) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(railway);
mapSectionsAndIntersections(railway.getSections(), railway.getIntersections(), top);
mapAbstractTransportationSpace(railway, top);
finishTransportationMapping(top);
}
@Override
public void visit(Square square) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(square);
mapAbstractTransportationSpace(square, top);
finishTransportationMapping(top);
}
@Override
public void visit(TransportationComplex tc) {
TopLevelTransportFeature top = TopLevelTransportFeature.from(tc);
mapAbstractTransportationSpace(tc, top);
finishTransportationMapping(top);
}
private void mapAbstractThematicSurface(AbstractThematicSurface ats, CityObject co) {
mapAbstractSpaceBoundary(ats, co);
parseAndAddMultiSurface(ats.getLod0MultiSurface(), Lod.LOD0, co);
......@@ -709,9 +746,6 @@ public class Citygml3FeatureMapper extends ObjectWalker {
private void mapAbstractTransportationSpace(AbstractTransportationSpace ats, TransportationSpace trsp) {
parseAbstractTransportationSpaceGeometries(ats, trsp);
if (trsp instanceof TopLevelTransportFeature top) {
mapSectionsAndIntersections(ats, top);
}
for (TrafficSpaceProperty tsp : ats.getTrafficSpaces()) {
if (tsp.isSetObject()) {
TrafficSpaceObject tso = new TrafficSpaceObject(TrafficSpaceObject.TrafficSpaceType.TRAFFIC_SPACE);
......@@ -728,28 +762,8 @@ public class Citygml3FeatureMapper extends ObjectWalker {
}
}
private void mapSectionsAndIntersections(AbstractTransportationSpace ats, TopLevelTransportFeature top) {
List<SectionProperty> sectionProps;
List<IntersectionProperty> intersectionProps;
if (ats instanceof Track tr) {
sectionProps = tr.getSections();
intersectionProps = tr.getIntersections();
} else if (ats instanceof Road ro) {
sectionProps = ro.getSections();
intersectionProps = ro.getIntersections();
} else if (ats instanceof Waterway wa) {
sectionProps = wa.getSections();
intersectionProps = wa.getIntersections();
} else if (ats instanceof Railway rw) {
sectionProps = rw.getSections();
intersectionProps = rw.getIntersections();
} else if (ats instanceof Square || ats instanceof Section || ats instanceof Intersection) {
return;
} else {
logger.error("Mapping of Sections and Intersections failed, {} is not a TopLevelTransportFeature",
ats.getClass().getSimpleName());
return;
}
private void mapSectionsAndIntersections(List<SectionProperty> sectionProps, List<IntersectionProperty> intersectionProps,
TopLevelTransportFeature top) {
for (SectionProperty sectionProp : sectionProps) {
if (sectionProp.isSetObject()) {
TransportSection sect = new TransportSection(TransportSection.SectionType.SECTION);
......@@ -796,6 +810,7 @@ public class Citygml3FeatureMapper extends ObjectWalker {
tso.addTrafficArea(tao);
}
}
finishCityObjectConstruction(tso);
}
private void mapTrafficArea(TrafficArea ta, TrafficAreaObject tao) {
......
......@@ -75,9 +75,6 @@ public class TransportationObjectTest {
assertEquals(TransportationType.RAILWAY, railway.getTransportationType());
TopLevelTransportFeature square = TopLevelTransportFeature.from(Mockito.mock(Square.class));
assertEquals(TransportationType.SQUARE, square.getTransportationType());
assertNull(TopLevelTransportFeature.from(Mockito.mock(Section.class)));
assertNull(TopLevelTransportFeature.from(Mockito.mock(Intersection.class)));
}
@Test(expected = IllegalStateException.class)
......
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