001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.content;
019:
020: import java.util.Collections;
021: import java.util.List;
022: import java.util.Vector;
023:
024: /**
025: * Works similiar to a map, but with the difference that there may be multiple
026: * values for a single key.
027: */
028: public class Metadata {
029:
030: protected Vector keys;
031:
032: protected Vector values;
033:
034: protected boolean modified;
035:
036: public Metadata() {
037: keys = new Vector();
038: values = new Vector();
039: }
040:
041: private Metadata(List keys, List values) {
042: this .keys = new Vector(keys);
043: this .values = new Vector(values);
044: }
045:
046: public synchronized Object clone() {
047: return new Metadata(keys, values);
048: }
049:
050: /**
051: * This should only be used to set the modification state to false after the
052: * metadata has been read or written by the persistence layer.
053: */
054: public synchronized void setModified(boolean modified) {
055: this .modified = modified;
056: }
057:
058: public synchronized boolean isModified() {
059: return modified;
060: }
061:
062: public synchronized List getKeys() {
063: return Collections.unmodifiableList(keys);
064: }
065:
066: public synchronized List getValues(String key) {
067: int index = keys.indexOf(key);
068: List results = new Vector();
069: while (index != -1) {
070: results.add(values.get(index));
071: index = keys.indexOf(key, index + 1);
072: }
073: return results;
074: }
075:
076: public synchronized void add(String key, String value) {
077: // Ignore duplicate entries
078: if (!containsPair(key, value)) {
079: modified = true;
080: keys.add(key);
081: values.add(value);
082: }
083: }
084:
085: public synchronized void remove(String key, String value) {
086: modified = true;
087: int keyIndex = keys.indexOf(key);
088: while (keyIndex != -1) {
089: if (values.get(keyIndex).equals(value)) {
090: keys.remove(keyIndex);
091: values.remove(keyIndex);
092: keyIndex = keys.indexOf(key, keyIndex);
093: } else {
094: keyIndex = keys.indexOf(key, keyIndex + 1);
095: }
096: }
097: }
098:
099: public synchronized void removeAll() {
100: modified = true;
101: keys = new Vector();
102: values = new Vector();
103: }
104:
105: protected synchronized boolean containsPair(String key, String value) {
106: int keyIndex = keys.indexOf(key);
107: while (keyIndex != -1) {
108: if (values.get(keyIndex).equals(value)) {
109: // We found this key/value pair contained in this object already
110: return true;
111: } else {
112: keyIndex = keys.indexOf(key, keyIndex + 1);
113: }
114: }
115: return false;
116: }
117:
118: }
|