VolD 0.1

Key.java

Go to the documentation of this file.
00001 package de.zib.vold.common;
00002 
00003 import java.util.List;
00004 import java.util.LinkedList;
00005 
00006 import java.net.URI;
00007 import java.net.URISyntaxException;
00008 
00009 import org.apache.commons.lang3.StringEscapeUtils;
00010 
00019 public class Key
00020 {
00028         public Key( String scope, String type, String keyname )
00029                 throws IllegalArgumentException
00030         {
00031                 // normalize path
00032                 {
00033                         URI uri;
00034                         try
00035                         {
00036                                 uri = new URI( scope );
00037                         }
00038                         catch( URISyntaxException e )
00039                         {
00040                                 throw new IllegalArgumentException( "Scope (\"" + scope + "\") for a key must be a valid UNIX-Style path. " + e.getMessage() );
00041                         }
00042 
00043                         scope = new String( uri.normalize().getPath() );
00044                 }
00045 
00046                 // check for "/" at beginning and end (and add it if not present)
00047                 {
00048                         if( 0 == scope.length() )
00049                         {
00050                                 scope = "/";
00051                         }
00052                         else
00053                         {
00054                                 if( ! scope.substring( 0, 1 ).equals( "/" ) )
00055                                 {
00056                                         scope = "/" + scope;
00057                                 }
00058                                 if( ! scope.substring( scope.length()-1, scope.length() ).equals( "/" ) )
00059                                 {
00060                                         scope = scope + "/";
00061                                 }
00062                         }
00063                 }
00064 
00065                 this.scope = scope;
00066                 this.type = type;
00067                 this.keyname = keyname;
00068         }
00069 
00075         public String get_scope( )
00076         {
00077                 return scope;
00078         }
00079 
00085         public String get_type( )
00086         {
00087                 return type;
00088         }
00089 
00095         public String get_keyname( )
00096         {
00097                 return keyname;
00098         }
00099 
00105         public String toString( )
00106         {
00107                 return this._buildkey().toString();
00108         }
00109 
00116         public static Key buildkey( List< String > key )
00117                 throws IllegalArgumentException
00118         {
00119                 if( key.size() < 3 )
00120                 {
00121                         throw new IllegalArgumentException( "Tried to build a key out of " + key.size() + " arguments. At leest three (scope, type, keyname) of them are necessary." );
00122                 }
00123 
00124                 return new Key( key.get( 0 ), key.get( 1 ), key.get( 2 ) );
00125         }
00126 
00132         public List< String > _buildkey( )
00133         {
00134                 List< String > key = new LinkedList< String >();
00135 
00136                 key.add( scope );
00137                 key.add( type );
00138                 key.add( keyname );
00139 
00140                 return key;
00141         }
00142 
00148         public boolean equals( Object obj )
00149         {
00150                 // same instance
00151                 if( this == obj )
00152                 {
00153                         return true;
00154                 }
00155                 else if( obj instanceof Key )
00156                 {
00157                         Key key = this.getClass().cast( obj );
00158                         return (
00159                                 key.get_scope().equals( this.get_scope() ) &&
00160                                 key.get_type().equals( this.get_type() ) &&
00161                                 key.get_keyname().equals( this.get_keyname() )
00162                                 );
00163                 }
00164                 else
00165                 {
00166                         return false;
00167                 }
00168         }
00169 
00175         public int hashCode( )
00176         {
00177                 return  get_scope().hashCode() +
00178                         463*get_type().hashCode() +
00179                         971*get_keyname().hashCode();
00180         }
00181 
00182         private final String scope;
00183         private final String type;
00184         private final String keyname;
00185 }