001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 23.04.2005 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.jmeasurement;
018:
019: import java.util.Date;
020:
021: /**
022: * this class implements one measure data of an measure point.
023: *
024: * @author w.klaas
025: */
026: public class MeasureData {
027:
028: /** name of this data value. */
029: private String name;
030:
031: /** the value of this data. */
032: private Object value;
033:
034: /** the value of this data. */
035: private String asString;
036:
037: /** description of this data. */
038: private String description;
039:
040: /** the class of the value. */
041: private Class myClass;
042:
043: /** empty constructor not allowed. */
044: private MeasureData() {
045:
046: }
047:
048: /**
049: * creating a new Measure data object.
050: *
051: * @param aName
052: * name of this data
053: * @param aClass
054: * the value class
055: * @param aValue
056: * value of this data
057: * @param aAsString
058: * value as string
059: * @param aDescription
060: * description
061: */
062: public MeasureData(final String aName, final Class aClass,
063: final Object aValue, final String aAsString,
064: final String aDescription) {
065: this .name = aName;
066: this .value = aValue;
067: this .myClass = aClass;
068: if (aAsString == null) {
069: this .asString = aValue.toString();
070: } else {
071: this .asString = aAsString;
072: }
073: this .description = aDescription;
074: }
075:
076: /**
077: * @return Returns the string.
078: */
079: public final String toString() {
080: return "name:" + name + " valuetype:" + myClass.toString()
081: + " value:" + value.toString();
082: }
083:
084: /**
085: * @return Returns the asString.
086: */
087: public final String getAsString() {
088: return asString;
089: }
090:
091: /**
092: * setting the value as String.
093: *
094: * @param string
095: * value as string
096: */
097: public final void setAsString(final String string) {
098: asString = string;
099: }
100:
101: /**
102: * @return Returns the description.
103: */
104: public final String getDescription() {
105: return description;
106: }
107:
108: /**
109: * @return Returns the name.
110: */
111: public final String getName() {
112: return name;
113: }
114:
115: /**
116: * @return Returns the value.
117: */
118: public final Object getValue() {
119: return value;
120: }
121:
122: /**
123: * @return Returns the value.
124: */
125: public final Class getValueClass() {
126: return myClass;
127: }
128:
129: /**
130: *
131: * @return the value as long
132: * @throws InvalidMeasureDataTypeException
133: * if the value couldn't be converted.
134: */
135: public final long getAsLong()
136: throws InvalidMeasureDataTypeException {
137: if (value instanceof Long) {
138: return ((Long) value).longValue();
139: } else {
140: throw new InvalidMeasureDataTypeException(
141: "The measure data is not a Long type.");
142: }
143: }
144:
145: /**
146: * setting a data to a long value.
147: *
148: * @param aValue
149: * value to set
150: * @throws InvalidMeasureDataTypeException
151: * if the value couldn't be converted.
152: */
153: public final void setAsLong(final long aValue)
154: throws InvalidMeasureDataTypeException {
155: if ((value == null) || (value instanceof Long)) {
156: value = Long.valueOf(aValue);
157: setAsString(Long.toString(aValue));
158: } else {
159: throw new InvalidMeasureDataTypeException(
160: "The measure data is not a Long type.");
161: }
162: }
163:
164: /**
165: *
166: * @return the value as long
167: * @throws InvalidMeasureDataTypeException
168: * if the value couldn't be converted.
169: */
170: public final int getAsInt() throws InvalidMeasureDataTypeException {
171: if (value instanceof Integer) {
172: return ((Integer) value).intValue();
173: } else {
174: throw new InvalidMeasureDataTypeException(
175: "The measure data is not a Integer type.");
176: }
177: }
178:
179: /**
180: * setting a data to a long value.
181: *
182: * @param aValue
183: * value to set
184: * @throws InvalidMeasureDataTypeException
185: * if the value couldn't be converted.
186: */
187: public final void setAsInt(final int aValue)
188: throws InvalidMeasureDataTypeException {
189: if ((value == null) || (value instanceof Integer)) {
190: value = Integer.valueOf(aValue);
191: setAsString(Integer.toString(aValue));
192: } else {
193: throw new InvalidMeasureDataTypeException(
194: "The measure data is not a Integer type.");
195: }
196: }
197:
198: /**
199: * Getting the data as a date.
200: *
201: * @return Date
202: * @throws InvalidMeasureDataTypeException
203: * if the value couldn't be converted.
204: */
205: public final Date getAsDate()
206: throws InvalidMeasureDataTypeException {
207: if (value instanceof Date) {
208: return ((Date) value);
209: } else {
210: throw new InvalidMeasureDataTypeException(
211: "The measure data is not a Date type.");
212: }
213: }
214:
215: /**
216: * setting a data to a long value.
217: *
218: * @param aDate
219: * value to set
220: * @throws InvalidMeasureDataTypeException
221: * if the value couldn't be converted.
222: */
223: public final void setAsDate(final Date aDate)
224: throws InvalidMeasureDataTypeException {
225: value = aDate;
226: setAsString(aDate.toString());
227: }
228: }
|