jclass
Class ResourceHash

java.lang.Object
  |
  +--jclass.ResourceHash

public class ResourceHash
extends java.lang.Object

A class that allows a good maintenance of a set of constants. It basically does nothing more than storing a vector of strings representing the names of the resources and another vector representing the values of the same resources.

Note: The code herein is vvv ugly ...

Written: Radu Sion
Version: 0.18
Source: ResourceHash.java

Visit Smart Software 


Constructor Summary
ResourceHash(SimpleProperties p)
          Creates a resource hash from a properties object.
ResourceHash(java.util.Vector names, java.util.Vector values)
          Creates a new object.
 
Method Summary
static ResourceHash fromFile(java.lang.String filename)
          Reads a resource file and returnes the resources into a ResourceHash for later use from inside the calling program.
 int getBoolean(java.lang.String resname)
          Returns the first occurence of a resource value interpreted as a boolean.
 java.lang.Double getDouble(java.lang.String resname)
          Returns the first occurence of a resource value interpreted as a double.
 double getDoubleValue(java.lang.String resname)
          Returns a double representing the value of the above .getDouble() returned Double object.
 java.lang.Long getLong(java.lang.String resname)
          Returns the first occurence of a resource value interpreted as a Long.
 long getLongValue(java.lang.String resname)
          Returns a long representing the value of the above .getLong() returned Long object.
 java.lang.String getString(java.lang.String resname)
          Returns the first occurence of a resource value treated as a string with the given name.
 java.lang.String HREF="../jclass/ResourceHash.html#set(java.lang.String, java.lang.String)">set(java.lang.String resname, java.lang.String resvalue)
          Sets the named resource value, but only if it already exists here with another value.
 int size()
          Returns the size of this structure, actually the length of the internal arrays that hold the resources.
 SimpleProperties toSimpleProperties()
          Returns a corresponding properties object, containing everything in this resource hash.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceHash

public ResourceHash(java.util.Vector names,
                    java.util.Vector values)
Creates a new object. The sizes of the parameters should be the same although no parameter checkings are performed.
Parameters:
names - A Vector containing the resource names as String objects
values - A Vector containing the resource string values as String objects

ResourceHash

public ResourceHash(SimpleProperties p)
Creates a resource hash from a properties object. Very very ugly code.
Method Detail

toSimpleProperties

public SimpleProperties toSimpleProperties()
Returns a corresponding properties object, containing everything in this resource hash.

size

public int size()
Returns the size of this structure, actually the length of the internal arrays that hold the resources.

set

public java.lang.String set(java.lang.String resname,
                            java.lang.String resvalue)
Sets the named resource value, but only if it already exists here with another value.
Parameters:
resname - Resource name to consider (case senzitive)
resvalue - Resource value to set
Returns:
The old resource value if ok, null if resource not already there.

getString

public java.lang.String getString(java.lang.String resname)
Returns the first occurence of a resource value treated as a string with the given name.
Parameters:
resname - Resource name to consider (case senzitive)
Returns:
The resource value or null if nonexistent or error.

getBoolean

public int getBoolean(java.lang.String resname)
Returns the first occurence of a resource value interpreted as a boolean. A boolean may have the following string values meaning true or false:
 true: ok, enabled, true, on
 false: disabled, false, off
 
Parameters:
resname - Resource name to consider (case senzitive)
Returns:
1 if resource interpreted as true, 0 if false, -1 if error

getLong

public java.lang.Long getLong(java.lang.String resname)
Returns the first occurence of a resource value interpreted as a Long.
Parameters:
resname - Resource name to consider (case senzitive)
Returns:
The value interpreted, null if error

getLongValue

public long getLongValue(java.lang.String resname)
Returns a long representing the value of the above .getLong() returned Long object.
NOTE: If .getLong() returns null then this method returns 0 !!!
Error tracking is difficult.
Parameters:
resname - Resource name to consider
Returns:
A long or 0 if error (may be zero also if no error !!!)

getDouble

public java.lang.Double getDouble(java.lang.String resname)
Returns the first occurence of a resource value interpreted as a double.
Parameters:
resname - Resource name to consider (case senzitive)
Returns:
The value interpreted, null if error

getDoubleValue

public double getDoubleValue(java.lang.String resname)
Returns a double representing the value of the above .getDouble() returned Double object.
NOTE: If .getDouble() returns null then this method returns 0 !!! Error tracking is difficult !!!
Parameters:
resname - Resource name to consider
Returns:
A double or 0 if error (may be zero also if no error !!!)

fromFile

public static ResourceHash fromFile(java.lang.String filename)
Reads a resource file and returnes the resources into a ResourceHash for later use from inside the calling program. Should be useful for modifying the constants of the system without having to recompile.
A resource file looks like this:
 # comment line
 Resource1	valueofresource1
 Resource2    valueofresource2
 Resource3	" string value of resource 3 "
 

The values are treated as simple strings and the overall return should be a ResourceHash. The resourc hash provides good methods of handling the resource names. I wanted to separate the reading part from the real handler (the hash) in order not to overload the system with a big object.

No parameter checkings are performed.
Parameters:
filename - Resource file name
Returns:
A resource hash if ok, null if some error occured