Skip to content

Commit fffd0ba

Browse files
committed
vtype: minor cleanup
1 parent eefc55d commit fffd0ba

2 files changed

Lines changed: 155 additions & 162 deletions

File tree

epics-vtype/vtype/src/main/java/org/epics/vtype/VNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public final String toString() {
5757
* @param display the display
5858
* @return the new number
5959
*/
60-
public static VNumber of(Number value, Alarm alarm, Time time, Display display){
60+
public static VNumber of(Number value, Alarm alarm, Time time, Display display) {
6161
if (value instanceof Double) {
6262
return VDouble.of((Double) value, alarm, time, display);
6363
} else if (value instanceof Float) {
Lines changed: 154 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,154 @@
1-
/**
2-
* Copyright (C) 2010-14 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.util.Arrays;
8-
import java.util.Collection;
9-
import java.util.List;
10-
import org.epics.util.array.CollectionNumbers;
11-
import org.epics.util.array.ListNumber;
12-
import org.epics.util.array.ListNumbers;
13-
14-
/**
15-
* Tag interface to mark all the members of the value classes.
16-
*
17-
* @author carcassi
18-
*/
19-
public abstract class VType {
20-
21-
// private static Collection<Class<?>> types = Arrays.<Class<?>>asList(VByte.class, VByteArray.class, VDouble.class,
22-
// VDoubleArray.class, VEnum.class, VEnumArray.class, VFloat.class, VFloatArray.class,
23-
// VLong.class, VLongArray.class, VInt.class, VIntArray.class, VMultiDouble.class, VMultiEnum.class,
24-
// VMultiInt.class, VMultiString.class, VShort.class, VShortArray.class,
25-
// VStatistics.class, VString.class, VStringArray.class, VBoolean.class, VBooleanArray.class, VTable.class,
26-
// VImage.class);
27-
private static final Collection<Class<?>> types = Arrays.<Class<?>>asList(
28-
VByte.class,
29-
VUByte.class,
30-
VShort.class,
31-
VUShort.class,
32-
VInt.class,
33-
VUInt.class,
34-
VLong.class,
35-
VULong.class,
36-
VFloat.class,
37-
VDouble.class,
38-
VEnum.class,
39-
VString.class,
40-
VByteArray.class,
41-
VUByteArray.class,
42-
VShortArray.class,
43-
VUShortArray.class,
44-
VIntArray.class,
45-
VUIntArray.class,
46-
VLongArray.class,
47-
VULongArray.class,
48-
VFloatArray.class,
49-
VDoubleArray.class);
50-
51-
/**
52-
* Returns the type of the object by returning the class object of one
53-
* of the VXxx interfaces. The getClass() methods returns the
54-
* concrete implementation type, which is of little use. If no
55-
* super-interface is found, Object.class is used.
56-
*
57-
* @param obj an object implementing a standard type
58-
* @return the type is implementing
59-
*/
60-
public static Class<?> typeOf(Object obj) {
61-
if (obj == null)
62-
return null;
63-
64-
for (Class<?> type : types) {
65-
if (type.isInstance(obj)) {
66-
return type;
67-
}
68-
}
69-
70-
return Object.class;
71-
}
72-
73-
/**
74-
* Converts a standard java type to VTypes. Returns null if no conversion
75-
* is possible. Calls {@link #toVType(java.lang.Object, org.epics.vtype.Alarm, org.epics.vtype.Time, org.epics.vtype.Display) }
76-
* with no alarm, time now and no display.
77-
*
78-
* @param javaObject the value to wrap
79-
* @return the new VType value
80-
*/
81-
public static VType toVType(Object javaObject) {
82-
return toVType(javaObject, Alarm.none(), Time.now(), Display.none());
83-
}
84-
85-
/**
86-
* Converts a standard java type to VTypes. Returns null if no conversion
87-
* is possible. Calls {@link #toVType(java.lang.Object, org.epics.vtype.Alarm, org.epics.vtype.Time, org.epics.vtype.Display) }
88-
* with the given alarm, time now and no display.
89-
*
90-
* @param javaObject the value to wrap
91-
* @param alarm the alarm
92-
* @return the new VType value
93-
*/
94-
public static VType toVType(Object javaObject, Alarm alarm) {
95-
return toVType(javaObject, alarm, Time.now(), Display.none());
96-
}
97-
98-
/**
99-
* Converts a standard java type to VTypes. Returns null if no conversion
100-
* is possible.
101-
* <p>
102-
* Types are converted as follow:
103-
* <ul>
104-
* <li>Boolean -&gt; VBoolean</li>
105-
* <li>Number -&gt; corresponding VNumber</li>
106-
* <li>String -&gt; VString</li>
107-
* <li>number array -&gt; corresponding VNumberArray</li>
108-
* <li>ListNumber -&gt; corresponding VNumberArray</li>
109-
* <li>List -&gt; if all elements are String, VStringArray</li>
110-
* </ul>
111-
*
112-
* @param javaObject the value to wrap
113-
* @param alarm the alarm
114-
* @param time the time
115-
* @param display the display
116-
* @return the new VType value
117-
*/
118-
public static VType toVType(Object javaObject, Alarm alarm, Time time, Display display) {
119-
if (javaObject instanceof Number) {
120-
return VNumber.of((Number) javaObject, alarm, time, display);
121-
} else if (javaObject instanceof String) {
122-
return VString.of((String) javaObject, alarm, time);
123-
} else if (javaObject instanceof Boolean) {
124-
return null;//newVBoolean((Boolean) javaObject, alarm, time);
125-
} else if (javaObject instanceof byte[]
126-
|| javaObject instanceof short[]
127-
|| javaObject instanceof int[]
128-
|| javaObject instanceof long[]
129-
|| javaObject instanceof float[]
130-
|| javaObject instanceof double[]) {
131-
return VNumberArray.of(CollectionNumbers.toList(javaObject), alarm, time, display);
132-
} else if (javaObject instanceof ListNumber) {
133-
return VNumberArray.of((ListNumber) javaObject, alarm, time, display);
134-
} else if (javaObject instanceof String[]) {
135-
return null;//newVStringArray(Arrays.asList((String[]) javaObject), alarm, time);
136-
} else if (javaObject instanceof List) {
137-
boolean matches = true;
138-
List list = (List) javaObject;
139-
for (Object object : list) {
140-
if (!(object instanceof String)) {
141-
matches = false;
142-
}
143-
}
144-
if (matches) {
145-
@SuppressWarnings("unchecked")
146-
List<String> newList = (List<String>) list;
147-
return null;//newVStringArray(Collections.unmodifiableList(newList), alarm, time);
148-
} else {
149-
return null;
150-
}
151-
} else {
152-
return null;
153-
}
154-
}
155-
156-
static void argumentNotNull(String argName, Object value) {
157-
if (value == null) {
158-
throw new NullPointerException(argName + " can't be null");
159-
}
160-
}
161-
}
1+
/**
2+
* Copyright (C) 2010-14 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.util.Arrays;
8+
import java.util.Collection;
9+
import java.util.List;
10+
import org.epics.util.array.CollectionNumbers;
11+
import org.epics.util.array.ListNumber;
12+
13+
/**
14+
* Tag interface to mark all the members of the value classes.
15+
*
16+
* @author carcassi
17+
*/
18+
public abstract class VType {
19+
20+
private static final Collection<Class<?>> TYPES = Arrays.<Class<?>>asList(
21+
VDouble.class,
22+
VFloat.class,
23+
VULong.class,
24+
VLong.class,
25+
VUInt.class,
26+
VInt.class,
27+
VUShort.class,
28+
VShort.class,
29+
VUByte.class,
30+
VByte.class,
31+
VEnum.class,
32+
VString.class,
33+
VDoubleArray.class,
34+
VFloatArray.class,
35+
VULongArray.class,
36+
VLongArray.class,
37+
VUIntArray.class,
38+
VIntArray.class,
39+
VUShortArray.class,
40+
VShortArray.class,
41+
VUByteArray.class,
42+
VByteArray.class);
43+
44+
/**
45+
* Returns the type of the object by returning the class object of one
46+
* of the VXxx interfaces. The getClass() methods returns the
47+
* concrete implementation type, which is of little use. If no
48+
* super-interface is found, Object.class is used.
49+
*
50+
* @param obj an object implementing a standard type
51+
* @return the type is implementing
52+
*/
53+
public static Class<?> typeOf(Object obj) {
54+
if (obj == null)
55+
return null;
56+
57+
for (Class<?> type : TYPES) {
58+
if (type.isInstance(obj)) {
59+
return type;
60+
}
61+
}
62+
63+
return Object.class;
64+
}
65+
66+
/**
67+
* Converts a standard java type to VTypes. Returns null if no conversion
68+
* is possible. Calls {@link #toVType(java.lang.Object, org.epics.vtype.Alarm, org.epics.vtype.Time, org.epics.vtype.Display) }
69+
* with no alarm, time now and no display.
70+
*
71+
* @param javaObject the value to wrap
72+
* @return the new VType value
73+
*/
74+
public static VType toVType(Object javaObject) {
75+
return toVType(javaObject, Alarm.none(), Time.now(), Display.none());
76+
}
77+
78+
/**
79+
* Converts a standard java type to VTypes. Returns null if no conversion
80+
* is possible. Calls {@link #toVType(java.lang.Object, org.epics.vtype.Alarm, org.epics.vtype.Time, org.epics.vtype.Display) }
81+
* with the given alarm, time now and no display.
82+
*
83+
* @param javaObject the value to wrap
84+
* @param alarm the alarm
85+
* @return the new VType value
86+
*/
87+
public static VType toVType(Object javaObject, Alarm alarm) {
88+
return toVType(javaObject, alarm, Time.now(), Display.none());
89+
}
90+
91+
/**
92+
* Converts a standard java type to VTypes. Returns null if no conversion
93+
* is possible.
94+
* <p>
95+
* Types are converted as follow:
96+
* <ul>
97+
* <li>Boolean -&gt; VBoolean</li>
98+
* <li>Number -&gt; corresponding VNumber</li>
99+
* <li>String -&gt; VString</li>
100+
* <li>number array -&gt; corresponding VNumberArray</li>
101+
* <li>ListNumber -&gt; corresponding VNumberArray</li>
102+
* <li>List -&gt; if all elements are String, VStringArray</li>
103+
* </ul>
104+
*
105+
* @param javaObject the value to wrap
106+
* @param alarm the alarm
107+
* @param time the time
108+
* @param display the display
109+
* @return the new VType value
110+
*/
111+
public static VType toVType(Object javaObject, Alarm alarm, Time time, Display display) {
112+
if (javaObject instanceof Number) {
113+
return VNumber.of((Number) javaObject, alarm, time, display);
114+
} else if (javaObject instanceof String) {
115+
return VString.of((String) javaObject, alarm, time);
116+
} else if (javaObject instanceof Boolean) {
117+
return null;//newVBoolean((Boolean) javaObject, alarm, time);
118+
} else if (javaObject instanceof byte[]
119+
|| javaObject instanceof short[]
120+
|| javaObject instanceof int[]
121+
|| javaObject instanceof long[]
122+
|| javaObject instanceof float[]
123+
|| javaObject instanceof double[]) {
124+
return VNumberArray.of(CollectionNumbers.toList(javaObject), alarm, time, display);
125+
} else if (javaObject instanceof ListNumber) {
126+
return VNumberArray.of((ListNumber) javaObject, alarm, time, display);
127+
} else if (javaObject instanceof String[]) {
128+
return null;//newVStringArray(Arrays.asList((String[]) javaObject), alarm, time);
129+
} else if (javaObject instanceof List) {
130+
boolean matches = true;
131+
List list = (List) javaObject;
132+
for (Object object : list) {
133+
if (!(object instanceof String)) {
134+
matches = false;
135+
}
136+
}
137+
if (matches) {
138+
@SuppressWarnings("unchecked")
139+
List<String> newList = (List<String>) list;
140+
return null;//newVStringArray(Collections.unmodifiableList(newList), alarm, time);
141+
} else {
142+
return null;
143+
}
144+
} else {
145+
return null;
146+
}
147+
}
148+
149+
static void argumentNotNull(String argName, Object value) {
150+
if (value == null) {
151+
throw new NullPointerException(argName + " can't be null");
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)