Skip to content

Commit

Permalink
sequenced collections
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Oct 27, 2023
1 parent 9ac7ed4 commit 507ea4e
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void main(String[] args) throws Exception {
List<TileCoord> randomCoordsToFetchPerRepetition = new LinkedList<>();

do {
try (var db = Mbtiles.newReadOnlyDatabase(mbtilesPaths.get(0))) {
try (var db = Mbtiles.newReadOnlyDatabase(mbtilesPaths.getFirst())) {
try (var statement = db.connection().prepareStatement(SELECT_RANDOM_COORDS)) {
statement.setInt(1, nrTileReads - randomCoordsToFetchPerRepetition.size());
var rs = statement.executeQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static List<VectorTile.Feature> mergeGeometries(
List<VectorTile.Feature> result = new ArrayList<>(features.size());
var groupedByAttrs = groupByAttrs(features, result, geometryType);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
VectorTile.Feature feature1 = groupedFeatures.get(0);
VectorTile.Feature feature1 = groupedFeatures.getFirst();
if (groupedFeatures.size() == 1) {
result.add(feature1);
} else {
Expand Down Expand Up @@ -158,7 +158,7 @@ public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature>
List<VectorTile.Feature> result = new ArrayList<>(features.size());
var groupedByAttrs = groupByAttrs(features, result, GeometryType.LINE);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
VectorTile.Feature feature1 = groupedFeatures.get(0);
VectorTile.Feature feature1 = groupedFeatures.getFirst();
double lengthLimit = lengthLimitCalculator.apply(feature1.attrs());

// as a shortcut, can skip line merging only if:
Expand Down Expand Up @@ -300,7 +300,7 @@ public static List<VectorTile.Feature> mergeNearbyPolygons(List<VectorTile.Featu
Collection<List<VectorTile.Feature>> groupedByAttrs = groupByAttrs(features, result, GeometryType.POLYGON);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
List<Polygon> outPolygons = new ArrayList<>();
VectorTile.Feature feature1 = groupedFeatures.get(0);
VectorTile.Feature feature1 = groupedFeatures.getFirst();
List<Geometry> geometries = new ArrayList<>(groupedFeatures.size());
for (var feature : groupedFeatures) {
try {
Expand Down Expand Up @@ -331,7 +331,7 @@ public static List<VectorTile.Feature> mergeNearbyPolygons(List<VectorTile.Featu
}
merged = GeoUtils.snapAndFixPolygon(merged, stats, "merge").reverse();
} else {
merged = polygonGroup.get(0);
merged = polygonGroup.getFirst();
if (!(merged instanceof Polygonal) || merged.getEnvelopeInternal().getArea() < minArea) {
continue;
}
Expand Down Expand Up @@ -572,5 +572,5 @@ public static List<VectorTile.Feature> removePointsOutsideBuffer(List<VectorTile
return result;
}

private record WithIndex<T> (T feature, int hilbert) {}
private record WithIndex<T>(T feature, int hilbert) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private static Geometry decodeCommands(GeometryType geomType, int[] commands, in
lineStrings.add(gf.createLineString(coordSeq));
}
if (lineStrings.size() == 1) {
geometry = lineStrings.get(0);
geometry = lineStrings.getFirst();
} else if (lineStrings.size() > 1) {
geometry = gf.createMultiLineString(lineStrings.toArray(new LineString[0]));
}
Expand Down Expand Up @@ -305,12 +305,12 @@ private static Geometry decodeCommands(GeometryType geomType, int[] commands, in
}
List<Polygon> polygons = new ArrayList<>();
for (List<LinearRing> rings : polygonRings) {
LinearRing shell = rings.get(0);
LinearRing shell = rings.getFirst();
LinearRing[] holes = rings.subList(1, rings.size()).toArray(new LinearRing[rings.size() - 1]);
polygons.add(gf.createPolygon(shell, holes));
}
if (polygons.size() == 1) {
geometry = polygons.get(0);
geometry = polygons.getFirst();
}
if (polygons.size() > 1) {
geometry = gf.createMultiPolygon(GeometryFactory.toPolygonArray(polygons));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void sort() {
.sinkToConsumer("worker", workers, group -> {
try {
readSemaphore.acquire();
var chunk = group.get(0);
var chunk = group.getFirst();
var others = group.stream().skip(1).toList();
var toSort = time(reading, () -> {
// merge all chunks into first one, and remove the others
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public Expression simplifyOnce() {
return TRUE;
}
if (children.size() == 1) {
return children.get(0).simplifyOnce();
return children.getFirst().simplifyOnce();
}
if (children.contains(FALSE)) {
return FALSE;
Expand Down Expand Up @@ -283,7 +283,7 @@ public Expression simplifyOnce() {
return FALSE;
}
if (children.size() == 1) {
return children.get(0).simplifyOnce();
return children.getFirst().simplifyOnce();
}
if (children.contains(TRUE)) {
return TRUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ default List<O> getMatches(WithTags input) {
*/
default O getOrElse(WithTags input, O defaultValue) {
List<O> matches = getMatches(input);
return matches.isEmpty() ? defaultValue : matches.get(0);
return matches.isEmpty() ? defaultValue : matches.getFirst();
}

/**
* Returns the data value associated with expressions matching a feature with {@code tags}.
*/
default O getOrElse(Map<String, Object> tags, O defaultValue) {
List<O> matches = getMatches(WithTags.from(tags));
return matches.isEmpty() ? defaultValue : matches.get(0);
return matches.isEmpty() ? defaultValue : matches.getFirst();
}

/** Returns true if any expression matches that tags from an input element. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ public static Geometry fixPolygon(Geometry geom, double buffer) throws GeometryE
}

public static Geometry combineLineStrings(List<LineString> lineStrings) {
return lineStrings.size() == 1 ? lineStrings.get(0) : createMultiLineString(lineStrings);
return lineStrings.size() == 1 ? lineStrings.getFirst() : createMultiLineString(lineStrings);
}

public static Geometry combinePolygons(List<Polygon> polys) {
return polys.size() == 1 ? polys.get(0) : createMultiPolygon(polys);
return polys.size() == 1 ? polys.getFirst() : createMultiPolygon(polys);
}

public static Geometry combinePoints(List<Point> points) {
return points.size() == 1 ? points.get(0) : createMultiPoint(points);
return points.size() == 1 ? points.getFirst() : createMultiPoint(points);
}

/**
Expand Down Expand Up @@ -383,7 +383,7 @@ public static Geometry polygonToLineString(Geometry geom) throws GeometryExcepti
if (lineStrings.isEmpty()) {
throw new GeometryException("polygon_to_linestring_empty", "No line strings");
} else if (lineStrings.size() == 1) {
return lineStrings.get(0);
return lineStrings.getFirst();
} else {
return createMultiLineString(lineStrings);
}
Expand Down Expand Up @@ -530,7 +530,7 @@ public static Geometry combine(Geometry... geometries) {
innerGeometries.add(geom);
}
}
return innerGeometries.size() == 1 ? innerGeometries.get(0) :
return innerGeometries.size() == 1 ? innerGeometries.getFirst() :
JTS_FACTORY.createGeometryCollection(innerGeometries.toArray(Geometry[]::new));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void build() {
/** Returns the data associated with the first polygon containing {@code point}. */
public T getOnlyContaining(Point point) {
List<T> result = getContaining(point);
return result.isEmpty() ? null : result.get(0);
return result.isEmpty() ? null : result.getFirst();
}

/** Returns the data associated with all polygons containing {@code point}. */
Expand Down Expand Up @@ -77,7 +77,7 @@ public List<T> getContainingOrNearest(Point point) {
List<?> items = index.query(point.getEnvelopeInternal());
// optimization: if there's only one then skip checking contains/distance
if (items.size() == 1) {
if (items.get(0) instanceof GeomWithData<?> value) {
if (items.getFirst() instanceof GeomWithData<?> value) {
@SuppressWarnings("unchecked") T t = (T) value.data;
return List.of(t);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public List<T> getContainingOrNearest(Point point) {
/** Returns the data associated with a polygon that contains {@code point} or nearest polygon if none are found. */
public T get(Point point) {
List<T> nearests = getContainingOrNearest(point);
return nearests.isEmpty() ? null : nearests.get(0);
return nearests.isEmpty() ? null : nearests.getFirst();
}

/** Indexes {@code item} for all polygons contained in {@code geom}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private static Set<Ring> groupParentChildShells(List<Ring> polygons) {
if (numPolygons == 0) {
return shells;
}
shells.add(polygons.get(0));
shells.add(polygons.getFirst());
if (numPolygons == 1) {
return shells;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static Geometry reassembleLineStrings(List<List<CoordinateSequence>> geoms) {
static Geometry reassemblePolygons(List<List<CoordinateSequence>> groups) throws GeometryException {
int numGeoms = groups.size();
if (numGeoms == 1) {
return reassemblePolygon(groups.get(0));
return reassemblePolygon(groups.getFirst());
} else {
Polygon[] polygons = new Polygon[numGeoms];
for (int i = 0; i < numGeoms; i++) {
Expand All @@ -135,7 +135,7 @@ static Geometry reassemblePolygons(List<List<CoordinateSequence>> groups) throws
/** Returns a {@link Polygon} built from all outer/inner rings in {@code group}, reversing all inner rings. */
private static Polygon reassemblePolygon(List<CoordinateSequence> group) throws GeometryException {
try {
LinearRing first = GeoUtils.JTS_FACTORY.createLinearRing(group.get(0));
LinearRing first = GeoUtils.JTS_FACTORY.createLinearRing(group.getFirst());
LinearRing[] rest = new LinearRing[group.size() - 1];
for (int j = 1; j < group.size(); j++) {
CoordinateSequence seq = group.get(j);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private void slicePoint(Coordinate coord) {
TileCoord tile = TileCoord.ofXYZ(wrappedX, y, z);
double tileY = worldY - y;
tileContents.computeIfAbsent(tile, t -> List.of(new ArrayList<>()))
.get(0)
.getFirst()
.add(GeoUtils.coordinateSequence(tileX * 256, tileY * 256));
}
}
Expand Down Expand Up @@ -384,7 +384,7 @@ private void addShapeToResults(Map<TileCoord, List<CoordinateSequence>> inProgre
for (var entry : inProgressShapes.entrySet()) {
TileCoord tileID = entry.getKey();
List<CoordinateSequence> inSeqs = entry.getValue();
if (area && inSeqs.get(0).size() < 4) {
if (area && inSeqs.getFirst().size() < 4) {
// not enough points in outer polygon, ignore
continue;
}
Expand Down Expand Up @@ -573,20 +573,20 @@ record SkippedSegment(Direction side, int lo, int hi, boolean asc) {}
}
/*
A tile is inside a filled region when there is an odd number of vertical edges to the left and right
for example a simple shape:
---------
out | in | out
(0/2) | (1/1) | (2/0)
---------
or a more complex shape
--------- ---------
out | in | out | in |
(0/4) | (1/3) | (2/2) | (3/1) |
| --------- |
-------------------------
So we keep track of this number by xor'ing the left and right fills repeatedly,
then and'ing them together at the end.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private void addDeltaLogger(String name, Supplier<Duration> supplier, DoubleFunc

/** Adds the CPU utilization of every thread starting with {@code prefix} since the last log to output. */
public ProgressLoggers addThreadPoolStats(String name, String prefix) {
boolean first = loggers.isEmpty() || !(loggers.get(loggers.size() - 1) instanceof WorkerPipelineLogger);
boolean first = loggers.isEmpty() || !(loggers.getLast() instanceof WorkerPipelineLogger);
try {
Map<Long, ProcessInfo.ThreadState> lastThreads = ProcessInfo.getThreadStats();
AtomicLong lastTime = new AtomicLong(System.nanoTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected String searchIndexForDownloadUrl(String searchQuery, List<ContentXml>
} else if (results.size() > 1) {
throw new IllegalArgumentException("Found multiple AWS osm download URLs for " + searchQuery + ": " + results);
}
return results.get(0);
return results.getFirst();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static String getIfOnly(String name, String searchQuery, List<Properties
"Multiple " + name + " for '" + searchQuery + "': " + values.stream().map(d -> d.id).collect(
Collectors.joining(", ")));
} else if (values.size() == 1) {
return values.get(0).urls.get("pbf");
return values.getFirst().urls.get("pbf");
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ void testReorderNestedMultipolygons() throws Exception {

var tileContents = results.tiles.get(TileCoord.ofXYZ(0, 0, 0));
assertEquals(1, tileContents.size());
Geometry geom = tileContents.get(0).geometry().geom();
Geometry geom = tileContents.getFirst().geometry().geom();
assertTrue(geom instanceof MultiPolygon, geom.toString());
MultiPolygon multiPolygon = (MultiPolygon) geom;
assertSameNormalizedFeature(newPolygon(
Expand Down Expand Up @@ -1884,7 +1884,7 @@ void testIssue546Terschelling() throws Exception {
var point = newPoint(tileX, tileY);

assertEquals(1, problematicTile.size());
var geomCompare = problematicTile.get(0).geometry();
var geomCompare = problematicTile.getFirst().geometry();
geomCompare.validate();
var geom = geomCompare.geom();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void testAttributeTypes() {

List<VectorTile.Feature> decoded = VectorTile.decode(encoded);
assertEquals(1, decoded.size());
Map<String, Object> decodedAttributes = decoded.get(0).attrs();
Map<String, Object> decodedAttributes = decoded.getFirst().attrs();
assertEquals("value1", decodedAttributes.get("key1"));
assertEquals(123L, decodedAttributes.get("key2"));
assertEquals(234.1f, decodedAttributes.get("key3"));
Expand Down Expand Up @@ -220,7 +220,7 @@ void testMultiPolygon() {

var features = VectorTile.decode(encoded);
assertEquals(1, features.size());
MultiPolygon mp2 = (MultiPolygon) decodeSilently(features.get(0).geometry());
MultiPolygon mp2 = (MultiPolygon) decodeSilently(features.getFirst().geometry());
assertEquals(mp.getNumGeometries(), mp2.getNumGeometries());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ void testReadShapefileLeniently(@TempDir Path dir) throws IOException, Transform
assertEquals(1, reader.getFeatureCount());
List<SimpleFeature> features = new ArrayList<>();
reader.readFeatures(features::add);
assertEquals(10.5113, features.get(0).latLonGeometry().getCentroid().getX(), 1e-4);
assertEquals(0, features.get(0).latLonGeometry().getCentroid().getY(), 1e-4);
assertEquals(3, features.get(0).getTag("value"));
assertEquals(10.5113, features.getFirst().latLonGeometry().getCentroid().getX(), 1e-4);
assertEquals(0, features.getFirst().latLonGeometry().getCentroid().getY(), 1e-4);
assertEquals(3, features.getFirst().getTag("value"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public Object apply(String key) {
}

public String matchKey() {
return matchKeys().isEmpty() ? null : matchKeys().get(0);
return matchKeys().isEmpty() ? null : matchKeys().getFirst();
}

public Object matchValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public O apply(I i) {
public ConfigExpression<I, O> simplifyOnce() {
return switch (children.size()) {
case 0 -> constOf(null);
case 1 -> children.get(0);
case 1 -> children.getFirst();
default -> {
var result = children.stream()
.flatMap(
Expand Down

0 comments on commit 507ea4e

Please sign in to comment.