SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
DataField.java
1/*
2 * Class: DataField
3 * Description: Represents a data field
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author David Munger
9 * @since August 2009
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.util.io;
26
27import java.lang.reflect.Array;
28
35public class DataField {
36 protected String label;
37 protected Object data;
38 protected int effectiveLength;
39
46 public DataField(String label, Object data) {
47 this(label, data, -1);
48 }
49
58 public DataField(String label, Object data, int effectiveLength) {
59 this.label = label;
60 this.data = data;
61 this.effectiveLength = effectiveLength;
62 }
63
67
71 public String getLabel() {
72 return label;
73 }
74
78 public Class getType() {
79 return data.getClass();
80 }
81
85 public boolean isAtomic() {
86 return !isArray();
87 }
88
92 public boolean isArray() {
93 return data.getClass().isArray();
94 }
95
99 public boolean isArray2D() {
100 return isArray() && Array.get(data, 0).getClass().isArray();
101 }
102
107 public int getArrayLength() {
108 if (!isArray())
109 return -1;
110 if (effectiveLength < 0)
111 return Array.getLength(data);
112 return effectiveLength;
113 }
114
118 public boolean isString() {
119 return (data instanceof String);
120 }
121
125 public boolean isInt() {
126 return (data instanceof Integer);
127 }
128
132 public boolean isFloat() {
133 return (data instanceof Float);
134 }
135
139 public boolean isDouble() {
140 return (data instanceof Double);
141 }
142
146
150
155 public String asString() {
156 return (data instanceof String) ? (String) data : null;
157 }
158
162 public int asInt() {
163 return (data instanceof Integer) ? ((Integer) data).intValue() : 0;
164 }
165
170 public float asFloat() {
171 return (data instanceof Float) ? ((Float) data).floatValue() : 0;
172 }
173
178 public double asDouble() {
179 return (data instanceof Double) ? ((Double) data).doubleValue() : 0;
180 }
181
185
189
194 public String[] asStringArray() {
195 return (data instanceof String[]) ? (String[]) data : null;
196 }
197
202 public int[] asIntArray() {
203 return (data instanceof int[]) ? (int[]) data : null;
204 }
205
210 public float[] asFloatArray() {
211 return (data instanceof float[]) ? (float[]) data : null;
212 }
213
218 public double[] asDoubleArray() {
219 return (data instanceof double[]) ? (double[]) data : null;
220 }
221
225
229
234 public String[][] asStringArray2D() {
235 return (data instanceof String[][]) ? (String[][]) data : null;
236 }
237
242 public int[][] asIntArray2D() {
243 return (data instanceof int[][]) ? (int[][]) data : null;
244 }
245
250 public float[][] asFloatArray2D() {
251 return (data instanceof float[][]) ? (float[][]) data : null;
252 }
253
258 public double[][] asDoubleArray2D() {
259 return (data instanceof double[][]) ? (double[][]) data : null;
260 }
261
265
269
273 public Object asObject() {
274 return data;
275 }
276
277}
278
Class getType()
Returns the type of the field.
String getLabel()
Returns the field label (or name).
double[][] asDoubleArray2D()
Returns the value as two-dimensional double array or null if it is not of type double[][].
int[] asIntArray()
Returns the value as one-dimensional int array or null if it is not of type int[].
double asDouble()
Returns the value as double or 0 if it is not of type double See isDouble.
String asString()
Returns the value as String, or null if it is not of type String.
boolean isAtomic()
Returns true if the field value is atomic data.
boolean isFloat()
Returns true if the field value is an atomic float.
double[] asDoubleArray()
Returns the value as one-dimensional double array or null if it is not of type double[].
float asFloat()
Returns the value as float or 0 if it is not of type float See isFloat.
DataField(String label, Object data, int effectiveLength)
Constructor.
String[] asStringArray()
Returns the value as one-dimensional String array or null if it is not of type String[].
float[][] asFloatArray2D()
Returns the value as two-dimensional float array or null if it is not of type float[][].
boolean isString()
Returns true if the field value is an atomic String.
int getArrayLength()
Returns the length of the array contained by the field, or -1 if it is not an array.
boolean isArray2D()
Returns true if the field contains a two-dimensional array.
boolean isArray()
Returns true if the field contains an array.
boolean isDouble()
Returns true if the field value is an atomic double.
int[][] asIntArray2D()
Returns the value as two-dimensional int array or null if it is not of type int[][].
int asInt()
Returns the value as int or 0 if it is not of type int See isInt.
DataField(String label, Object data)
Constructor.
String[][] asStringArray2D()
Returns the value as two-dimensional String array or null if it is not of type String[][].
float[] asFloatArray()
Returns the value as one-dimensional float array or null if it is not of type float[].
Object asObject()
Returns the value of the field as an Object.
boolean isInt()
Returns true if the field value is an atomic int.