001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto 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 GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.dataset;
025:
026: import jacareto.system.Environment;
027: import jacareto.system.EnvironmentMember;
028:
029: import java.util.Vector;
030:
031: /**
032: * A set of data cases. All cases contained in the set must be of the same type.
033: *
034: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
035: * @version 1.0
036: */
037: public class DataSet extends EnvironmentMember {
038: /** The data cases. */
039: private Vector dataCases;
040:
041: /** The name of the data set. */
042: private String name;
043:
044: /** The modifier of the data set. */
045: private DataSetModifier modifier;
046:
047: /**
048: * Creates a new data set containing the given data cases.
049: *
050: * @param env the environment
051: * @param name the data sets name
052: * @param dataCases the data cases
053: */
054: public DataSet(Environment env, String name, DataCase[] dataCases) {
055: super (env);
056: this .dataCases = new Vector(10, 10);
057: setName(name);
058: addCases(dataCases);
059: modifier = new DataSetModifier(env, this );
060: }
061:
062: /**
063: * Creates a new data set containing no data cases.
064: *
065: * @param env the environment
066: */
067: public DataSet(Environment env) {
068: this (env, "", new DataCase[0]);
069: }
070:
071: /**
072: * Sets the name of the data set.
073: *
074: * @param name DOCUMENT ME!
075: */
076: public void setName(String name) {
077: this .name = name;
078: }
079:
080: /**
081: * Returns the name of the data set.
082: *
083: * @return DOCUMENT ME!
084: */
085: public String getName() {
086: return name;
087: }
088:
089: /**
090: * Adds a data case.
091: *
092: * @param dataCase DOCUMENT ME!
093: */
094: private void addCase(DataCase dataCase) {
095: try {
096: dataCases.add(dataCase);
097: } catch (NullPointerException n) {
098: ;
099: }
100: }
101:
102: /**
103: * Removes a data case.
104: *
105: * @param i the index of the data case to be removed
106: */
107: public void removeCase(int i) {
108: try {
109: dataCases.remove(i);
110: } catch (Exception e) {
111: env.getLogger().error(
112: env.getLanguage().getString(
113: "DataSet.Error.DeletingCase")
114: + " " + i);
115: }
116: }
117:
118: /**
119: * Returns the number of cases contained in the dataset
120: *
121: * @return DOCUMENT ME!
122: */
123: public int size() {
124: try {
125: return dataCases.size();
126: } catch (NullPointerException n) {
127: return 0;
128: }
129: }
130:
131: /**
132: * Returns the number of values belonging to a case contained in this set. If no case is
133: * contained, 0 will be returned.
134: *
135: * @return DOCUMENT ME!
136: */
137: public int caseSize() {
138: try {
139: return getCase(0).size();
140: } catch (NullPointerException n) {
141: return 0;
142: }
143: }
144:
145: /**
146: * Adds the cases contained in the array.
147: *
148: * @param dataCases the cases to add
149: */
150: private void addCases(DataCase[] dataCases) {
151: try {
152: for (int i = 0; i < dataCases.length; i++) {
153: addCase(dataCases[i]);
154: }
155: } catch (NullPointerException n) {
156: ;
157: }
158: }
159:
160: /**
161: * Returns the case at the specified index.
162: *
163: * @param i the index
164: *
165: * @return the data case at the given index
166: */
167: public DataCase getCase(int i) {
168: return (DataCase) dataCases.get(i);
169: }
170:
171: /**
172: * Returns the cases as array
173: *
174: * @return DOCUMENT ME!
175: */
176: public DataCase[] getCases() {
177: DataCase[] result = new DataCase[dataCases.size()];
178:
179: for (int i = 0; i < result.length; i++) {
180: result[i] = (DataCase) dataCases.get(i);
181: }
182:
183: return result;
184: }
185:
186: /**
187: * Returns the value names.
188: *
189: * @return DOCUMENT ME!
190: */
191: public String[] getValueNames() {
192: if (dataCases.size() > 0) {
193: return ((DataCase) dataCases.get(0)).getValueNames();
194: } else {
195: return new String[0];
196: }
197: }
198:
199: /**
200: * Returns the modifier of this data set.
201: *
202: * @return DOCUMENT ME!
203: */
204: public DataSetModifier getModifier() {
205: return modifier;
206: }
207: }
|