|
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 -> VBoolean</li> |
98 | | - * <li>Number -> corresponding VNumber</li> |
99 | | - * <li>String -> VString</li> |
100 | | - * <li>number array -> corresponding VNumberArray</li> |
101 | | - * <li>ListNumber -> corresponding VNumberArray</li> |
102 | | - * <li>List -> 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 | | -} |
| 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 | + VImage.class); |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the type of the object by returning the class object of one |
| 47 | + * of the VXxx interfaces. The getClass() methods returns the |
| 48 | + * concrete implementation type, which is of little use. If no |
| 49 | + * super-interface is found, Object.class is used. |
| 50 | + * |
| 51 | + * @param obj an object implementing a standard type |
| 52 | + * @return the type is implementing |
| 53 | + */ |
| 54 | + public static Class<?> typeOf(Object obj) { |
| 55 | + if (obj == null) |
| 56 | + return null; |
| 57 | + |
| 58 | + for (Class<?> type : TYPES) { |
| 59 | + if (type.isInstance(obj)) { |
| 60 | + return type; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return Object.class; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Converts a standard java type to VTypes. Returns null if no conversion |
| 69 | + * is possible. Calls {@link #toVType(java.lang.Object, org.epics.vtype.Alarm, org.epics.vtype.Time, org.epics.vtype.Display) } |
| 70 | + * with no alarm, time now and no display. |
| 71 | + * |
| 72 | + * @param javaObject the value to wrap |
| 73 | + * @return the new VType value |
| 74 | + */ |
| 75 | + public static VType toVType(Object javaObject) { |
| 76 | + return toVType(javaObject, Alarm.none(), Time.now(), Display.none()); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Converts a standard java type to VTypes. Returns null if no conversion |
| 81 | + * is possible. Calls {@link #toVType(java.lang.Object, org.epics.vtype.Alarm, org.epics.vtype.Time, org.epics.vtype.Display) } |
| 82 | + * with the given alarm, time now and no display. |
| 83 | + * |
| 84 | + * @param javaObject the value to wrap |
| 85 | + * @param alarm the alarm |
| 86 | + * @return the new VType value |
| 87 | + */ |
| 88 | + public static VType toVType(Object javaObject, Alarm alarm) { |
| 89 | + return toVType(javaObject, alarm, Time.now(), Display.none()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Converts a standard java type to VTypes. Returns null if no conversion |
| 94 | + * is possible. |
| 95 | + * <p> |
| 96 | + * Types are converted as follow: |
| 97 | + * <ul> |
| 98 | + * <li>Boolean -> VBoolean</li> |
| 99 | + * <li>Number -> corresponding VNumber</li> |
| 100 | + * <li>String -> VString</li> |
| 101 | + * <li>number array -> corresponding VNumberArray</li> |
| 102 | + * <li>ListNumber -> corresponding VNumberArray</li> |
| 103 | + * <li>List -> if all elements are String, VStringArray</li> |
| 104 | + * </ul> |
| 105 | + * |
| 106 | + * @param javaObject the value to wrap |
| 107 | + * @param alarm the alarm |
| 108 | + * @param time the time |
| 109 | + * @param display the display |
| 110 | + * @return the new VType value |
| 111 | + */ |
| 112 | + public static VType toVType(Object javaObject, Alarm alarm, Time time, Display display) { |
| 113 | + if (javaObject instanceof Number) { |
| 114 | + return VNumber.of((Number) javaObject, alarm, time, display); |
| 115 | + } else if (javaObject instanceof String) { |
| 116 | + return VString.of((String) javaObject, alarm, time); |
| 117 | + } else if (javaObject instanceof Boolean) { |
| 118 | + return null;//newVBoolean((Boolean) javaObject, alarm, time); |
| 119 | + } else if (javaObject instanceof byte[] |
| 120 | + || javaObject instanceof short[] |
| 121 | + || javaObject instanceof int[] |
| 122 | + || javaObject instanceof long[] |
| 123 | + || javaObject instanceof float[] |
| 124 | + || javaObject instanceof double[]) { |
| 125 | + return VNumberArray.of(CollectionNumbers.toList(javaObject), alarm, time, display); |
| 126 | + } else if (javaObject instanceof ListNumber) { |
| 127 | + return VNumberArray.of((ListNumber) javaObject, alarm, time, display); |
| 128 | + } else if (javaObject instanceof String[]) { |
| 129 | + return null;//newVStringArray(Arrays.asList((String[]) javaObject), alarm, time); |
| 130 | + } else if (javaObject instanceof List) { |
| 131 | + boolean matches = true; |
| 132 | + List list = (List) javaObject; |
| 133 | + for (Object object : list) { |
| 134 | + if (!(object instanceof String)) { |
| 135 | + matches = false; |
| 136 | + } |
| 137 | + } |
| 138 | + if (matches) { |
| 139 | + @SuppressWarnings("unchecked") |
| 140 | + List<String> newList = (List<String>) list; |
| 141 | + return null;//newVStringArray(Collections.unmodifiableList(newList), alarm, time); |
| 142 | + } else { |
| 143 | + return null; |
| 144 | + } |
| 145 | + } else { |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + static void argumentNotNull(String argName, Object value) { |
| 151 | + if (value == null) { |
| 152 | + throw new NullPointerException(argName + " can't be null"); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments