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  
23  /**
24   * Relates the responsible group or individual to one
25   * or more resources.
26   */
27  public class ByOrganisation implements Comparable<ByOrganisation>, ContentElement {
28  
29      /** The responsible group or individual. */
30      private final Organisation organisation;
31      /** The resources for which the group or individual is responsible. */
32      private final Collection<Resource> resources;
33  
34      /**
35       * Links an individual or group
36       * to the resources for which they have responsibility.
37       * @param resources
38       *            not null
39       * @param organisation
40       *            not null
41       */
42      public ByOrganisation(final Organisation organisation,
43              final Collection<Resource> resources) {
44          super();
45          this.organisation = organisation;
46          this.resources = resources;
47      }
48  
49      /**
50       * Gets the name of the individual or group responsible.
51       * @return not null
52       */
53      public String getName() {
54          return this.organisation.getName();
55      }
56  
57      /**
58       * Gets the primary URL for the individual or group responsible.
59       * @return not null
60       */
61      public String getURL() {
62          return this.organisation.getURL();
63      }
64  
65      /**
66       * Gets the primary identifier for the individual or group responsible.
67       * @return not null
68       */
69      public String getId() {
70          return this.organisation.getId();
71      }
72  
73      /**
74       * Gets the resource for which the linked individual or group is responsible.
75       * @return not null, possibly empty
76       */
77      public Collection<Resource> getResources() {
78          return this.resources;
79      }
80  
81      /**
82       * Gets the organisation representing the individual or group responible
83       * for the linked resources.
84       * @return the organisation , not ull
85       */
86      public Organisation getOrganisation() {
87          return this.organisation;
88      }
89  
90      /**
91       * Based on organisation.
92       * @see java.lang.Object#hashCode()
93       * @return hash based on organisation
94       */
95      @Override
96      public final int hashCode() {
97          final int prime = 31;
98          int result = 1;
99          result = prime
100                 * result
101                 + ((this.organisation == null) ? 0 : this.organisation
102                         .hashCode());
103         return result;
104     }
105 
106     /**
107      * Equal iff organisations are equal.
108      * @see java.lang.Object#equals(java.lang.Object)
109      * @param obj possibly null
110      * @return true when organisations are equal,
111      * false otherwise
112      */
113     @Override
114     public boolean equals(final Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
124         final ByOrganisation other = (ByOrganisation) obj;
125         if (this.organisation == null) {
126             if (other.organisation != null) {
127                 return false;
128             }
129         } else if (!this.organisation.equals(other.organisation)) {
130             return false;
131         }
132         return true;
133     }
134 
135     /**
136      * Delegates to organisation.
137      * @see java.lang.Comparable#compareTo(java.lang.Object)
138      * @param other possibly null
139      * @return {@link Organisation#compareTo(Organisation)}
140      */
141     public int compareTo(final ByOrganisation other) {
142         return this.organisation.compareTo(other.getOrganisation());
143     }
144 
145     /**
146      * Accepts a visitor.
147      * @param visitor possibly null
148      */
149     public void accept(final Visitor visitor) {
150         if (visitor != null && visitor.traverseByOrganisation()) {
151             visitor.visit(this);
152             for (final Resource resource : getResources()) {
153                 resource.accept(visitor);
154             }
155         }
156     }
157 
158     /**
159      * Describes object suitably for logging.
160      * @return something suitable for logging
161      */
162     @Override
163     public String toString() {
164         return "ByOrganisation [organisation=" + this.organisation
165                 + ", resources=" + this.resources + "]";
166     }
167 }