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.lang.reflect.Constructor;
027    import java.util.logging.Level;
028    
029    import org.jdmp.core.AbstractCoreObject;
030    import org.jdmp.core.variable.Variable;
031    import org.ujmp.core.Matrix;
032    import org.ujmp.core.MatrixFactory;
033    import org.ujmp.core.interfaces.GUIObject;
034    import org.ujmp.core.util.MathUtil;
035    import org.ujmp.core.util.StringUtil;
036    
037    public abstract class AbstractSample extends AbstractCoreObject implements Sample {
038            private static final long serialVersionUID = 1693258179407382419L;
039    
040            private transient GUIObject guiObject = null;
041    
042            public AbstractSample() {
043                    super();
044            }
045    
046            @Override
047            public abstract Sample clone();
048    
049            public final String getId() {
050                    if (getMatrix(Sample.ID) == null) {
051                            return null;
052                    }
053                    return StringUtil.convert(getMatrix(Sample.ID));
054            }
055    
056            @Override
057            public String toString() {
058                    StringBuilder s = new StringBuilder();
059                    s.append(getClass().getSimpleName());
060                    s.append(" [ ");
061                    for (Object key : getVariables().keySet()) {
062                            Variable v = getVariables().get(key);
063                            s.append(key + "=");
064                            s.append(StringUtil.format(v.getMatrix()));
065                            s.append(" ");
066                    }
067                    s.append("]");
068                    return s.toString();
069            }
070    
071            public final GUIObject getGUIObject() {
072                    if (guiObject == null) {
073                            try {
074                                    Class<?> c = Class.forName("org.jdmp.gui.sample.SampleGUIObject");
075                                    Constructor<?> con = c.getConstructor(new Class<?>[] { Sample.class });
076                                    guiObject = (GUIObject) con.newInstance(new Object[] { this });
077                            } catch (Exception e) {
078                                    logger.log(Level.WARNING, "cannot create sample gui object", e);
079                            }
080                    }
081                    return guiObject;
082            }
083    
084            public final Matrix getMatrix(Object variableKey) {
085                    Variable v = getVariables().get(variableKey);
086                    if (v != null) {
087                            return v.getMatrix();
088                    } else {
089                            return null;
090                    }
091            }
092    
093            public final String getAsString(Object variableKey) {
094                    Variable v = getVariables().get(variableKey);
095                    if (v != null) {
096                            return v.getAsString();
097                    } else {
098                            return null;
099                    }
100            }
101    
102            public final void notifyGUIObject() {
103                    if (guiObject != null) {
104                            guiObject.fireValueChanged();
105                    }
106            }
107    
108            @Override
109            public void setId(String id) {
110                    setMatrix(Sample.ID, MathUtil.getMatrix(id));
111            }
112    
113            @Override
114            public String getDescription() {
115                    Matrix m = getMatrix(Sample.DESCRIPTION);
116                    if (m == null) {
117                            return null;
118                    } else {
119                            return StringUtil.convert(m);
120                    }
121            }
122    
123            @Override
124            public void setDescription(String description) {
125                    setMatrix(Sample.DESCRIPTION, MathUtil.getMatrix(description));
126            }
127    
128            @Override
129            public String getLabel() {
130                    Matrix m = getMatrix(Sample.LABEL);
131                    if (m == null) {
132                            return null;
133                    } else {
134                            return StringUtil.convert(m);
135                    }
136            }
137    
138            @Override
139            public void setLabel(String label) {
140                    setMatrix(Sample.LABEL, MathUtil.getMatrix(label));
141            }
142    
143            public final void setObject(Object variableKey, Object value) {
144                    if (value == null) {
145                            setMatrix(variableKey, MatrixFactory.emptyMatrix());
146                    } else if (value instanceof Matrix) {
147                            setMatrix(variableKey, (Matrix) value);
148                    } else {
149                            setMatrix(variableKey, MatrixFactory.linkToValue(value));
150                    }
151            }
152    
153            @Override
154            public final void clear() {
155                    getVariables().clear();
156            }
157    
158    }