Source for org.jfree.resourceloader.ResourceKey

   1: /**
   2:  * ================================================
   3:  * LibLoader : a free Java resource loading library
   4:  * ================================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libloader/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: ResourceKey.java 3520 2007-10-16 10:34:47Z tmorgner $
  28:  * ------------
  29:  * (C) Copyright 2006, by Pentaho Corporation.
  30:  */
  31: package org.jfree.resourceloader;
  32: 
  33: import java.io.Serializable;
  34: import java.util.Collections;
  35: import java.util.HashMap;
  36: import java.util.Map;
  37: import java.util.Arrays;
  38: 
  39: /**
  40:  * The key is an unique identifier for the resource. Most of the time,
  41:  * this may be an URL, but other (especially database based) schemas are
  42:  * possible.
  43:  *
  44:  * A resource key must provide an 'equals' implementation. ResourceKeys should
  45:  * be implemented as immutable classes, so that they can be safely stored in
  46:  * collections or on external storages (like caches).
  47:  *
  48:  * @author Thomas Morgner
  49:  */
  50: public final class ResourceKey implements Serializable
  51: {
  52:   private static final Map EMPTY_MAP =
  53:       Collections.unmodifiableMap(new HashMap());
  54: 
  55:   private Map factoryParameters;
  56:   private Integer hashCode;
  57:   private Object schema;
  58:   private Object identifier;
  59:   private ResourceKey parent;
  60: 
  61:   public ResourceKey(final Object schema,
  62:                      final Object identifier,
  63:                      final Map factoryParameters)
  64:   {
  65:     if (schema == null)
  66:     {
  67:       throw new NullPointerException();
  68:     }
  69:     if (identifier == null)
  70:     {
  71:       throw new NullPointerException();
  72:     }
  73: 
  74:     this.schema = schema;
  75:     this.identifier = identifier;
  76:     if (factoryParameters != null)
  77:     {
  78:       this.factoryParameters =
  79:           Collections.unmodifiableMap(new HashMap(factoryParameters));
  80:     }
  81:     else
  82:     {
  83:       this.factoryParameters = EMPTY_MAP;
  84:     }
  85:   }
  86: 
  87:   public ResourceKey(final ResourceKey parent,
  88:                      final Object schema,
  89:                      final Object identifier,
  90:                      final Map factoryParameters)
  91:   {
  92:     this(schema, identifier, factoryParameters);
  93:     this.parent = parent;
  94:   }
  95: 
  96:   public ResourceKey getParent()
  97:   {
  98:     return parent;
  99:   }
 100: 
 101:   public Map getFactoryParameters ()
 102:   {
 103:     return factoryParameters;
 104:   }
 105: 
 106:   public boolean equals(final Object o)
 107:   {
 108:     if (this == o)
 109:     {
 110:       return true;
 111:     }
 112:     if (o == null || getClass() != o.getClass())
 113:     {
 114:       return false;
 115:     }
 116: 
 117:     final ResourceKey that = (ResourceKey) o;
 118: 
 119:     if (!schema.equals(that.schema))
 120:     {
 121:       return false;
 122:     }
 123:     if (!factoryParameters.equals(that.factoryParameters))
 124:     {
 125:       return false;
 126:     }
 127:     if (!identifier.equals(that.identifier))
 128:     {
 129:       if (identifier instanceof byte[] && that.identifier instanceof byte[])
 130:       {
 131:         final byte[] me = (byte[]) identifier;
 132:         final byte[] he = (byte[]) that.identifier;
 133:         Arrays.equals(me, he);
 134:       }
 135:       return false;
 136:     }
 137: 
 138:     return true;
 139:   }
 140: 
 141:   public int hashCode()
 142:   {
 143:     if (hashCode == null)
 144:     {
 145:       int result = factoryParameters.hashCode();
 146:       result = 29 * result + schema.hashCode();
 147:       result = 29 * result + identifier.hashCode();
 148:       hashCode = new Integer(result);
 149:     }
 150:     return hashCode.intValue();
 151:   }
 152: 
 153:   public Object getIdentifier()
 154:   {
 155:     return identifier;
 156:   }
 157: 
 158:   /**
 159:    * Returns the schema of this resource key. The schema is an internal
 160:    * identifier to locate the resource-loader implementation that was
 161:    * responsible for creating the key in the first place.
 162:    *
 163:    * The schema has no meaning outside the resource loading framework.
 164:    *
 165:    * @return
 166:    */
 167:   public Object getSchema ()
 168:   {
 169:     return schema;
 170:   }
 171: 
 172: 
 173:   public String toString()
 174:   {
 175:     return "ResourceKey{" +
 176:            "schema=" + schema +
 177:            ", identifier=" + identifier +
 178:            ", factoryParameters=" + factoryParameters +
 179:            ", parent=" + parent +
 180:            '}';
 181:   }
 182: }
 183: