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.Set;
22  import java.util.TreeSet;
23  
24  /**
25   * Collates licenses and organisations.
26   */
27  public class LicenseAndOrganisationCollator extends Visitor {
28      /** The licenses currently collected. */
29      private final Set<License> licenses = new TreeSet<License>();
30      /** The organisations currently collected. */
31      private final Set<Organisation> organisations = new TreeSet<Organisation>();
32  
33      /**
34       * Gets the licenses collected.
35       * @return not null
36       */
37      public Set<License> getLicenses() {
38          return this.licenses;
39      }
40  
41      /**
42       * Gets the organisations collected.
43       * @return not null
44       */
45      public Set<Organisation> getOrganisation() {
46          return this.organisations;
47      }
48  
49      /**
50       * Don't traverse resources.
51       * @see Visitor#traverseResource()
52       * @return false
53       */
54      @Override
55      public boolean traverseResource() {
56          return false;
57      }
58  
59      /**
60       * Visits {@link WithLicense}.
61       * @see Visitor#visit(WithLicense)
62       * @param license not null
63       */
64      @Override
65      public void visit(final WithLicense license) {
66          this.licenses.add(license.getLicense());
67      }
68  
69      /**
70       * Was this the only license collected?
71       * @param license not null
72       * @return true when the collection contains just this license,
73       * false when no licenses or any other licenses were collected
74       */
75      public boolean isOnlyLicense(final License license) {
76          return (this.licenses.size() == 1) && this.licenses.contains(license);
77      }
78  
79      /**
80       * Visits {@link ByOrganisation}.
81       * @see Visitor#visit(ByOrganisation)
82       * @param byOrganisation not null
83       */
84      @Override
85      public void visit(final ByOrganisation byOrganisation) {
86          this.organisations.add(byOrganisation.getOrganisation());
87      }
88  
89      /**
90       * Something useful for logging.
91       * @see java.lang.Object#toString()
92       * @return something for logging
93       */
94      @Override
95      public String toString() {
96          return "LicenseAndOrganisationCollator [licenses=" + this.licenses
97                  + ", organisations=" + this.organisations + "]";
98      }
99  
100     /**
101      * Is there only one organisation collected with the given id?
102      * @param primaryOrganisationId not null
103      * @return true when only one organisation has been collected
104      * and it has the given id
105      */
106     public boolean isOnlyOrganisation(final String primaryOrganisationId) {
107         return this.organisations.size() == 1
108                 && this.organisations.iterator().next().getId()
109                         .equals(primaryOrganisationId);
110     }
111 }