00001 /* 00002 * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc. 00003 * All rights reserved. This program is made available under the terms of the 00004 * Eclipse Public License v1.0 which accompanies this distribution, and is 00005 * available at http://www.eclipse.org/legal/epl-v10.html 00006 * Contributors: 00007 * General Robotix Inc. 00008 * National Institute of Advanced Industrial Science and Technology (AIST) 00009 */ 00016 package com.generalrobotix.ui.view.graph; 00017 00018 import java.text.DecimalFormat; 00019 import java.util.StringTokenizer; 00020 00021 public class SEDoubleArray implements StringExchangeable { 00022 Double value_[]; 00023 DecimalFormat df_; 00024 boolean isNaN_; 00025 00031 public SEDoubleArray(int size) { 00032 value_ = new Double[size]; 00033 for (int i = 0; i < size; i ++) { 00034 value_[i] = new Double(0.0); 00035 } 00036 df_ = new DecimalFormat("0.####"); 00037 } 00038 00044 public SEDoubleArray(Double value[]) { 00045 value_ = value; 00046 df_ = new DecimalFormat("0.####"); 00047 00048 if (!_isValid(value_)) { 00049 throw new StringExchangeException(); 00050 } 00051 } 00052 00058 public SEDoubleArray(double value[]) { 00059 value_ = new Double[value.length]; 00060 for (int i = 0; i < value.length; i ++) { 00061 value_[i] = new Double(value[i]); 00062 } 00063 df_ = new DecimalFormat("0.####"); 00064 if (!_isValid(value_)) { 00065 throw new StringExchangeException(); 00066 } 00067 } 00068 00074 public SEDoubleArray(float value[]) { 00075 value_ = new Double[value.length]; 00076 for (int i = 0; i < value.length; i ++) { 00077 value_[i] = new Double((double)value[i]); 00078 } 00079 df_ = new DecimalFormat("0.####"); 00080 if (!_isValid(value_)) { 00081 throw new StringExchangeException(); 00082 } 00083 } 00084 00090 public SEDoubleArray(String value) { 00091 StringTokenizer token = new StringTokenizer(value); 00092 00093 value_ = new Double[token.countTokens()]; 00094 for (int i = 0; i < value_.length; i ++) { 00095 String str = token.nextToken(); 00096 if (str.equals("NaN") || str.equals("")) { 00097 isNaN_ = true; 00098 value_[i] = new Double(Double.NaN); 00099 } else { 00100 value_[i] = new Double(str); 00101 if (value_[i].isInfinite()) { 00102 throw new StringExchangeException(); 00103 } 00104 } 00105 } 00106 df_ = new DecimalFormat("0.####"); 00107 } 00108 00114 public String toString() { 00115 if (isNaN_) { return ""; } 00116 00117 StringBuffer strBuf = new StringBuffer(); 00118 00119 if (value_.length == 0) return strBuf.toString(); 00120 strBuf.append(df_.format(value_[0])); 00121 for (int i = 1; i < value_.length; i ++) { 00122 strBuf.append(" "); 00123 strBuf.append(df_.format(value_[i])); 00124 } 00125 return strBuf.toString(); 00126 } 00127 00134 public Object fromString(String str) { 00135 setValue(str); 00136 return (Object)value_; 00137 } 00138 00144 public void setValue(Object value) { 00145 isNaN_ = false; 00146 00147 if (_isValid((Double[])value)) { 00148 value_ = (Double[])value; 00149 } else { 00150 throw new StringExchangeException(); 00151 } 00152 } 00153 00154 public void setValue(double[] value) { 00155 isNaN_ = false; 00156 00157 for (int i = 0; i < value.length; i ++) { 00158 if (Double.isNaN(value[i])) { 00159 isNaN_ = true; 00160 } else if (Double.isInfinite(value[i])) { 00161 throw new StringExchangeException(); 00162 } 00163 value_[i] = new Double(value[i]); 00164 } 00165 } 00166 00172 public void setValue(String str) { 00173 if (str.equals("")) { 00174 isNaN_ = true; 00175 for (int i = 0; i < value_.length; i ++) { 00176 value_[i] = new Double(Double.NaN); 00177 } 00178 return; 00179 } 00180 00181 StringTokenizer token = new StringTokenizer(str); 00182 00183 isNaN_ = false; 00184 for (int i = 0; i < value_.length; i ++) { 00185 if (token.hasMoreTokens()) { 00186 String value = token.nextToken(); 00187 if (value.equals("NaN")) { 00188 isNaN_ = true; 00189 value_[i] = new Double(Double.NaN); 00190 } else { 00191 value_[i] = new Double(value); 00192 if (value_[i].isInfinite()) { 00193 throw new StringExchangeException(); 00194 } 00195 } 00196 } else { 00197 throw new StringExchangeException(); 00198 } 00199 } 00200 } 00201 00202 public void setValue(int index, double value) { 00203 value_[index] = new Double(value); 00204 } 00205 00211 public int size() { 00212 return value_.length; 00213 } 00214 00220 public Object getValue() { 00221 return (Object)value_; 00222 } 00223 00230 public Double getValue(int index) { 00231 if (value_.length <= index) return null; 00232 return value_[index]; 00233 } 00234 00241 public double doubleValue(int index) { 00242 if (value_.length <= index) return 0.0; 00243 return value_[index].doubleValue(); 00244 } 00245 00252 public float floatValue(int index) { 00253 if (value_.length <= index) return 0.0f; 00254 return value_[index].floatValue(); 00255 } 00256 00262 public Double[] getDoubleArray() { 00263 return value_; 00264 } 00265 00266 public void get(double[] value) { 00267 for (int i = 0; i < value_.length; i ++) { 00268 value[i] = value_[i].doubleValue(); 00269 } 00270 } 00271 00272 public void get(float[] value) { 00273 for (int i = 0; i < value_.length; i ++) { 00274 value[i] = value_[i].floatValue(); 00275 } 00276 } 00277 00278 private boolean _isValid(Double[] value) { 00279 for (int i = 0; i < value.length; i ++) { 00280 if (value[i].isNaN()) { 00281 isNaN_ = true; 00282 return true; 00283 } else if (value[i].isInfinite()) { 00284 return false; 00285 } 00286 } 00287 return true; 00288 } 00289 }