Skip to content

Commit 7dd9d7a

Browse files
committed
Adding value formatter from diirt
1 parent 1caaf83 commit 7dd9d7a

2 files changed

Lines changed: 618 additions & 0 deletions

File tree

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/**
2+
* Copyright (C) 2010-18 diirt developers. See COPYRIGHT.TXT
3+
* All rights reserved. Use is subject to license terms. See LICENSE.TXT
4+
*/
5+
package org.epics.vtype;
6+
7+
import java.text.FieldPosition;
8+
import java.text.NumberFormat;
9+
import java.util.List;
10+
import org.epics.util.array.ListBoolean;
11+
import org.epics.util.array.ListByte;
12+
import org.epics.util.array.ListInteger;
13+
import org.epics.util.array.ListLong;
14+
import org.epics.util.array.ListNumber;
15+
import org.epics.util.array.ListShort;
16+
17+
/**
18+
* Default implementation for formatting.
19+
*
20+
* @author carcassi
21+
*/
22+
public class SimpleValueFormat extends ValueFormat {
23+
24+
private static final long serialVersionUID = 1L;
25+
private int maxElements;
26+
27+
/**
28+
* Formats any scalar and array, by using the server side formatting
29+
* and limiting the elements of the array displayed to maxElements.
30+
*
31+
* @param maxElements maximum number of array elements converted to string
32+
*/
33+
public SimpleValueFormat(int maxElements) {
34+
this.maxElements = maxElements;
35+
}
36+
37+
@Override
38+
protected StringBuffer format(Scalar scalar, StringBuffer toAppendTo, FieldPosition pos) {
39+
if (scalar == null || scalar.getValue() == null) {
40+
return toAppendTo;
41+
}
42+
43+
if (scalar instanceof DisplayProvider && nf(scalar) != null) {
44+
NumberFormat f = ((DisplayProvider)scalar).getDisplay().getFormat();
45+
return f.format(scalar.getValue(), toAppendTo, pos);
46+
}
47+
48+
toAppendTo.append(scalar.getValue());
49+
return toAppendTo;
50+
}
51+
52+
/**
53+
* Returns the appropriate NumberFormat: either the one
54+
* from the data or the set by the formatting options.
55+
*
56+
* @param obj data object
57+
* @return number format
58+
*/
59+
private NumberFormat nf(Object obj) {
60+
if (getNumberFormat() != null)
61+
return getNumberFormat();
62+
63+
if (obj instanceof DisplayProvider) {
64+
return ((DisplayProvider) obj).getDisplay().getFormat();
65+
}
66+
67+
return null;
68+
}
69+
70+
/**
71+
* Formats a numeric array. This method can be overridden to change
72+
* the way numeric arrays are formatted.
73+
*
74+
* @param array the array to format
75+
* @param toAppendTo the buffer to append to
76+
* @param pos the position of the field
77+
* @return the string buffer
78+
*/
79+
protected StringBuffer format(VNumberArray array, StringBuffer toAppendTo, FieldPosition pos) {
80+
NumberFormat f = nf(array);
81+
82+
toAppendTo.append("[");
83+
boolean hasMore = false;
84+
85+
ListNumber data = array.getData();
86+
if (data.size() > maxElements) {
87+
hasMore = true;
88+
}
89+
90+
for (int i = 0; i < Math.min(data.size(), maxElements); i++) {
91+
if (i != 0) {
92+
toAppendTo.append(", ");
93+
}
94+
if (data instanceof ListByte || data instanceof ListShort || data instanceof ListInteger || data instanceof ListLong) {
95+
toAppendTo.append(f.format(data.getLong(i)));
96+
} else {
97+
toAppendTo.append(f.format(data.getDouble(i)));
98+
}
99+
}
100+
101+
if (hasMore) {
102+
toAppendTo.append(", ...");
103+
}
104+
toAppendTo.append("]");
105+
return toAppendTo;
106+
}
107+
108+
/**
109+
* Formats a string array. This method can be overridden to change
110+
* the way string arrays are formatted.
111+
*
112+
* @param data the data to format
113+
* @param toAppendTo the buffer to append to
114+
* @param pos the position of the field
115+
* @return the string buffer
116+
*/
117+
protected StringBuffer format(List<String> data, StringBuffer toAppendTo, FieldPosition pos) {
118+
toAppendTo.append("[");
119+
boolean hasMore = false;
120+
121+
if (data.size() > maxElements) {
122+
hasMore = true;
123+
}
124+
125+
for (int i = 0; i < Math.min(data.size(), maxElements); i++) {
126+
if (i != 0) {
127+
toAppendTo.append(", ");
128+
}
129+
toAppendTo.append(data.get(i));
130+
}
131+
132+
if (hasMore) {
133+
toAppendTo.append(", ...");
134+
}
135+
toAppendTo.append("]");
136+
return toAppendTo;
137+
}
138+
139+
/**
140+
* Formats a boolean array. This method can be overridden to change
141+
* the way string arrays are formatted.
142+
*
143+
* @param data the data to format
144+
* @param toAppendTo the buffer to append to
145+
* @param pos the position of the field
146+
* @return the string buffer
147+
*/
148+
protected StringBuffer format(ListBoolean data, StringBuffer toAppendTo, FieldPosition pos) {
149+
toAppendTo.append("[");
150+
boolean hasMore = false;
151+
152+
if (data.size() > maxElements) {
153+
hasMore = true;
154+
}
155+
156+
for (int i = 0; i < Math.min(data.size(), maxElements); i++) {
157+
if (i != 0) {
158+
toAppendTo.append(", ");
159+
}
160+
toAppendTo.append(data.getBoolean(i));
161+
}
162+
163+
if (hasMore) {
164+
toAppendTo.append(", ...");
165+
}
166+
toAppendTo.append("]");
167+
return toAppendTo;
168+
}
169+
170+
@Override
171+
protected StringBuffer format(Array array, StringBuffer toAppendTo, FieldPosition pos) {
172+
if (array instanceof VNumberArray) {
173+
return format((VNumberArray) array, toAppendTo, pos);
174+
}
175+
176+
if (array instanceof VStringArray) {
177+
return format(((VStringArray) array).getData(), toAppendTo, pos);
178+
}
179+
180+
// if (array instanceof VBooleanArray) {
181+
// return format(((VBooleanArray) array).getData(), toAppendTo, pos);
182+
// }
183+
184+
// if (array instanceof VEnumArray) {
185+
// return format(((VEnumArray) array).getData(), toAppendTo, pos);
186+
// }
187+
188+
throw new UnsupportedOperationException("Type " + array.getClass().getName() + " not yet supported.");
189+
}
190+
}

0 commit comments

Comments
 (0)