All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mcs.jmeasurement.MeasureData Maven / Gradle / Ivy

The newest version!
/*
 * MCS Media Computer Software Copyright (c) 2005 by MCS
 * -------------------------------------- Created on 23.04.2005 by w.klaas
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.mcs.jmeasurement;

import java.util.Date;

import de.mcs.jmeasurement.exception.InvalidMeasureDataTypeException;

/**
 * this class implements one measure data of an measure point.
 * 
 * @author w.klaas
 */
public class MeasureData {

  /** name of this data value. */
  private String name;

  /** the value of this data. */
  private Object value;

  /** the value of this data. */
  private String asString;

  /** description of this data. */
  private String description;

  /** the class of the value. */
  private Class myClass;

  /** empty constructor not allowed. */
  @SuppressWarnings("unused")
  private MeasureData() {

  }

  /**
   * creating a new Measure data object.
   * 
   * @param aName
   *            name of this data
   * @param aClass
   *            the value class
   * @param aValue
   *            value of this data
   * @param aAsString
   *            value as string
   * @param aDescription
   *            description
   */
  public MeasureData(final String aName, final Class aClass, final Object aValue, final String aAsString,
      final String aDescription) {
    this.name = aName;
    this.value = aValue;
    this.myClass = aClass;
    if (aAsString == null) {
      this.asString = aValue.toString();
    } else {
      this.asString = aAsString;
    }
    this.description = aDescription;
  }

  /**
   * @return Returns the string.
   */
  public final String toString() {
    return "name:" + name + " valuetype:" + myClass.toString() + " value:" + value.toString();
  }

  /**
   * @return Returns the asString.
   */
  public final String getAsString() {
    return asString;
  }

  /**
   * setting the value as String.
   * 
   * @param string
   *            value as string
   */
  public final void setAsString(final String string) {
    asString = string;
  }

  /**
   * @return Returns the description.
   */
  public final String getDescription() {
    return description;
  }

  /**
   * @return Returns the name.
   */
  public final String getName() {
    return name;
  }

  /**
   * @return Returns the value.
   */
  public final Object getValue() {
    return value;
  }

  /**
   * @return Returns the value.
   */
  public final Class getValueClass() {
    return myClass;
  }

  /**
   * 
   * @return the value as long
   * @throws InvalidMeasureDataTypeException
   *             if the value couldn't be converted.
   */
  public final long getAsLong() throws InvalidMeasureDataTypeException {
    if (value instanceof Long) {
      return ((Long) value).longValue();
    } else {
      throw new InvalidMeasureDataTypeException("The measure data is not a Long type.");
    }
  }

  /**
   * setting a data to a long value.
   * 
   * @param aValue
   *            value to set
   * @throws InvalidMeasureDataTypeException
   *             if the value couldn't be converted.
   */
  public final void setAsLong(final long aValue) throws InvalidMeasureDataTypeException {
    if ((value == null) || (value instanceof Long)) {
      value = Long.valueOf(aValue);
      setAsString(Long.toString(aValue));
    } else {
      throw new InvalidMeasureDataTypeException("The measure data is not a Long type.");
    }
  }

  /**
   * 
   * @return the value as long
   * @throws InvalidMeasureDataTypeException
   *             if the value couldn't be converted.
   */
  public final int getAsInt() throws InvalidMeasureDataTypeException {
    if (value instanceof Integer) {
      return ((Integer) value).intValue();
    } else {
      throw new InvalidMeasureDataTypeException("The measure data is not a Integer type.");
    }
  }

  /**
   * setting a data to a long value.
   * 
   * @param aValue
   *            value to set
   * @throws InvalidMeasureDataTypeException
   *             if the value couldn't be converted.
   */
  public final void setAsInt(final int aValue) throws InvalidMeasureDataTypeException {
    if ((value == null) || (value instanceof Integer)) {
      value = Integer.valueOf(aValue);
      setAsString(Integer.toString(aValue));
    } else {
      throw new InvalidMeasureDataTypeException("The measure data is not a Integer type.");
    }
  }

  /**
   * Getting the data as a date.
   * 
   * @return Date
   * @throws InvalidMeasureDataTypeException
   *             if the value couldn't be converted.
   */
  public final Date getAsDate() throws InvalidMeasureDataTypeException {
    if (value instanceof Date) {
      return ((Date) value);
    } else {
      throw new InvalidMeasureDataTypeException("The measure data is not a Date type.");
    }
  }

  /**
   * setting a data to a long value.
   * 
   * @param aDate
   *            value to set
   * @throws InvalidMeasureDataTypeException
   *             if the value couldn't be converted.
   */
  public final void setAsDate(final Date aDate) throws InvalidMeasureDataTypeException {
    value = aDate;
    setAsString(aDate.toString());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy