001: /*
002: * MCS Media Computer Software
003: * Copyright (c) 2005 by MCS
004: * --------------------------------------
005: * Created on 23.04.2005 by w.klaas
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: package de.mcs.jmeasurement.example;
020:
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.io.PrintWriter;
026: import java.text.SimpleDateFormat;
027: import java.util.ArrayList;
028: import java.util.Date;
029: import java.util.Iterator;
030:
031: import de.mcs.jmeasurement.IUserData;
032:
033: /**
034: * This is a Object to store all measured data into an ArrayList. The data will
035: * be stored with the date, currentMilliSecond and the accrued fields. Then, if
036: * you want, you can store the data automatically every x data into a file. The
037: * file will be opend first time when writing data and can be closed any time
038: * you want. After measuring the file must be closed (with
039: * UserDataStore.close()). You can also read the measuring with the
040: * UserDataStore.getData()
041: *
042: * @author w.klaas
043: */
044: public class UserDataStore implements IUserData {
045:
046: /**
047: *
048: */
049: private static final long serialVersionUID = 7121946243997081081L;
050:
051: /** default buffer size. */
052: private static final int BUFFER_SIZE = 256;
053:
054: /**
055: * Class for holding one measuredata.
056: *
057: * @author w.klaas
058: */
059: static class Item {
060: /** position to store. */
061: private long pos;
062:
063: /** date to store. */
064: private Date date;
065:
066: /** msec to store. */
067: private long msec;
068:
069: /** accrued to store. */
070: private long accrued;
071:
072: /**
073: * user data store item.
074: *
075: * @param position
076: * position
077: * @param aAccrued
078: * accrued
079: * @param milliseconds
080: * milliseconds
081: * @param aDate
082: * date
083: */
084: public Item(final long position, final long aAccrued,
085: final long milliseconds, final Date aDate) {
086: this .pos = position;
087: this .date = aDate;
088: this .msec = milliseconds;
089: this .accrued = aAccrued;
090: }
091: }
092:
093: /** data array for the data. */
094: private ArrayList<Item> store;
095:
096: /** separator for the dataentries. */
097: private char sepChar;
098:
099: /** how many datas must be stored before they will be writing to the file. */
100: private int writingCount;
101:
102: /** output stream for writing. */
103: private transient FileOutputStream fileOut;
104:
105: /** filename to write to. */
106: private String filename;
107:
108: /** position of this data. */
109: private long actualPosition;
110:
111: /**
112: * Constructor for this object.
113: *
114: * @param aWritingCount
115: * if the are writingCount data items in the list, the data will
116: * be stored to the file. Set this to -1 no data will be stored
117: * automatically. But you can store the data manually with the
118: * writeToStream methode.
119: * @param aFileName
120: * name of the file to write to (incl. path)
121: */
122: public UserDataStore(final int aWritingCount, final String aFileName) {
123: if (aWritingCount > 0) {
124: store = new ArrayList<Item>(aWritingCount);
125: } else {
126: store = new ArrayList<Item>(1);
127: }
128: this .writingCount = aWritingCount;
129: filename = aFileName;
130: actualPosition = 0;
131: }
132:
133: /**
134: * writeing all data to the stream and resetting the internal array.
135: *
136: * @param stream
137: * Stream to write
138: */
139: public final void writeToStream(final OutputStream stream) {
140: PrintWriter printWriter = new PrintWriter(stream);
141: synchronized (store) {
142: String[] datas = getData();
143: for (int i = 0; i < datas.length; i++) {
144: String string = datas[i];
145: printWriter.println(string);
146: }
147: store.clear();
148: }
149: printWriter.flush();
150: }
151:
152: /**
153: * @return Returns the sepChar.
154: */
155: public final char getSepChar() {
156: return sepChar;
157: }
158:
159: /**
160: * @param aSepChar
161: * The sepChar to set.
162: */
163: public final void setSepChar(final char aSepChar) {
164: this .sepChar = aSepChar;
165: }
166:
167: /**
168: * Adding data with the actual datetime.
169: *
170: * @param accrued
171: * the data to add
172: */
173: public final void addData(final long accrued) {
174: addData(accrued, new Date());
175: }
176:
177: /**
178: * Adding data with defined date.
179: *
180: * @param accrued
181: * data to add
182: * @param date
183: * date when to add
184: */
185: public final void addData(final long accrued, final Date date) {
186: synchronized (store) {
187: store.add(new Item(actualPosition, accrued, System
188: .currentTimeMillis(), date));
189:
190: if ((writingCount > 0) && (store.size() >= writingCount)) {
191: writeToFile();
192: }
193: actualPosition++;
194: }
195: }
196:
197: /**
198: * This methode will write all measure data to the file defined.
199: */
200: private void writeToFile() {
201: if (null == fileOut) {
202: try {
203: if (null != filename) {
204: fileOut = new FileOutputStream(filename, true);
205: PrintWriter printWriter = new PrintWriter(fileOut);
206: printWriter.print("Number");
207: printWriter.print(sepChar);
208: printWriter.print("Date and Time");
209: printWriter.print(sepChar);
210: printWriter.print("CurrentTimeMillis");
211: printWriter.print(sepChar);
212: printWriter.println("Accrued");
213: printWriter.flush();
214: }
215: } catch (FileNotFoundException e) {
216: // TODO Auto-generated catch block
217: e.printStackTrace();
218: }
219: }
220: if (null != fileOut) {
221: writeToStream(fileOut);
222: }
223: }
224:
225: /**
226: * Writing the last datas and close the file.
227: */
228: public final void closeFile() {
229: writeToFile();
230: if (null != fileOut) {
231: try {
232: fileOut.flush();
233: fileOut.close();
234: } catch (IOException e) {
235: // TODO Auto-generated catch block
236: e.printStackTrace();
237: }
238: fileOut = null;
239: }
240: }
241:
242: /**
243: * Getting all data as String array (like the file, but without the header).
244: *
245: * @return String[]
246: */
247: public final String[] getData() {
248: SimpleDateFormat dateFormat = new SimpleDateFormat();
249: String[] data = null;
250: synchronized (store) {
251: data = new String[store.size()];
252: int count = 0;
253: for (Iterator iter = store.iterator(); iter.hasNext();) {
254: Item element = (Item) iter.next();
255: StringBuffer dataLine = new StringBuffer(BUFFER_SIZE);
256: dataLine.append(Long.toString(element.pos));
257: dataLine.append(sepChar);
258: if (null != element.date) {
259: dataLine.append(dateFormat.format(element.date));
260: } else {
261: dataLine.append("no date set");
262: }
263: dataLine.append(sepChar);
264: dataLine.append(Long.toString(element.msec));
265: dataLine.append(sepChar);
266: dataLine.append(Long.toString(element.accrued));
267: data[count] = dataLine.toString();
268: count++;
269: }
270: }
271: return data;
272: }
273:
274: /**
275: * cloning this userdata object.
276: *
277: * @return Object
278: */
279: @SuppressWarnings("unchecked")
280: public final Object clone() {
281: try {
282: super .clone();
283: } catch (CloneNotSupportedException e) {
284: e.printStackTrace();
285: }
286: UserDataStore udat = new UserDataStore(this .writingCount,
287: this .filename);
288: udat.actualPosition = this .actualPosition;
289: udat.sepChar = this .sepChar;
290: udat.store = (ArrayList<Item>) this.store.clone();
291: return udat;
292: }
293: }
|