SEDoubleArray.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
16 package com.generalrobotix.ui.view.graph;
17 
18 import java.text.DecimalFormat;
19 import java.util.StringTokenizer;
20 
21 public class SEDoubleArray implements StringExchangeable {
22  Double value_[];
23  DecimalFormat df_;
24  boolean isNaN_;
25 
31  public SEDoubleArray(int size) {
32  value_ = new Double[size];
33  for (int i = 0; i < size; i ++) {
34  value_[i] = new Double(0.0);
35  }
36  df_ = new DecimalFormat("0.####");
37  }
38 
44  public SEDoubleArray(Double value[]) {
45  value_ = value;
46  df_ = new DecimalFormat("0.####");
47 
48  if (!_isValid(value_)) {
49  throw new StringExchangeException();
50  }
51  }
52 
58  public SEDoubleArray(double value[]) {
59  value_ = new Double[value.length];
60  for (int i = 0; i < value.length; i ++) {
61  value_[i] = new Double(value[i]);
62  }
63  df_ = new DecimalFormat("0.####");
64  if (!_isValid(value_)) {
65  throw new StringExchangeException();
66  }
67  }
68 
74  public SEDoubleArray(float value[]) {
75  value_ = new Double[value.length];
76  for (int i = 0; i < value.length; i ++) {
77  value_[i] = new Double((double)value[i]);
78  }
79  df_ = new DecimalFormat("0.####");
80  if (!_isValid(value_)) {
81  throw new StringExchangeException();
82  }
83  }
84 
90  public SEDoubleArray(String value) {
91  StringTokenizer token = new StringTokenizer(value);
92 
93  value_ = new Double[token.countTokens()];
94  for (int i = 0; i < value_.length; i ++) {
95  String str = token.nextToken();
96  if (str.equals("NaN") || str.equals("")) {
97  isNaN_ = true;
98  value_[i] = new Double(Double.NaN);
99  } else {
100  value_[i] = new Double(str);
101  if (value_[i].isInfinite()) {
102  throw new StringExchangeException();
103  }
104  }
105  }
106  df_ = new DecimalFormat("0.####");
107  }
108 
114  public String toString() {
115  if (isNaN_) { return ""; }
116 
117  StringBuffer strBuf = new StringBuffer();
118 
119  if (value_.length == 0) return strBuf.toString();
120  strBuf.append(df_.format(value_[0]));
121  for (int i = 1; i < value_.length; i ++) {
122  strBuf.append(" ");
123  strBuf.append(df_.format(value_[i]));
124  }
125  return strBuf.toString();
126  }
127 
134  public Object fromString(String str) {
135  setValue(str);
136  return (Object)value_;
137  }
138 
144  public void setValue(Object value) {
145  isNaN_ = false;
146 
147  if (_isValid((Double[])value)) {
148  value_ = (Double[])value;
149  } else {
150  throw new StringExchangeException();
151  }
152  }
153 
154  public void setValue(double[] value) {
155  isNaN_ = false;
156 
157  for (int i = 0; i < value.length; i ++) {
158  if (Double.isNaN(value[i])) {
159  isNaN_ = true;
160  } else if (Double.isInfinite(value[i])) {
161  throw new StringExchangeException();
162  }
163  value_[i] = new Double(value[i]);
164  }
165  }
166 
172  public void setValue(String str) {
173  if (str.equals("")) {
174  isNaN_ = true;
175  for (int i = 0; i < value_.length; i ++) {
176  value_[i] = new Double(Double.NaN);
177  }
178  return;
179  }
180 
181  StringTokenizer token = new StringTokenizer(str);
182 
183  isNaN_ = false;
184  for (int i = 0; i < value_.length; i ++) {
185  if (token.hasMoreTokens()) {
186  String value = token.nextToken();
187  if (value.equals("NaN")) {
188  isNaN_ = true;
189  value_[i] = new Double(Double.NaN);
190  } else {
191  value_[i] = new Double(value);
192  if (value_[i].isInfinite()) {
193  throw new StringExchangeException();
194  }
195  }
196  } else {
197  throw new StringExchangeException();
198  }
199  }
200  }
201 
202  public void setValue(int index, double value) {
203  value_[index] = new Double(value);
204  }
205 
211  public int size() {
212  return value_.length;
213  }
214 
220  public Object getValue() {
221  return (Object)value_;
222  }
223 
230  public Double getValue(int index) {
231  if (value_.length <= index) return null;
232  return value_[index];
233  }
234 
241  public double doubleValue(int index) {
242  if (value_.length <= index) return 0.0;
243  return value_[index].doubleValue();
244  }
245 
252  public float floatValue(int index) {
253  if (value_.length <= index) return 0.0f;
254  return value_[index].floatValue();
255  }
256 
262  public Double[] getDoubleArray() {
263  return value_;
264  }
265 
266  public void get(double[] value) {
267  for (int i = 0; i < value_.length; i ++) {
268  value[i] = value_[i].doubleValue();
269  }
270  }
271 
272  public void get(float[] value) {
273  for (int i = 0; i < value_.length; i ++) {
274  value[i] = value_[i].floatValue();
275  }
276  }
277 
278  private boolean _isValid(Double[] value) {
279  for (int i = 0; i < value.length; i ++) {
280  if (value[i].isNaN()) {
281  isNaN_ = true;
282  return true;
283  } else if (value[i].isInfinite()) {
284  return false;
285  }
286  }
287  return true;
288  }
289 }
#define null
our own NULL pointer
Definition: IceTypes.h:57
png_voidp int value
Definition: png.h:2113
png_uint_32 i
Definition: png.h:2735


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:05