FloatParameter.java :  » UnTagged » epic-architecture » org » mobilesynergies » epic » client » parameter » Android Open Source

Android Open Source » UnTagged » epic architecture 
epic architecture » org » mobilesynergies » epic » client » parameter » FloatParameter.java
package org.mobilesynergies.epic.client.parameter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.jivesoftware.smackx.FormField;

public class FloatParameter extends Parameter {
  
  private float mValue = 0;
  private float mMin = Float.NEGATIVE_INFINITY;
  private float mMax = Float.POSITIVE_INFINITY;
  private float mStepsize = 1;

  public FloatParameter(float value) {
    setValue(value);
  }

  public FloatParameter(float value, float min, float max, float stepsize) {
    setMin(min);
    setMax(max);
    setValue(value);
    setStepsize(stepsize);
  }
  
  public FloatParameter(HashMap<String, String> map) {
    if(map.containsKey(KEY_VALUE)){
      try{
        mValue = Float.parseFloat(map.get(KEY_VALUE));        
      } catch (Exception e){
        System.out.println("Unable to parse value");
      }
    }
    if(map.containsKey(KEY_MIN)){
      try{
        mMin = Float.parseFloat(map.get(KEY_MIN));        
      } catch (Exception e){
        System.out.println("Unable to parse minimum value");
      }
    }
    if(map.containsKey(KEY_MAX)){
      try{
        mMax = Float.parseFloat(map.get(KEY_MAX));        
      } catch (Exception e){
        System.out.println("Unable to parse maximum value");
      }
    }
    if(map.containsKey(KEY_STEPSIZE)){
      try{
        mStepsize = Float.parseFloat(map.get(KEY_STEPSIZE));        
      } catch (Exception e){
        System.out.println("Unable to parse stepsize");
      }
    }
  }


  /**
   * @param mValue the mValue to set
   */
  public void setValue(float value) {
    this.mValue = Math.max(Math.min(value, mMax),mMin);
  }

  /**
   * @return the mValue
   */
  public float getValue() {
    return mValue;
  }
  
  /**
   * @param min the min to set
   */
  public void setMin(float min) {
    this.mMin = min;
  }

  /**
   * @return the mMin
   */
  public float getMin() {
    return mMin;
  }

  /**
   * @param max the max to set
   */
  public void setMax(float max) {
    this.mMax = max;
  }

  /**
   * @return the mMax
   */
  public float getMax() {
    return mMax;
  }

  /**
   * @param stepsize the stepsize to set
   */
  public void setStepsize(float stepsize) {
    this.mStepsize = stepsize;
  }

  /**
   * @return the mStepsize
   */
  public float getStepsize() {
    return mStepsize;
  }

  @Override
  public String getType() {
    return Parameter.TYPENAME_FLOAT;
  }
  
  @Override
  public String toXml(String name) {
    String type = Float.class.getCanonicalName();
    String value = Float.toString(mValue); 
    String extraattributes = null;
    String extraxml = null;
    return toXml(name, type, value, extraattributes, extraxml);
  }
  
  @Override
  public Set<FormField> toFormFields(String name) {
    Set<FormField> set = new HashSet<FormField>();
    FormField f1 = new FormField(name);
    f1.setLabel(name);
    f1.setType(FormField.TYPE_TEXT_SINGLE);
    f1.addValue(Float.toString(mValue));
    set.add(f1);
    
    FormField f2 = new FormField(name+SPECIALIZECHARACTER+KEY_MIN);
    f2.setLabel(name);
    f2.setType(FormField.TYPE_HIDDEN);
    f2.addValue(Float.toString(mMin));
    set.add(f2);
    
    FormField f3 = new FormField(name+SPECIALIZECHARACTER+KEY_MAX);
    f3.setLabel(name);
    f3.setType(FormField.TYPE_HIDDEN);
    f3.addValue(Float.toString(mMax));
    set.add(f3);
    
    FormField f4 = new FormField(name+SPECIALIZECHARACTER+KEY_STEPSIZE);
    f4.setLabel(name);
    f4.setType(FormField.TYPE_HIDDEN);
    f4.addValue(Float.toString(mStepsize));
    set.add(f4);
    
    FormField f5 = new FormField(name+SPECIALIZECHARACTER+KEY_TYPE);
    f5.setLabel(name);
    f5.setType(FormField.TYPE_HIDDEN);
    f5.addValue(TYPENAME_FLOAT);
    set.add(f5);    
    
    return set;
  }

  @Override
  public String getValueAsString() {
    return Float.toString(getValue());
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.