001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP 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     * UJMP 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 UJMP; 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.ujmp.core.collections;
025    
026    import java.io.IOException;
027    import java.io.ObjectInputStream;
028    import java.io.ObjectOutputStream;
029    import java.io.OptionalDataException;
030    import java.io.Serializable;
031    import java.util.AbstractCollection;
032    import java.util.AbstractSet;
033    import java.util.Collection;
034    import java.util.Iterator;
035    import java.util.Map;
036    import java.util.Set;
037    
038    import org.ujmp.core.exceptions.MatrixException;
039    
040    public abstract class AbstractMap<K, V> extends java.util.AbstractMap<K, V> implements Serializable {
041            private static final long serialVersionUID = -6429342188863787235L;
042    
043            public boolean isEmpty() {
044                    return size() == 0;
045            }
046    
047            public void putAll(Map<? extends K, ? extends V> map) {
048                    for (K k : map.keySet()) {
049                            put(k, map.get(k));
050                    }
051            }
052    
053            public boolean containsKey(Object key) {
054                    return keySet().contains(key);
055            }
056    
057            public boolean containsValue(Object value) {
058                    for (K key : keySet()) {
059                            if (value.equals(get(key))) {
060                                    return true;
061                            }
062                    }
063                    return false;
064    
065            }
066    
067            public Collection<V> values() {
068                    return new AbstractCollection<V>() {
069    
070                            @Override
071                            public Iterator<V> iterator() {
072                                    return new Iterator<V>() {
073    
074                                            Iterator<K> it = keySet().iterator();
075    
076                                            public boolean hasNext() {
077                                                    return it.hasNext();
078                                            }
079    
080                                            public V next() {
081                                                    return get(it.next());
082                                            }
083    
084                                            public void remove() {
085                                                    throw new MatrixException("not implemented");
086                                            }
087                                    };
088                            }
089    
090                            @Override
091                            public int size() {
092                                    return size();
093                            }
094                    };
095            }
096    
097            public Set<java.util.Map.Entry<K, V>> entrySet() {
098                    return new AbstractSet<Entry<K, V>>() {
099    
100                            @Override
101                            public Iterator<java.util.Map.Entry<K, V>> iterator() {
102                                    return new Iterator<Entry<K, V>>() {
103    
104                                            Iterator<K> it = keySet().iterator();
105    
106                                            public boolean hasNext() {
107                                                    return it.hasNext();
108                                            }
109    
110                                            public java.util.Map.Entry<K, V> next() {
111                                                    final K k = it.next();
112                                                    final V v = get(k);
113                                                    return new java.util.Map.Entry<K, V>() {
114    
115                                                            public K getKey() {
116                                                                    return k;
117                                                            }
118    
119                                                            public V getValue() {
120                                                                    return v;
121                                                            }
122    
123                                                            public V setValue(V value) {
124                                                                    throw new MatrixException("not implemented");
125                                                            }
126                                                    };
127                                            }
128    
129                                            public void remove() {
130                                                    throw new MatrixException("not implemented");
131                                            }
132                                    };
133                            }
134    
135                            @Override
136                            public int size() {
137                                    return size();
138                            }
139                    };
140            }
141    
142            @SuppressWarnings("unchecked")
143            private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
144                    s.defaultReadObject();
145                    int size = s.readInt();
146                    for (int i = 0; i < size; i++) {
147                            try {
148                                    K k = (K) s.readObject();
149                                    V v = (V) s.readObject();
150                                    put(k, v);
151                            } catch (OptionalDataException e) {
152                                    return;
153                            }
154                    }
155            }
156    
157            private void writeObject(ObjectOutputStream s) throws IOException, MatrixException {
158                    s.defaultWriteObject();
159                    s.writeInt(size());
160                    for (Object k : keySet()) {
161                            Object v = get(k);
162                            s.writeObject(k);
163                            s.writeObject(v);
164                    }
165            }
166    
167            public abstract void clear();
168    
169            public abstract V get(Object key);
170    
171            public abstract Set<K> keySet();
172    
173            public abstract V put(K key, V value);
174    
175            public abstract V remove(Object key);
176    
177            public abstract int size();
178    
179    }