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 if (other.name != null) { 104 return false; 105 } 106 } else if (!this.name.equals(other.name)) { 107 return false; 108 } 109 return true; 110 } 111 112 /** 113 * Gets a description suitable for logging. 114 * @see java.lang.Object#toString() 115 * @return a description suitable for logging 116 */ 117 @Override 118 public String toString() { 119 return "Resource [name=" + this.name + "]"; 120 } 121 122 /** 123 * Comparison happens based on name. 124 * @see java.lang.Comparable#compareTo(java.lang.Object) 125 * @param other resource to compare to. 126 * @return result of comparison based on name 127 */ 128 public int compareTo(final Resource other) { 129 return getName().compareTo(other.getName()); 130 } 131 132 /** 133 * Accepts a visitor. 134 * @param visitor possibly null 135 */ 136 public void accept(final Visitor visitor) { 137 if (visitor != null && visitor.traverseResource()) { 138 visitor.visit(this); 139 } 140 } 141 142 /** 143 * Gets a locator for the source. 144 * @return a source locator, possibly null 145 */ 146 public String getSource() { 147 return this.source; 148 } 149 150 /** 151 * Is this resource linked to source? 152 * @return true when this resource has linked source, 153 * false otherwise 154 */ 155 public boolean hasSource() { 156 return getSource() != null && !"".equals(getSource()); 157 } 158 }