View Javadoc
1   /*
2    * Copyright (C) 2026 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.geotools.collection;
7   
8   import java.util.function.IntConsumer;
9   import org.geotools.data.simple.SimpleFeatureCollection;
10  import org.geotools.data.simple.SimpleFeatureIterator;
11  import org.geotools.feature.collection.DecoratingSimpleFeatureCollection;
12  import org.jspecify.annotations.Nullable;
13  
14  /**
15   * A decorating feature collection that will pass a callback to the iterator to report the number of features provided.
16   */
17  public class ProgressReportingFeatureCollection extends DecoratingSimpleFeatureCollection {
18    private final int progressInterval;
19    private final IntConsumer progressCallback;
20  
21    /**
22     * Creates a new {@code ProgressReportingFeatureCollection} that wraps the given delegate and reports progress at
23     * the specified interval.
24     *
25     * @param delegate the underlying {@link SimpleFeatureCollection} to decorate
26     * @param progressInterval the number of features between each progress callback invocation; must be greater than
27     *     {@code 0}
28     * @param progressCallback a callback that receives the current feature count at each interval; may be {@code null}
29     */
30    public ProgressReportingFeatureCollection(
31        SimpleFeatureCollection delegate, int progressInterval, @Nullable IntConsumer progressCallback) {
32      super(delegate);
33      if (progressInterval <= 0) {
34        throw new IllegalArgumentException("progressInterval must be greater than 0");
35      }
36      this.delegate = delegate;
37      this.progressInterval = progressInterval;
38      this.progressCallback = progressCallback;
39    }
40  
41    @Override
42    public SimpleFeatureIterator features() {
43      return new ProgressReportingFeatureIterator(delegate.features(), progressInterval, progressCallback);
44    }
45  }