001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.core.util;
025
026 import java.util.Collection;
027 import java.util.HashMap;
028 import java.util.Iterator;
029 import java.util.Map;
030 import java.util.Set;
031
032 import javax.swing.AbstractListModel;
033
034 public class DefaultObservableMap<V> extends AbstractListModel implements ObservableMap<V> {
035 private static final long serialVersionUID = -1811632376295464484L;
036
037 private Map<Object, V> map = null;
038
039 public DefaultObservableMap() {
040 // must be a HashMap, because different object types cannot be compared
041 map = new HashMap<Object, V>(2);
042 }
043
044 public synchronized V getElementAt(int index) {
045 Iterator<V> it = map.values().iterator();
046 for (int i = 0; it.hasNext() && i < index; i++) {
047 it.next();
048 }
049 return it.hasNext() ? it.next() : null;
050 }
051
052 public synchronized V get(Object key) {
053 return map.get(key);
054 }
055
056 public synchronized int indexOf(V value) {
057 Iterator<V> it = map.values().iterator();
058 for (int i = 0; it.hasNext(); i++) {
059 if (it.next().equals(value)) {
060 return i;
061 }
062 }
063 return -1;
064 }
065
066 public synchronized int getSize() {
067 return map.size();
068 }
069
070 public synchronized V put(Object key, V value) {
071 V v = map.put(key, value);
072 int index = indexOf(value);
073 if (v == null) {
074 fireIntervalAdded(this, index, index);
075 } else {
076 fireContentsChanged(this, index, index);
077 }
078 return v;
079 }
080
081 public synchronized V remove(Object key) {
082 int index = indexOf(map.get(key));
083 V v = map.remove(key);
084 if (index >= 0) {
085 fireIntervalRemoved(this, index, index);
086 }
087 return v;
088 }
089
090 @Override
091 public void fireContentsChanged() {
092 fireContentsChanged(this, -1, -1);
093 }
094
095 public synchronized Iterator<V> iterator() {
096 return map.values().iterator();
097 }
098
099 public boolean isEmpty() {
100 return map.isEmpty();
101 }
102
103 public void clear() {
104 int size = map.size();
105 map.clear();
106 fireIntervalRemoved(this, 0, size - 1);
107 }
108
109 public Collection<V> toCollection() {
110 return map.values();
111 }
112
113 public Set<Object> keySet() {
114 return map.keySet();
115 }
116
117 @Override
118 public boolean containsKey(Object key) {
119 return map.containsKey(key);
120 }
121
122 @Override
123 public boolean containsValue(Object value) {
124 return map.containsValue(value);
125 }
126
127 @Override
128 public Set<java.util.Map.Entry<Object, V>> entrySet() {
129 return map.entrySet();
130 }
131
132 @Override
133 public void putAll(Map<? extends Object, ? extends V> m) {
134 for (Object k : m.keySet()) {
135 V v = m.get(k);
136 put(k, v);
137 }
138 }
139
140 @Override
141 public int size() {
142 return map.size();
143 }
144
145 @Override
146 public Collection<V> values() {
147 return map.values();
148 }
149
150 public String toString() {
151 return map.toString();
152 }
153
154 }