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.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  /**
29   * 
30   */
31  public class NoticeCollator extends Visitor {
32  
33      private final Map<String, Collection<Resource>> resourcesByNoticeId = new HashMap<String, Collection<Resource>>();
34  
35      /**
36       * @return the noticeIds
37       */
38      public Set<String> getNoticeIds() {
39          return this.resourcesByNoticeId.keySet();
40      }
41  
42      /**
43       * @see Visitor#visit(Resource)
44       */
45      @Override
46      public void visit(final Resource resource) {
47          final String noticeId = resource.getNoticeId();
48          if (noticeId != null) {
49              if (!this.resourcesByNoticeId.containsKey(noticeId)) {
50                  this.resourcesByNoticeId.put(noticeId, new TreeSet<Resource>());
51              }
52              this.resourcesByNoticeId.get(noticeId).add(resource);
53          }
54      }
55  
56      public Map<String, Collection<Resource>> resourceNotices(
57              final Map<String, String> notices) {
58          final Map<String, Collection<Resource>> results = new HashMap<String, Collection<Resource>>();
59          for (final Map.Entry<String, Collection<Resource>> entry : this.resourcesByNoticeId
60                  .entrySet()) {
61              if (notices.containsKey(entry.getKey())) {
62                  results.put(notices.get(entry.getKey()), new TreeSet<Resource>(
63                          entry.getValue()));
64              } else {
65                  throw new IllegalArgumentException("Notice missing for id "
66                          + entry.getKey());
67              }
68          }
69          return results;
70      }
71  
72      /**
73       * @param notices map of notice-id and notices.
74       * @return list of notices.
75       */
76      public Set<String> notices(final Map<String, String> notices) {
77          final Set<String> results = new HashSet<String>();
78          for (final String id : getNoticeIds()) {
79              if (notices.containsKey(id)) {
80                  results.add(notices.get(id));
81              } else {
82                  throw new IllegalArgumentException("Notice missing for id "
83                          + id);
84              }
85          }
86          return results;
87      }
88  }