View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.util;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.function.Function;
32  
33  import org.eluder.coveralls.maven.plugin.CoverageParser;
34  
35  /**
36   * The Class ExistingFiles.
37   */
38  public class ExistingFiles implements Iterable<File> {
39  
40      /** The delegate. */
41      private final List<File> delegate = new ArrayList<>();
42  
43      /**
44       * Instantiates a new existing files.
45       */
46      public ExistingFiles() {
47          // do nothing
48      }
49  
50      /**
51       * Adds the all.
52       *
53       * @param files
54       *            the files
55       *
56       * @return the existing files
57       */
58      public ExistingFiles addAll(final Iterable<File> files) {
59          if (files == null) {
60              throw new NullPointerException("Files must be defined");
61          }
62          for (final File file : files) {
63              this.add(file);
64          }
65          return this;
66      }
67  
68      /**
69       * Adds the.
70       *
71       * @param file
72       *            the file
73       *
74       * @return the existing files
75       */
76      public ExistingFiles add(final File file) {
77          if (file == null) {
78              throw new NullPointerException("File must be defined");
79          }
80          if (file.exists() && file.isFile() && !this.delegate.contains(file)) {
81              this.delegate.add(file);
82          }
83          return this;
84      }
85  
86      @Override
87      public Iterator<File> iterator() {
88          return this.delegate.iterator();
89      }
90  
91      /**
92       * Creates the.
93       *
94       * @param files
95       *            the files
96       *
97       * @return the existing files
98       */
99      public static ExistingFiles create(final Iterable<File> files) {
100         final var existingFiles = new ExistingFiles();
101         if (files != null) {
102             existingFiles.addAll(files);
103         }
104         return existingFiles;
105     }
106 
107     /**
108      * Converts existing files to a list of CoverageParser using the provided function.
109      *
110      * @param parserFunction
111      *            function to convert File to CoverageParser
112      *
113      * @return list of CoverageParser for valid files
114      */
115     public List<CoverageParser> toParsers(Function<File, CoverageParser> parserFunction) {
116         final List<CoverageParser> parsers = new ArrayList<>();
117         for (final File file : this.delegate) {
118             parsers.add(parserFunction.apply(file));
119         }
120         return parsers;
121     }
122 }