001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.meta;
010:
011: import java.util.Iterator;
012: import java.util.LinkedHashMap;
013: import java.util.Map;
014:
015: /**
016: * @author Gennady Krizhevsky
017: */
018: public class MetaObjectModel {
019:
020: public static final String TAG_OBJECTS_REFERENCES = "objectsReferences";
021:
022: private LinkedHashMap objectsReferences = new LinkedHashMap(); // LinkedHashMap<MetaObjectReference>
023:
024: public MetaObjectModel() {
025: }
026:
027: public MetaObjectModel(Map map) {
028: fromMap(map);
029: }
030:
031: public MetaObjectReference getMetaObjectReference(String name) {
032: return (MetaObjectReference) objectsReferences.get(name);
033: }
034:
035: public void setMetaObjectReference(String name,
036: MetaObjectReference reference) {
037: objectsReferences.put(name, reference);
038: }
039:
040: public MetaObjectReferenceIterator iterator() {
041: return new MetaObjectReferenceIterator(objectsReferences);
042: }
043:
044: public Map toMap() {
045: Map map = new LinkedHashMap();
046: for (Iterator it = objectsReferences.keySet().iterator(); it
047: .hasNext();) {
048: String key = (String) it.next();
049: MetaObjectReference value = (MetaObjectReference) map
050: .get(key);
051: map.put(key, value.toMap());
052: }
053: Map super Map = new LinkedHashMap();
054: super Map.put(TAG_OBJECTS_REFERENCES, map);
055: return map;
056: }
057:
058: public void fromMap(Map map) {
059: Map refs = (Map) map.get(TAG_OBJECTS_REFERENCES);
060: for (Iterator it = refs.keySet().iterator(); it.hasNext();) {
061: Object key = null;
062: try {
063: key = it.next();
064: Map value = (Map) refs.get(key);
065: String keyName = (String) key;
066: objectsReferences.put(keyName, new MetaObjectReference(
067: value));
068: } catch (Exception e) {
069: throw new RuntimeException("Wrong value by key " + key,
070: e);
071: }
072: }
073: }
074:
075: public static class MetaObjectReferenceIterator implements Iterator {
076:
077: private Iterator iterator;
078: private String key;
079:
080: MetaObjectReferenceIterator(Map objectsReferences) {
081: iterator = objectsReferences.entrySet().iterator();
082: }
083:
084: public boolean hasNext() {
085: return iterator.hasNext();
086: }
087:
088: public Object next() {
089: return iterator.next();
090: }
091:
092: public MetaObjectReference nextMetaObjectReference() {
093: Map.Entry entry = (Map.Entry) iterator.next();
094: this .key = (String) entry.getKey();
095: return (MetaObjectReference) entry.getValue();
096: }
097:
098: public String getKey() {
099: return key;
100: }
101:
102: public void remove() {
103: throw new UnsupportedOperationException();
104: }
105: }
106:
107: }
|