1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jadira.usertype.unitsofmeasurement.indriya;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.measure.Unit;
22
23 import org.hibernate.SessionFactory;
24 import org.jadira.usertype.spi.shared.AbstractParameterizedUserType;
25 import org.jadira.usertype.spi.shared.ValidTypesConfigured;
26 import org.jadira.usertype.spi.utils.reflection.ClassLoaderUtils;
27 import org.jadira.usertype.unitsofmeasurement.indriya.columnmapper.StringColumnUnitMapper;
28
29
30
31
32
33
34 public class PersistentUnit extends AbstractParameterizedUserType<Unit<?>, String, StringColumnUnitMapper> {
35
36 private static final long serialVersionUID = -2015829087239519037L;
37
38 @Override
39 public void applyConfiguration(SessionFactory sessionFactory) {
40 super.applyConfiguration(sessionFactory);
41 doApplyConfiguration();
42 }
43
44 private <Z> void doApplyConfiguration() {
45
46
47
48
49
50
51
52 ValidTypesConfigured<Unit<?>> next = (ValidTypesConfigured<Unit<?>>)getColumnMapper();
53 performValidTypesConfiguration(next);
54
55 }
56
57 private void performValidTypesConfiguration(ValidTypesConfigured<Unit<?>> next) {
58
59 String validTypesString = null;
60 if (getParameterValues() != null) {
61 validTypesString = getParameterValues().getProperty("validTypes");
62 }
63 if (validTypesString != null) {
64 String[] validTypes = validTypesString.split(",");
65 List<Class<Unit<?>>> units = new ArrayList<>();
66 for (String nextType : validTypes) {
67
68
69 try {
70 @SuppressWarnings("unchecked")
71 Class<Unit<?>> nextClass = (Class<Unit<?>>)(ClassLoaderUtils.classForName(nextType));
72 units.add(nextClass);
73 } catch (ClassNotFoundException e) {
74 throw new IllegalStateException("Cannot find specified class " + nextType, e);
75 }
76
77 }
78 next.setValidTypes(units);
79 }
80 }
81 }