View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package dev.aherscu.qa.s3.publisher.maven.plugin.config;
18  
19  import java.io.*;
20  import java.util.*;
21  
22  import org.apache.maven.shared.utils.io.*;
23  
24  public abstract class AbstractFilesListBuilder {
25  
26      protected final File                     inputDirectory;
27      protected final List<String>             excludes;
28      protected final Map<String, Metadata>    metadataMap;
29      protected final Map<String, ManagedFile> fileMap;
30  
31      protected AbstractFilesListBuilder(final File inputDirectory,
32          final List<String> excludes, final List<Metadata> metadatas) {
33          this.inputDirectory = inputDirectory;
34          this.excludes = excludes;
35          metadataMap = buildMetadataMap(metadatas);
36          fileMap = new HashMap<>();
37      }
38  
39      private static Map<String, Metadata> buildMetadataMap(
40          final Collection<Metadata> metadatas) {
41          final Map<String, Metadata> map =
42              new HashMap<>(metadatas.size());
43          for (final Metadata metadata : metadatas) {
44              map.put(metadata.getId(), metadata);
45          }
46          return map;
47      }
48  
49      private static String convertToString(final Collection<String> list) {
50          final StringBuilder builder = new StringBuilder();
51          int i = 0;
52          for (final Iterator<?> iterator = list.iterator(); iterator
53              .hasNext(); i++) {
54              if (i > 0) {
55                  builder.append(", ");
56              }
57              builder.append(iterator.next());
58          }
59          return builder.toString();
60      }
61  
62      protected List<String> getMatchingFilesNames(final String pattern)
63          throws IOException {
64          return FileUtils.getFileNames(inputDirectory, pattern,
65              convertToString(excludes), true);
66      }
67  
68      protected Metadata getMetadata(final Bind bind) {
69          Metadata metadata = metadataMap.get(bind.getMetadataId());
70          if (metadata == null) {
71              metadata = getDefaultMetadata();
72              if (metadata == null)
73                  throw new IllegalArgumentException("The metadata with Id "
74                      + bind.getMetadataId() + " is undefined.");
75          }
76          return metadata;
77      }
78  
79      private Metadata getDefaultMetadata() {
80          Metadata retMetadata = null;
81          for (final Metadata metadata : metadataMap.values()) {
82              if (metadata.isDefault()) {
83                  retMetadata = metadata;
84              }
85          }
86          return retMetadata;
87      }
88  }