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.mapmatrix; 025 026 import java.util.Collection; 027 import java.util.Iterator; 028 import java.util.List; 029 import java.util.Map; 030 import java.util.Set; 031 032 import org.ujmp.core.objectmatrix.stub.AbstractDenseObjectMatrix2D; 033 034 public abstract class AbstractMapMatrix<K, V> extends AbstractDenseObjectMatrix2D implements 035 MapMatrix<K, V> { 036 private static final long serialVersionUID = 5571429371462164416L; 037 038 public abstract Map<K, V> getMap(); 039 040 public final long[] getSize() { 041 return new long[] { size(), 2 }; 042 } 043 044 public final Object getObject(long row, long column) { 045 return getObject((int) row, (int) column); 046 } 047 048 public final Object getObject(int row, int column) { 049 Object mapKey = getKey(row); 050 if (column == 0) { 051 return mapKey; 052 } else if (column == 1) { 053 return (mapKey == null ? null : getMap().get(mapKey)); 054 } else { 055 return null; 056 } 057 } 058 059 public final void setObject(Object key, long row, long column) { 060 } 061 062 public final void setObject(Object key, int row, int column) { 063 } 064 065 public abstract MapMatrix<K, V> copy(); 066 067 // TODO: concurrentmodification exceptions can come from here 068 @SuppressWarnings("unchecked") 069 private final Object getKey(int index) { 070 if (getMap() instanceof List) { 071 return ((List) getMap()).get(index); 072 } 073 Iterator<K> it = keySet().iterator(); 074 for (int i = 0; it.hasNext() && i < index; i++) { 075 it.next(); 076 } 077 return it.hasNext() ? it.next() : null; 078 } 079 080 public final boolean containsKey(Object key) { 081 return getMap().containsKey(key); 082 } 083 084 public final boolean containsValue(Object value) { 085 return getMap().containsValue(value); 086 } 087 088 public final Set<java.util.Map.Entry<K, V>> entrySet() { 089 return getMap().entrySet(); 090 } 091 092 public final V get(Object key) { 093 return getMap().get(key); 094 } 095 096 public final boolean isEmpty() { 097 return getMap().isEmpty(); 098 } 099 100 public final Set<K> keySet() { 101 return getMap().keySet(); 102 } 103 104 public final V put(K key, V value) { 105 V v = getMap().put(key, value); 106 notifyGUIObject(); 107 return v; 108 } 109 110 public final void putAll(Map<? extends K, ? extends V> m) { 111 getMap().putAll(m); 112 notifyGUIObject(); 113 } 114 115 public final V remove(Object key) { 116 V v = getMap().remove(key); 117 notifyGUIObject(); 118 return v; 119 } 120 121 public final int size() { 122 return getMap().size(); 123 } 124 125 public final Collection<V> values() { 126 return getMap().values(); 127 } 128 129 public final StorageType getStorageType() { 130 return StorageType.MAP; 131 } 132 133 public final void clear() { 134 getMap().clear(); 135 } 136 137 }