001 /* 002 * Copyright 2005 Stephen J. McConnell 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.library.info; 020 021 import java.io.Serializable; 022 import java.util.Properties; 023 024 /** 025 * The ModuleDirective class describes a module data-structure. 026 * 027 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 028 * @version 1.1.2 029 */ 030 public abstract class AbstractDirective implements Serializable 031 { 032 private Properties m_properties; 033 034 /** 035 * Creation of a new abstract directive. 036 */ 037 public AbstractDirective() 038 { 039 this( null ); 040 } 041 042 /** 043 * Creation of a new abstract directive. 044 * @param properties the properties associated with the directive 045 */ 046 public AbstractDirective( Properties properties ) 047 { 048 if( null == properties ) 049 { 050 m_properties = new Properties(); 051 } 052 else 053 { 054 m_properties = properties; 055 } 056 } 057 058 /** 059 * Return a property value. 060 * @param key the property key 061 * @return the property value 062 */ 063 public String getProperty( String key ) 064 { 065 return m_properties.getProperty( key ); 066 } 067 068 /** 069 * Return a property set. 070 * @return the properties 071 */ 072 public Properties getProperties() 073 { 074 return m_properties; 075 } 076 077 /** 078 * Compare this object to the supplied object for equality. 079 * @param other the other object 080 * @return true if equal 081 */ 082 public boolean equals( Object other ) 083 { 084 if( null == other ) 085 { 086 return false; 087 } 088 else 089 { 090 return ( other instanceof AbstractDirective ); 091 } 092 } 093 094 /** 095 * Calculate the hashcode. 096 * @return the hashcode value 097 */ 098 public int hashCode() 099 { 100 return getClass().hashCode(); 101 } 102 103 /** 104 * Utility to hash an array. 105 * @param array the array 106 * @return the hash value 107 */ 108 protected int hashArray( Object[] array ) 109 { 110 if( null == array ) 111 { 112 return 0; 113 } 114 int hash = 0; 115 for( int i=0; i<array.length; i++ ) 116 { 117 Object object = array[i]; 118 hash ^= hashValue( object ); 119 } 120 return hash; 121 } 122 123 /** 124 * Utility to hash an object. 125 * @param value the object 126 * @return the hash value 127 */ 128 protected int hashValue( Object value ) 129 { 130 if( null == value ) 131 { 132 return 0; 133 } 134 else if( value instanceof Object[] ) 135 { 136 return hashArray( (Object[]) value ); 137 } 138 else 139 { 140 return value.hashCode(); 141 } 142 } 143 144 /** 145 * Utility to compare two object for equality. 146 * @param a the first object 147 * @param b the second object 148 * @return true if the objects are equal 149 */ 150 protected boolean equals( Object a, Object b ) 151 { 152 if( null == a ) 153 { 154 return ( null == b ); 155 } 156 else 157 { 158 return a.equals( b ); 159 } 160 } 161 }