001: package simpleorm.properties;
002:
003: import java.util.Hashtable;
004: import java.util.Enumeration;
005:
006: /*
007: * Copyright (c) 2002 Southern Cross Software Limited (SCSQ). All rights
008: * reserved. See COPYRIGHT.txt included in this distribution.
009: */
010:
011: /** A property map similar to java.util.Propeties except that the keys
012: * are SProperty objects, and inheritance is supported.<p>
013: *
014: * Note that the map is SProperty --> Object directly.
015: * SPropertyValues instances are not stored directly in this map, they
016: * are picked appart into propety and value which are stored
017: * separately. ## Should they be?
018: */
019: public class SPropertyMap {
020:
021: Hashtable hashMap = new Hashtable();
022:
023: /** Gets the value of prop for this map. May return a default value
024: if none has been explicitly set.*/
025: public Object getProperty(SProperty prop) {
026: Object val = prop.theValue(this );
027: if (val != null)
028: return val;
029: if (hashMap.containsKey(prop))
030: return hashMap.get(prop);
031: else
032: return prop.defaultValue(this );
033: }
034:
035: /** Convenient routine for getting boolean properties. Default is
036: always false. */
037: public boolean getBoolean(SProperty prop) {
038: Object val = getProperty(prop);
039: if (val == null)
040: return false;
041: return ((Boolean) val).booleanValue();
042: }
043:
044: /** (String)getProperty(..). */
045: public String getString(SProperty prop) {
046: return (String) getProperty(prop);
047: }
048:
049: /** Sets prop to value for this map after validating it. May or may
050: not already exist. If value is null then removes the property,
051: so a property cannot actually be set to null -- once removed it
052: may start defaulting. */
053: public void putProperty(SProperty prop, Object value) {
054: //System.out.println(" -putting " + this + "." + prop + " := " + value);
055: prop.validate(this , value);
056: if (value == null) // Needs to be a special case!
057: hashMap.remove(prop);
058: else
059: hashMap.put(prop, value);
060: }
061:
062: /** Sets prop to value for this map provided that it does not
063: already have an <em>explicit</em> value in <em>this</em> map.
064: Either way returns the value of the property now.
065: */
066: public Object putDefaultProperty(SProperty prop, Object value) {
067: Object val = hashMap.get(prop);
068: if (val == null) {
069: val = value;
070: putProperty(prop, value);
071: }
072: return val;
073: }
074:
075: /** Convenience method that sets this map to each property value pair. */
076: public void setPropertyValues(SPropertyValue[] pvals) {
077: for (int px = 0; px < pvals.length; px++) {
078: setPropertyValue(pvals[px]);
079: }
080: }
081:
082: /** Convenience method that sets this mmap to this property value pair. */
083: public void setPropertyValue(SPropertyValue pval) {
084: putProperty(pval.property, pval.value);
085: }
086:
087: /** Removes the value in this Map only. It may still be inheritable
088: from other maps or have a default value. This is quite
089: different from setting the value to null. */
090: public void remove(SProperty prop) {
091: hashMap.remove(prop);
092: ;
093: }
094:
095: /** ## Used to create foreign keys. But should really be smarter and
096: use inheritance. But then multiple inheritiance issues need to
097: be resolved.
098: public SPropertyValue [] cloneMap() {
099: SPropertyValue [] pvals = new SPropertyValue[hashMap.size()];
100: int px=0;
101: for (Enumeration keys = hashMap.keys();
102: keys.hasMoreElements(); px++) {
103: SProperty prop = (SProperty)keys.nextElement();
104: pvals[px]=new SPropertyValue(prop, hashMap.get(prop));
105: }
106: return pvals;
107: }
108: */
109: }
|