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.sample;
025
026 import java.util.Collection;
027 import java.util.Iterator;
028 import java.util.Map;
029
030 import javax.swing.AbstractListModel;
031
032 import org.jdmp.core.util.DefaultObservableMap;
033 import org.jdmp.core.util.ObservableList;
034 import org.jdmp.core.util.ObservableMap;
035 import org.jdmp.core.variable.AbstractVariable;
036 import org.jdmp.core.variable.Variable;
037 import org.jdmp.core.variable.VariableFactory;
038 import org.ujmp.core.Matrix;
039 import org.ujmp.core.coordinates.Coordinates;
040 import org.ujmp.core.exceptions.MatrixException;
041 import org.ujmp.core.interfaces.Wrapper;
042 import org.ujmp.core.util.MathUtil;
043
044 public class MapSample extends AbstractSample implements Wrapper<Map<?, ?>> {
045 private static final long serialVersionUID = -8219872482909787192L;
046
047 private VariableMapWrapper variableMapWrapper = null;
048
049 private Map<?, ?> map = null;
050
051 public MapSample(Map<?, ?> map) {
052 this.map = map;
053 }
054
055 @Override
056 public final MapSample clone() {
057 MapSample s = null;
058 try {
059 s = this.getClass().newInstance();
060 } catch (Exception e) {
061 e.printStackTrace();
062 }
063 for (Object k : getVariables().keySet()) {
064 Variable v = getVariables().get(k);
065 s.getVariables().put(k, v.clone());
066 }
067 return s;
068 }
069
070 @Override
071 public ObservableMap<Variable> getVariables() {
072 if (variableMapWrapper == null) {
073 variableMapWrapper = new VariableMapWrapper(map);
074 }
075 return variableMapWrapper;
076 }
077
078 public void setMatrix(Object variableKey, Matrix matrix) {
079 Variable v = getVariables().get(variableKey);
080 if (v == null) {
081 v = VariableFactory.labeledVariable(variableKey.toString());
082 getVariables().put(variableKey, v);
083 }
084 v.addMatrix(matrix);
085 }
086
087 class VariableMapWrapper extends DefaultObservableMap<Variable> {
088 private static final long serialVersionUID = 1252960592502010150L;
089
090 public VariableMapWrapper(Map<?, ?> map) {
091 for (Object key : map.keySet()) {
092 Variable v = new VariableWrapper(map, key);
093 this.put(key, v);
094 }
095 }
096 }
097
098 class VariableWrapper extends AbstractVariable {
099 private static final long serialVersionUID = -6210738091770692066L;
100
101 private Map<?, ?> map = null;
102
103 private Object key = null;
104
105 private ObservableList<Matrix> matrixList = null;
106
107 public VariableWrapper(Map<?, ?> map, Object key) {
108 this.map = map;
109 this.key = key;
110 }
111
112 public Variable clone() {
113 Variable v = VariableFactory.labeledVariable(getLabel());
114 for (Matrix m : getMatrixList()) {
115 v.getMatrixList().add(m.copy());
116 }
117 return v;
118 }
119
120 @Override
121 public synchronized Matrix getAsMatrix() {
122 return MathUtil.getMatrix(map.get(key));
123 }
124
125 @Override
126 public synchronized long[] getSize() {
127 Matrix m = getMatrix();
128 return m == null ? Coordinates.ZERO2D : m.getSize();
129 }
130
131 @Override
132 public void setSize(long... size) {
133
134 }
135
136 @Override
137 public String getLabel() {
138 return "" + key;
139 }
140
141 @Override
142 public void setLabel(String label) {
143 }
144
145 @Override
146 public String getDescription() {
147 return null;
148 }
149
150 @Override
151 public void setDescription(String description) {
152 }
153
154 @Override
155 public synchronized ObservableList<Matrix> getMatrixList() {
156 if (matrixList == null) {
157 matrixList = new SingletonObservableList(map, key);
158 }
159 return matrixList;
160 }
161 }
162
163 class SingletonObservableList extends AbstractListModel implements ObservableList<Matrix> {
164 private static final long serialVersionUID = 5626812327084186535L;
165
166 private Map<?, ?> map = null;
167
168 private Object key = null;
169
170 public SingletonObservableList(Map<?, ?> map, Object key) {
171 this.map = map;
172 this.key = key;
173 }
174
175 @Override
176 public void add(Matrix value) {
177 ((Map<Object, Object>) map).put(key, value);
178 }
179
180 @Override
181 public void addAll(Collection<Matrix> values) {
182 throw new MatrixException("not implemented");
183 }
184
185 @Override
186 public void fireContentsChanged() {
187 fireContentsChanged(this, -1, -1);
188 }
189
190 @Override
191 public boolean remove(Matrix value) {
192 throw new MatrixException("not implemented");
193 }
194
195 @Override
196 public void clear() {
197 throw new MatrixException("not implemented");
198 }
199
200 @Override
201 public Matrix getElementAt(int index) {
202 return MathUtil.getMatrix(map.get(key));
203 }
204
205 @Override
206 public int indexOf(Matrix value) {
207 Matrix m = MathUtil.getMatrix(map.get(key));
208 return m == value ? 0 : -1;
209 }
210
211 @Override
212 public boolean isEmpty() {
213 return false;
214 }
215
216 @Override
217 public Collection<Matrix> toCollection() {
218 throw new MatrixException("not implemented");
219 }
220
221 @Override
222 public int getSize() {
223 return 1;
224 }
225
226 @Override
227 public Iterator<Matrix> iterator() {
228 throw new MatrixException("not implemented");
229 }
230
231 }
232
233 @Override
234 public void setVariables(ObservableMap<Variable> variables) {
235 }
236
237 @Override
238 public Map<?, ?> getWrappedObject() {
239 return map;
240 }
241
242 @Override
243 public void setWrappedObject(Map<?, ?> object) {
244 this.map = object;
245 }
246 }