View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.creadur.whisker.app.analysis;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.TreeSet;
24  
25  import org.apache.commons.lang3.tuple.ImmutablePair;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.creadur.whisker.model.Resource;
28  import org.apache.creadur.whisker.model.Visitor;
29  import org.apache.creadur.whisker.model.WithLicense;
30  import org.apache.creadur.whisker.model.WithinDirectory;
31  
32  /**
33   * Collects resource source details.
34   */
35  public final class ResourceSourceAuditor extends Visitor {
36      /** Last license visited. */
37      private WithLicense lastLicense;
38      /** Last directory visited. */
39      private WithinDirectory lastDirectory;
40      /** Collects resources that are missing source. */
41      private final Collection<
42          Pair<WithinDirectory, Resource>> resourcesMissingSource
43              = new TreeSet<Pair<WithinDirectory, Resource>>();
44      /** Collects resources that match a source. */
45      private final Collection<
46          Pair<WithinDirectory, Resource>> resourcesWithSource
47              = new TreeSet<Pair<WithinDirectory, Resource>>();
48  
49      /**
50       * Hook for public domain.
51       * @return false (no need to traverse public domain)
52       * @see Visitor#traversePublicDomain()
53       */
54      @Override
55      public boolean traversePublicDomain() {
56          // no need to traverse public domain
57          return false;
58      }
59  
60  
61      /**
62       * Gets the resources with source collected during visits.
63       * @return the resourcesWithSource, not null
64       */
65      public Collection<
66              Pair<WithinDirectory, Resource>> getResourcesWithSource() {
67          return resourcesWithSource;
68      }
69  
70      /**
71       * Gets the resources missing source collected during visits.
72       * @return the resourcesMissingSource not null
73       */
74      public Collection<
75              Pair<WithinDirectory, Resource>> getResourcesMissingSource() {
76          return resourcesMissingSource;
77      }
78  
79      /**
80       * Accepts a directory visit.
81       * @param directory not null
82       * @see Visitor#visit(WithinDirectory)
83       */
84      @Override
85      public void visit(final WithinDirectory directory) {
86          this.lastDirectory = directory;
87      }
88  
89  
90  
91      /**
92       * Accepts a license visit.
93       * @param license not null
94       * @see Visitor#visit(WithLicense)
95       */
96      @Override
97      public void visit(final WithLicense license) {
98          this.lastLicense = license;
99      }
100 
101     /**
102      * Accepts a visit to a resource.
103      * @param resource not null
104      * @see Visitor#visit(Resource)
105      */
106     @Override
107     @SuppressWarnings("PMD.EmptyIfStmt")
108     public void visit(final Resource resource) {
109         if (lastLicense == null) {
110             throw new IllegalArgumentException(
111                     "Last license unexpectedly null for resource "
112                             + resource);
113         } else if (lastLicense.isSourceRequired()) {
114             final ImmutablePair<WithinDirectory, Resource> pair =
115                     new ImmutablePair<
116                         WithinDirectory,
117                         Resource>(lastDirectory, resource);
118             if (resource.hasSource()) {
119                 resourcesWithSource.add(pair);
120             } else {
121                 resourcesMissingSource.add(pair);
122             }
123         } else {
124             // Source not required
125         }
126     }
127 
128 
129     /**
130      * Gets those resources visited which require source links.
131      * @return not null, possibly empty
132      */
133     public Collection<Resource> getResourcesRequiringSourceLinks() {
134         final Collection<Resource> results = new ArrayList<Resource>();
135         for (Pair<WithinDirectory, Resource> pair: resourcesWithSource) {
136             results.add(pair.getRight());
137         }
138         return results;
139     }
140 
141 }