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.model;
20  
21  import java.util.Collection;
22  import java.util.TreeSet;
23  
24  import org.apache.commons.lang3.tuple.ImmutablePair;
25  import org.apache.commons.lang3.tuple.Pair;
26  
27  /**
28   * Collates resources within directories, flattening the
29   * model.
30   */
31  public class ResourceNamesCollator extends Visitor {
32  
33      /** Resources collected. */
34      private final Collection<Pair<WithinDirectory, Resource>> resources 
35          = new TreeSet<Pair<WithinDirectory, Resource>>();
36      /** Duplicate resources discovered. */
37      private final Collection<Pair<WithinDirectory, Resource>> duplicates 
38          = new TreeSet<Pair<WithinDirectory, Resource>>();
39      /** Last directory visited. */
40      private WithinDirectory lastDirectory;
41  
42      /**
43       * Gets the names of the resources collected.
44       * @return not null
45       */
46      public Collection<String> getNames() {
47          final Collection<String> names = new TreeSet<String>();
48          for (final Pair<WithinDirectory, Resource> pair : this.resources) {
49              names.add(pair.getRight().getName());
50          }
51          return names;
52      }
53  
54      /**
55       * Gets the duplicate resources discovered.
56       * @return not null
57       */
58      public Collection<Pair<WithinDirectory, Resource>> getDuplicates() {
59          return this.duplicates;
60      }
61  
62      /**
63       * Sets the last directory visited.
64       * @see Visitor#visit(WithinDirectory)
65       * @param directory not null
66       */
67      @Override
68      public void visit(final WithinDirectory directory) {
69          this.lastDirectory = directory;
70      }
71  
72      /**
73       * Collects this resource.
74       * @see Visitor#visit(Resource)
75       * @param resource not null
76       */
77      @Override
78      public void visit(final Resource resource) {
79          if (!this.resources.add(new ImmutablePair<WithinDirectory, Resource>(
80                  this.lastDirectory, resource))) {
81              // Already added
82              if (this.lastDirectory == null) {
83                  // Issue with logic which will result in a null pointer later
84                  throw new IllegalArgumentException(
85                          "Expected directory to be present");
86              }
87              this.duplicates.add(new ImmutablePair<WithinDirectory, Resource>(
88                      this.lastDirectory, resource));
89        }
90      }
91  }