File: Variable.java

package info (click to toggle)
imagej 1.46a-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: java: 89,778; sh: 311; xml: 51; makefile: 6
file content (139 lines) | stat: -rw-r--r-- 2,939 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ij.macro;

class Variable implements MacroConstants, Cloneable {
	static final int VALUE=0, ARRAY=1, STRING=2;
    int symTabIndex;
    private double value;
    private String str;
    private Variable[] array;
    private int arraySize;

    Variable() {
    }

    Variable(double value) {
        this.value = value;
    }

    Variable(int symTabIndex, double value, String str) {
        this.symTabIndex = symTabIndex;
        this.value = value;
        this.str = str;
    }

    Variable(int symTabIndex, double value, String str, Variable[] array) {
        this.symTabIndex = symTabIndex;
        this.value = value;
        this.str = str;
        this.array = array;
    }

    Variable(byte[] array) {
    	this.array = new Variable[array.length];
    	for (int i=0; i<array.length; i++)
    		this.array[i] = new Variable(array[i]&255);
    }

    Variable(int[] array) {
    	this.array = new Variable[array.length];
    	for (int i=0; i<array.length; i++)
    		this.array[i] = new Variable(array[i]);
    }

    Variable(double[] array) {
    	this.array = new Variable[array.length];
    	for (int i=0; i<array.length; i++)
    		this.array[i] = new Variable(array[i]);
    }

    double getValue() {
    	if (str!=null)
    			return convertToDouble();  // string to number conversions
    	else
        	return value;
    }

	double convertToDouble() {
		try {
			Double d = new Double(str);
			return d.doubleValue();
		} catch (NumberFormatException e){
			return Double.NaN;
		}
	}

    void setValue(double value) {
        this.value = value;
        str = null;
        array = null;
    }

    String getString() {
        return str;
    }

    void setString(String str) {
        this.str = str;
        value = 0.0;
        array = null;
    }

    Variable[] getArray() {
        return array;
    }

    void setArray(Variable[] array) {
        this.array = array;
        value = 0.0;
        str = null;
        arraySize = 0;
    }
    
    void setArraySize(int size) {
    	if (array==null)
    		size = 0;
    	else if (size>array.length)
    		size = array.length;
    	arraySize = size;
    }
    
    int getArraySize() {
    	int size = array!=null?array.length:0;
    	if (arraySize>0) size = arraySize;
    	return size;
    }

    int getType() {
    	if (array!=null)
    		return ARRAY;
    	else if (str!=null)
    		return STRING;
    	else
    		return VALUE;
    }

	public String toString() {
		String s = "";
		if (array!=null)
			s += "array["+array.length+"]";
		else if (str!=null) {
			s = str;
			if (s.length()>80)
				s = s.substring(0, 80)+"...";
			s = s.replaceAll("\n", " | ");
			s = "\""+s+"\"";
		} else {
			if (value==(int)value)
				s += (int)value;
			else
				s += ij.IJ.d2s(value,4);
		}
		return s;
	}
    
	public synchronized Object clone() {
		try {return super.clone();}
		catch (CloneNotSupportedException e) {return null;}
	}

} // class Variable