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  /**
22   * A resource expected in a software distribution.
23   */
24  public class Resource implements Comparable<Resource>, ContentElement {
25  
26      /** Names this resource. */
27      private final String name;
28      /** Optional link to a notice for this resource. */
29      private final String noticeId;
30      /**
31       * Optional describes how source may be obtained
32       * for this resource.
33       */
34      private final String source;
35  
36      /**
37       * Constructs a resource in a software distribution.
38       * @param name not null
39       * @param noticeId identifies the notice for this resource,
40       * null when there is no NOTICE
41       * @param source describes how source may be obtained,
42       * null when this is not needed
43       */
44      public Resource(final String name, final String noticeId,
45              final String source) {
46          super();
47          this.name = name;
48          this.noticeId = noticeId;
49          this.source = source;
50      }
51  
52      /**
53       * Gets the name for this resource
54       * expected in a software distribution.
55       * @return not null
56       */
57      public String getName() {
58          return this.name;
59      }
60  
61      /**
62       * Gets an identifier for the optional NOTICE.
63       * @return an identifier for the NOTICE,
64       * or null when the resource has no NOTICE
65       */
66      public String getNoticeId() {
67          return this.noticeId;
68      }
69  
70      /**
71       * Based on name.
72       * @see java.lang.Object#hashCode()
73       * @return hash code for the name
74       */
75      @Override
76      public int hashCode() {
77          final int prime = 31;
78          int result = 1;
79          result = prime * result
80                  + ((this.name == null) ? 0 : this.name.hashCode());
81          return result;
82      }
83  
84      /**
85       * Based on name.
86       * @see java.lang.Object#equals(java.lang.Object)
87       * @param obj possibly null
88       * @return equality based on name
89       */
90      @Override
91      public boolean equals(final Object obj) {
92          if (this == obj) {
93              return true;
94          }
95          if (obj == null) {
96              return false;
97          }
98          if (getClass() != obj.getClass()) {
99              return false;
100         }
101         final Resource other = (Resource) obj;
102         if (this.name == null) {
103             return other.name == null;
104         } else return this.name.equals(other.name);
105     }
106 
107     /**
108      * Gets a description suitable for logging.
109      * @see java.lang.Object#toString()
110      * @return a description suitable for logging
111      */
112     @Override
113     public String toString() {
114         return "Resource [name=" + this.name + "]";
115     }
116 
117     /**
118      * Comparison happens based on name.
119      * @see java.lang.Comparable#compareTo(java.lang.Object)
120      * @param other resource to compare to.
121      * @return result of comparison based on name
122      */
123     public int compareTo(final Resource other) {
124         return getName().compareTo(other.getName());
125     }
126 
127     /**
128      * Accepts a visitor.
129      * @param visitor possibly null
130      */
131     public void accept(final Visitor visitor) {
132         if (visitor != null && visitor.traverseResource()) {
133             visitor.visit(this);
134         }
135     }
136 
137     /**
138      * Gets a locator for the source.
139      * @return a source locator, possibly null
140      */
141     public String getSource() {
142         return this.source;
143     }
144 
145     /**
146      * Is this resource linked to source?
147      * @return true when this resource has linked source,
148      * false otherwise
149      */
150     public boolean hasSource() {
151         return getSource() != null && !"".equals(getSource());
152     }
153 }