FiltersManager.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.data;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;

  21. /** Manager for {@link DataFilter data filters}.
  22.  * <p>
  23.  * This manager holds a set of filters and applies all the relevant
  24.  * ones by building a stack that transforms a raw {@link DataSource}
  25.  * into a processed {@link DataSource}.
  26.  * </p>
  27.  * @see DataSource
  28.  * @see DataFilter
  29.  * @author Luc Maisonobe
  30.  * @since 11.0
  31.  */
  32. public class FiltersManager {

  33.     /** Supported filters. */
  34.     private final List<DataFilter> filters;

  35.     /** Build an empty manager.
  36.      */
  37.     public FiltersManager() {
  38.         this.filters = new ArrayList<>();
  39.     }

  40.     /** Add a data filter.
  41.      * @param filter filter to add
  42.      * @see #applyRelevantFilters(DataSource)
  43.      * @see #clearFilters()
  44.      */
  45.     public void addFilter(final DataFilter filter) {
  46.         filters.add(filter);
  47.     }

  48.     /** Remove all data filters.
  49.      * @see #addFilter(DataFilter)
  50.      */
  51.     public void clearFilters() {
  52.         filters.clear();
  53.     }

  54.     /** Apply all the relevant data filters, taking care of layers.
  55.      * <p>
  56.      * If several filters can be applied, they will all be applied
  57.      * as a stack, even recursively if required. This means that if
  58.      * filter A applies to files with names of the form base.ext.a
  59.      * and filter B applies to files with names of the form base.ext.b,
  60.      * then providing base.ext.a.b.a will result in filter A being
  61.      * applied on top of filter B which itself is applied on top of
  62.      * another instance of filter A.
  63.      * </p>
  64.      * @param original original data source
  65.      * @return fully filtered data source
  66.      * @exception IOException if some data stream cannot be filtered
  67.      * @see #addFilter(DataFilter)
  68.      * @see #clearFilters()
  69.      * @since 9.2
  70.      */
  71.     public DataSource applyRelevantFilters(final DataSource original)
  72.         throws IOException {
  73.         DataSource top = original;
  74.         for (boolean filtering = true; filtering;) {
  75.             filtering = false;
  76.             for (final DataFilter filter : filters) {
  77.                 final DataSource filtered = filter.filter(top);
  78.                 if (filtered != top) {
  79.                     // the filter has been applied, we need to restart the loop
  80.                     top       = filtered;
  81.                     filtering = true;
  82.                     break;
  83.                 }
  84.             }
  85.         }
  86.         return top;
  87.     }

  88. }