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.variable;
025    
026    import java.util.LinkedList;
027    
028    import javax.swing.event.ListDataEvent;
029    import javax.swing.event.ListDataListener;
030    
031    import org.jdmp.core.util.DefaultObservableList;
032    import org.jdmp.core.util.MatrixListToMatrixWrapper;
033    import org.jdmp.core.util.ObservableList;
034    import org.ujmp.core.Matrix;
035    import org.ujmp.core.collections.RingBufferList;
036    import org.ujmp.core.coordinates.Coordinates;
037    
038    public class DefaultVariable extends AbstractVariable {
039            private static final long serialVersionUID = -7192491915167470355L;
040    
041            private transient Matrix matrixListMatrix = null;
042    
043            private long[] size = Coordinates.ZERO2D;
044    
045            private ObservableList<Matrix> matrixList = null;
046    
047            private String label = "";
048    
049            private String description = "";
050    
051            public final String getDescription() {
052                    return description;
053            }
054    
055            public final void setDescription(String description) {
056                    this.description = description;
057            }
058    
059            public String getLabel() {
060                    return label;
061            }
062    
063            public final void setLabel(String label) {
064                    this.label = label;
065            }
066    
067            public DefaultVariable() {
068                    super();
069                    matrixList = new DefaultObservableList<Matrix>(new LinkedList<Matrix>());
070                    matrixList.addListDataListener(new VariableListDataListener());
071            }
072    
073            public DefaultVariable(int memorySize) {
074                    super();
075                    matrixList = new DefaultObservableList<Matrix>(new RingBufferList<Matrix>(memorySize));
076                    matrixList.addListDataListener(new VariableListDataListener());
077            }
078    
079            public final long[] getSize() {
080                    if (Coordinates.product(size) == 0) {
081                            Matrix m = getMatrix();
082                            if (m != null) {
083                                    setSize(Coordinates.copyOf(m.getSize()));
084                            }
085                    }
086                    return size;
087            }
088    
089            public final void setSize(long... size) {
090                    this.size = Coordinates.copyOf(size);
091            }
092    
093            public Matrix getAsMatrix() {
094                    if (matrixListMatrix == null) {
095                            matrixListMatrix = new MatrixListToMatrixWrapper(this);
096                    }
097                    return matrixListMatrix;
098            }
099    
100            public ObservableList<Matrix> getMatrixList() {
101                    return matrixList;
102            }
103    
104            public Variable clone() {
105                    Variable v = VariableFactory.labeledVariable(getLabel());
106                    for (Matrix m : getMatrixList()) {
107                            v.getMatrixList().add(m.copy());
108                    }
109                    return v;
110            }
111    
112            class VariableListDataListener implements ListDataListener {
113    
114                    @Override
115                    public void contentsChanged(ListDataEvent e) {
116                            notifyGUIObject();
117                    }
118    
119                    @Override
120                    public void intervalAdded(ListDataEvent e) {
121                            notifyGUIObject();
122                    }
123    
124                    @Override
125                    public void intervalRemoved(ListDataEvent e) {
126                            notifyGUIObject();
127                    }
128    
129            }
130    
131    }