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  
15  package org.basepom.mojo.duplicatefinder.classpath;
16  
17  import java.io.File;
18  import java.util.function.Predicate;
19  import java.util.stream.Collectors;
20  
21  import com.google.common.collect.ImmutableSet;
22  import com.google.common.collect.Multimap;
23  
24  final class ClasspathCacheElement {
25  
26      private final File element;
27      private final ImmutableSet<String> classes;
28      private final ImmutableSet<String> resources;
29  
30      public static Builder builder(final File element) {
31          return new Builder(element);
32      }
33  
34      private ClasspathCacheElement(final File element, final ImmutableSet<String> classes, final ImmutableSet<String> resources) {
35          this.element = element;
36          this.classes = classes;
37          this.resources = resources;
38      }
39  
40      void putClasses(final Multimap<String, File> classMap, final Predicate<String> excludePredicate) {
41          for (final String className : classes.stream().filter(excludePredicate.negate()).collect(Collectors.toList())) {
42              classMap.put(className, element);
43          }
44      }
45  
46      void putResources(final Multimap<String, File> resourceMap, final Predicate<String> excludePredicate) {
47          for (final String resource : resources.stream().filter(excludePredicate.negate()).collect(Collectors.toList())) {
48              resourceMap.put(resource, element);
49          }
50      }
51  
52      static final class Builder {
53  
54          private final File element;
55          private final ImmutableSet.Builder<String> classBuilder = ImmutableSet.builder();
56          private final ImmutableSet.Builder<String> resourcesBuilder = ImmutableSet.builder();
57  
58          private Builder(final File element) {
59              this.element = element;
60          }
61  
62          void addClass(final String className) {
63              classBuilder.add(className);
64          }
65  
66          void addResource(final String resource) {
67              resourcesBuilder.add(resource);
68          }
69  
70          ClasspathCacheElement build() {
71              return new ClasspathCacheElement(element, classBuilder.build(), resourcesBuilder.build());
72          }
73      }
74  }