001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.entities.configuration.internal;
014:
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Locale;
018:
019: import org.rapla.components.util.iterator.FilterIterator;
020: import org.rapla.components.util.iterator.NestedIterator;
021: import org.rapla.entities.EntityNotFoundException;
022: import org.rapla.entities.RaplaObject;
023: import org.rapla.entities.RaplaType;
024: import org.rapla.entities.configuration.Preferences;
025: import org.rapla.entities.dynamictype.DynamicType;
026: import org.rapla.entities.storage.DynamicTypeDependant;
027: import org.rapla.entities.storage.EntityReferencer;
028: import org.rapla.entities.storage.EntityResolver;
029: import org.rapla.entities.storage.Mementable;
030: import org.rapla.entities.storage.RefEntity;
031: import org.rapla.entities.storage.internal.SimpleEntity;
032:
033: public class PreferencesImpl extends SimpleEntity implements
034: Preferences, DynamicTypeDependant, Mementable,
035: java.io.Serializable {
036: // Don't forget to increase the serialVersionUID when you change the fields
037: private static final long serialVersionUID = 1;
038:
039: HashMap map = new HashMap();
040:
041: final public RaplaType getRaplaType() {
042: return TYPE;
043: }
044:
045: public void putEntry(String role, RaplaObject entry) {
046: checkWritable();
047: if (entry == null) {
048: map.remove(role);
049: } else {
050: map.put(role, entry);
051: }
052: }
053:
054: public void putEntry(String role, String entry) {
055: checkWritable();
056: if (entry == null) {
057: map.remove(role);
058: } else {
059: map.put(role, entry);
060: }
061: }
062:
063: public void resolveEntities(EntityResolver resolver)
064: throws EntityNotFoundException {
065: super .resolveEntities(resolver);
066: for (Iterator it = getEntityReferencers(); it.hasNext();) {
067: Object obj = it.next();
068: ((EntityReferencer) obj).resolveEntities(resolver);
069: }
070: }
071:
072: public Object getEntry(String role) {
073: return map.get(role);
074: }
075:
076: public boolean hasEntry(String role) {
077: return map.get(role) != null;
078: }
079:
080: public String getEntryAsString(String role) {
081: return (String) map.get(role);
082: }
083:
084: public String getEntryAsString(String role, String defaultValue) {
085: String value = getEntryAsString(role);
086: if (value != null)
087: return value;
088: return defaultValue;
089: }
090:
091: public Iterator getPreferenceEntries() {
092: return map.keySet().iterator();
093: }
094:
095: private Iterator getEntityReferencers() {
096: return new FilterIterator(map.values().iterator()) {
097: protected boolean isInIterator(Object obj) {
098: return obj instanceof EntityReferencer;
099: }
100: };
101: }
102:
103: public Iterator getReferences() {
104: return new NestedIterator(getEntityReferencers()) {
105: public Iterator getNestedIterator(Object obj) {
106: return ((EntityReferencer) obj).getReferences();
107: }
108: };
109: }
110:
111: public boolean isRefering(RefEntity object) {
112: for (Iterator it = getEntityReferencers(); it.hasNext();) {
113: if (((EntityReferencer) it.next()).isRefering(object)) {
114: return true;
115: }
116: }
117: return false;
118: }
119:
120: public boolean isEmpty() {
121: return map.keySet().isEmpty();
122: }
123:
124: static private void copy(PreferencesImpl source,
125: PreferencesImpl dest) {
126: dest.map.clear();
127: for (Iterator it = source.map.keySet().iterator(); it.hasNext();) {
128: String role = (String) it.next();
129: Object entry = source.map.get(role);
130: dest.map.put(role, entry);
131: }
132: }
133:
134: public void copy(Object obj) {
135: super .copy((PreferencesImpl) obj);
136: copy((PreferencesImpl) obj, this );
137: }
138:
139: public Object deepClone() {
140: PreferencesImpl clone = new PreferencesImpl();
141: super .deepClone(clone);
142: copy(this , clone);
143: return clone;
144: }
145:
146: public Object clone() {
147: PreferencesImpl clone = new PreferencesImpl();
148: super .clone(clone);
149: copy(this , clone);
150: return clone;
151: }
152:
153: /**
154: * @see org.rapla.entities.Named#getName(java.util.Locale)
155: */
156: public String getName(Locale locale) {
157: StringBuffer buf = new StringBuffer();
158: if (getOwner() != null) {
159: buf.append("Preferences of ");
160: buf.append(getOwner().getName(locale));
161: } else {
162: buf.append("Rapla Preferences!");
163: }
164: return buf.toString();
165: }
166:
167: /* (non-Javadoc)
168: * @see org.rapla.entities.configuration.Preferences#getEntryAsBoolean(java.lang.String, boolean)
169: */
170: public boolean getEntryAsBoolean(String role, boolean defaultValue) {
171: String entry = getEntryAsString(role);
172: if (entry == null)
173: return defaultValue;
174: return Boolean.valueOf(entry).booleanValue();
175: }
176:
177: /* (non-Javadoc)
178: * @see org.rapla.entities.configuration.Preferences#getEntryAsInteger(java.lang.String, int)
179: */
180: public int getEntryAsInteger(String role, int defaultValue) {
181: String entry = getEntryAsString(role);
182: if (entry == null)
183: return defaultValue;
184: return Integer.parseInt(entry);
185: }
186:
187: public boolean needsChange(DynamicType type) {
188: for (Iterator it = map.values().iterator(); it.hasNext();) {
189: Object obj = it.next();
190: if (obj instanceof DynamicTypeDependant) {
191: if (((DynamicTypeDependant) obj).needsChange(type))
192: return true;
193: }
194: }
195: return false;
196: }
197:
198: public void commitChange(DynamicType type) {
199: for (Iterator it = map.values().iterator(); it.hasNext();) {
200: Object obj = it.next();
201: if (obj instanceof DynamicTypeDependant) {
202: ((DynamicTypeDependant) obj).commitChange(type);
203: }
204: }
205: }
206:
207: }
|