View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.basepom.mojo.duplicatefinder.classpath;
15  
16  import java.io.File;
17  import java.util.function.Predicate;
18  import java.util.stream.Collectors;
19  
20  import com.google.common.collect.ImmutableSet;
21  import com.google.common.collect.Multimap;
22  
23  class ClasspathCacheElement {
24  
25      private final File element;
26      private final ImmutableSet<String> classes;
27      private final ImmutableSet<String> resources;
28  
29      public static Builder builder(final File element) {
30          return new Builder(element);
31      }
32  
33      private ClasspathCacheElement(final File element, final ImmutableSet<String> classes, final ImmutableSet<String> resources) {
34          this.element = element;
35          this.classes = classes;
36          this.resources = resources;
37      }
38  
39      void putClasses(final Multimap<String, File> classMap, final Predicate<String> excludePredicate) {
40          for (final String className : classes.stream().filter(excludePredicate.negate()).collect(Collectors.toList())) {
41              classMap.put(className, element);
42          }
43      }
44  
45      void putResources(final Multimap<String, File> resourceMap, final Predicate<String> excludePredicate) {
46          for (final String resource : resources.stream().filter(excludePredicate.negate()).collect(Collectors.toList())) {
47              resourceMap.put(resource, element);
48          }
49      }
50  
51      static class Builder {
52  
53          private final File element;
54          private final ImmutableSet.Builder<String> classBuilder = ImmutableSet.builder();
55          private final ImmutableSet.Builder<String> resourcesBuilder = ImmutableSet.builder();
56  
57          private Builder(final File element) {
58              this.element = element;
59          }
60  
61          void addClass(final String className) {
62              classBuilder.add(className);
63          }
64  
65          void addResource(final String resource) {
66              resourcesBuilder.add(resource);
67          }
68  
69          ClasspathCacheElement build() {
70              return new ClasspathCacheElement(element, classBuilder.build(), resourcesBuilder.build());
71          }
72      }
73  }