1 50 51 package org.apache.avalon.meta.info; 52 53 import java.io.Serializable ; 54 55 import org.apache.avalon.framework.Version; 56 57 67 public final class ReferenceDescriptor 68 implements Serializable 69 { 70 73 private final String m_classname; 74 75 78 private final Version m_version; 79 80 86 public ReferenceDescriptor( final String type ) throws NullPointerException 87 { 88 this( parseClassname(type), parseVersion(type) ); 89 } 90 91 99 public ReferenceDescriptor( final String classname, 100 final Version version ) throws NullPointerException 101 { 102 if ( null == classname ) 103 { 104 throw new NullPointerException ( "classname" ); 105 } 106 if( classname.equals( "" ) ) 107 { 108 throw new IllegalArgumentException ( "classname" ); 109 } 110 if( classname.indexOf( "/" ) > -1 ) 111 { 112 throw new IllegalArgumentException ( "classname" ); 113 } 114 115 m_classname = classname; 116 117 if ( null == version ) 118 { 119 m_version = Version.getVersion( "" ); 120 } 121 else 122 { 123 m_version = version; 124 } 125 } 126 127 132 public String getClassname() 133 { 134 return m_classname; 135 } 136 137 142 public Version getVersion() 143 { 144 return m_version; 145 } 146 147 154 public boolean matches( final ReferenceDescriptor other ) 155 { 156 return m_classname.equals( other.m_classname ) 157 && other.getVersion().complies( getVersion() ); 158 } 159 160 165 public String toString() 166 { 167 return getClassname() + ":" + getVersion(); 168 } 169 170 176 public boolean equals( Object other ) 177 { 178 boolean match = false; 179 180 186 if ( other instanceof ReferenceDescriptor ) 187 { 188 match = ( (ReferenceDescriptor) other ).matches( this ); 189 } 190 else if ( other instanceof Service ) 191 { 192 match = ( (Service) other ).matches( this ); 193 } 194 else if ( other instanceof ServiceDescriptor ) 195 { 196 match = ( (ServiceDescriptor) other ).getReference().matches( this ); 197 } 198 199 return match; 200 } 201 202 206 public int hashCode() 207 { 208 return getClassname().hashCode() ^ getVersion().hashCode(); 209 } 210 211 private static final String parseClassname( final String type ) 212 { 213 if( type == null ) throw new NullPointerException ( "type" ); 214 215 int index = type.indexOf( ":" ); 216 if( index == -1 ) 217 { 218 return type; 219 } 220 else 221 { 222 return type.substring( 0, index ); 223 } 224 } 225 226 private static final Version parseVersion( final String type ) 227 { 228 if( type.indexOf( ":" ) == -1 ) 229 { 230 return Version.getVersion( "" ); 231 } 232 else 233 { 234 return Version.getVersion( type.substring( getColonIndex( type ) + 1) ); 235 } 236 } 237 238 private static final int getColonIndex( final String type ) 239 { 240 if ( null == type ) throw new NullPointerException ( "type" ); 241 return Math.min( type.length(), Math.max( 0, type.indexOf( ":" ) ) ); 242 } 243 } 244 | Popular Tags |