001 /* 002 * JBoss, Home of Professional Open Source 003 * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors 004 * as indicated by the @author tags. All rights reserved. 005 * See the copyright.txt in the distribution for a 006 * full listing of individual contributors. 007 * 008 * This copyrighted material is made available to anyone wishing to use, 009 * modify, copy, or redistribute it subject to the terms and conditions 010 * of the GNU Lesser General Public License, v. 2.1. 011 * This program is distributed in the hope that it will be useful, but WITHOUT A 012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 013 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 014 * You should have received a copy of the GNU Lesser General Public License, 015 * v.2.1 along with this distribution; if not, write to the Free Software 016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 017 * MA 02110-1301, USA. 018 */ 019 package org.switchyard.common.version; 020 021 import static org.switchyard.common.version.BaseVersion.compare; 022 023 import java.net.MalformedURLException; 024 import java.net.URI; 025 import java.net.URISyntaxException; 026 import java.net.URL; 027 028 /** 029 * BaseImplementation. 030 * 031 * @author David Ward <<a href="mailto:dward@jboss.org">dward@jboss.org</a>> (C) 2012 Red Hat Inc. 032 */ 033 public class BaseImplementation implements Implementation { 034 035 private final String _title; 036 private final String _vendor; 037 private final String _vendorId; 038 private final URL _url; 039 // we maintain a URI internally for equals() and hashCode() since URLs perform name resolution during equals() 040 private final URI _uri; 041 private final String _version; 042 043 /** 044 * Constructs a new BaseImplementation. 045 * @param title the title 046 * @param vendor the vendor 047 * @param vendorId the vendorId 048 * @param url the url 049 * @param version the version 050 */ 051 public BaseImplementation(String title, String vendor, String vendorId, URL url, String version) { 052 _title = title; 053 _vendor = vendor; 054 _vendorId = vendorId; 055 _url = url; 056 _uri = toURI(_url); 057 _version = version; 058 } 059 060 /** 061 * Constructs a new BaseImplementation. 062 * @param title the title 063 * @param vendor the vendor 064 * @param vendorId the vendorId 065 * @param url the url 066 * @param version the version 067 */ 068 public BaseImplementation(String title, String vendor, String vendorId, String url, String version) { 069 _title = title; 070 _vendor = vendor; 071 _vendorId = vendorId; 072 _url = toURL(url); 073 _uri = toURI(_url); 074 _version = version; 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 @Override 081 public String getTitle() { 082 return _title; 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 @Override 089 public String getVendor() { 090 return _vendor; 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 @Override 097 public String getVendorId() { 098 return _vendorId; 099 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override 105 public URL getURL() { 106 return _url; 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override 113 public String getVersion() { 114 return _version; 115 } 116 117 /** 118 * {@inheritDoc} 119 */ 120 @Override 121 public String toString() { 122 return String.format("%s [title=%s, vendor=%s, vendorId=%s, url=%s, version=%s]", Implementation.class.getSimpleName(), getTitle(), getVendor(), getVendorId(), getURL(), getVersion()); 123 } 124 125 /** 126 * {@inheritDoc} 127 */ 128 @Override 129 public boolean equals(Object obj) { 130 if (this == obj) { 131 return true; 132 } 133 if (obj == null) { 134 return false; 135 } 136 if (getClass() != obj.getClass()) { 137 return false; 138 } 139 BaseImplementation other = (BaseImplementation)obj; 140 if (_title == null) { 141 if (other._title != null) { 142 return false; 143 } 144 } else if (!_title.equals(other._title)) { 145 return false; 146 } 147 if (_vendor == null) { 148 if (other._vendor != null) { 149 return false; 150 } 151 } else if (!_vendor.equals(other._vendor)) { 152 return false; 153 } 154 if (_vendorId == null) { 155 if (other._vendorId != null) { 156 return false; 157 } 158 } else if (!_vendorId.equals(other._vendorId)) { 159 return false; 160 } 161 if (_uri == null) { 162 if (other._uri != null) { 163 return false; 164 } 165 } else if (!_uri.equals(other._uri)) { 166 return false; 167 } 168 if (_version == null) { 169 if (other._version != null) { 170 return false; 171 } 172 } else if (!_version.equals(other._version)) { 173 return false; 174 } 175 return true; 176 } 177 178 /** 179 * {@inheritDoc} 180 */ 181 @Override 182 public int hashCode() { 183 final int prime = 31; 184 int result = 1; 185 result = prime * result + (_title == null ? 0 : _title.hashCode()); 186 result = prime * result + (_vendor == null ? 0 : _vendor.hashCode()); 187 result = prime * result + (_vendorId == null ? 0 : _vendorId.hashCode()); 188 result = prime * result + (_uri == null ? 0 : _uri.hashCode()); 189 result = prime * result + (_version == null ? 0 : _version.hashCode()); 190 return result; 191 } 192 193 /** 194 * {@inheritDoc} 195 */ 196 @Override 197 public int compareTo(Implementation that) { 198 int comparison = 0; 199 if (this != that) { 200 comparison = compare(_title, that.getTitle()); 201 if (comparison == 0) { 202 comparison = compare(_vendor, that.getVendor()); 203 if (comparison == 0) { 204 comparison = compare(_vendorId, that.getVendorId()); 205 if (comparison == 0) { 206 comparison = compare(toString(_url), toString(that.getURL())); 207 if (comparison == 0) { 208 comparison = compare(_version, that.getVersion()); 209 } 210 } 211 } 212 } 213 } 214 return comparison; 215 } 216 217 private URI toURI(URL url) { 218 if (url != null) { 219 try { 220 return url.toURI(); 221 } catch (URISyntaxException usi) { 222 return null; 223 } 224 } 225 return null; 226 } 227 228 private URL toURL(String url) { 229 if (url != null) { 230 try { 231 return new URL(url); 232 } catch (MalformedURLException mue) { 233 return null; 234 } 235 } 236 return null; 237 } 238 239 private String toString(URL url) { 240 return url != null ? url.toString() : null; 241 } 242 243 }