1
16
17 package com.zaxxer.hikari.util;
18
19 import java.lang.reflect.Constructor;
20 import java.util.Locale;
21 import java.util.concurrent.*;
22
23 import static java.lang.Thread.currentThread;
24 import static java.util.concurrent.TimeUnit.SECONDS;
25
26
30 public final class UtilityElf
31 {
32
36 public static String getNullIfEmpty(final String text)
37 {
38 return text == null ? null : text.trim().isEmpty() ? null : text.trim();
39 }
40
41
46 public static void quietlySleep(final long millis)
47 {
48 try {
49 Thread.sleep(millis);
50 }
51 catch (InterruptedException e) {
52
53 currentThread().interrupt();
54 }
55 }
56
57
63 public static boolean safeIsAssignableFrom(Object obj, String className) {
64 try {
65 Class<?> clazz = Class.forName(className);
66 return clazz.isAssignableFrom(obj.getClass());
67 } catch (ClassNotFoundException ignored) {
68 return false;
69 }
70 }
71
72
82 public static <T> T createInstance(final String className, final Class<T> clazz, final Object... args)
83 {
84 if (className == null) {
85 return null;
86 }
87
88 try {
89 Class<?> loaded = UtilityElf.class.getClassLoader().loadClass(className);
90 if (args.length == 0) {
91 return clazz.cast(loaded.newInstance());
92 }
93
94 Class<?>[] argClasses = new Class<?>[args.length];
95 for (int i = 0; i < args.length; i++) {
96 argClasses[i] = args[i].getClass();
97 }
98 Constructor<?> constructor = loaded.getConstructor(argClasses);
99 return clazz.cast(constructor.newInstance(args));
100 }
101 catch (Exception e) {
102 throw new RuntimeException(e);
103 }
104 }
105
106
115 public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName, ThreadFactory threadFactory, final RejectedExecutionHandler policy)
116 {
117 if (threadFactory == null) {
118 threadFactory = new DefaultThreadFactory(threadName, true);
119 }
120
121 LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize);
122 ThreadPoolExecutor executor = new ThreadPoolExecutor(1 , 1 , 5 , SECONDS, queue, threadFactory, policy);
123 executor.allowCoreThreadTimeOut(true);
124 return executor;
125 }
126
127
136 public static ThreadPoolExecutor createThreadPoolExecutor(final BlockingQueue<Runnable> queue, final String threadName, ThreadFactory threadFactory, final RejectedExecutionHandler policy)
137 {
138 if (threadFactory == null) {
139 threadFactory = new DefaultThreadFactory(threadName, true);
140 }
141
142 ThreadPoolExecutor executor = new ThreadPoolExecutor(1 , 1 , 5 , SECONDS, queue, threadFactory, policy);
143 executor.allowCoreThreadTimeOut(true);
144 return executor;
145 }
146
147
148
149
150
151
157 public static int getTransactionIsolation(final String transactionIsolationName)
158 {
159 if (transactionIsolationName != null) {
160 try {
161
162 final String upperCaseIsolationLevelName = transactionIsolationName.toUpperCase(Locale.ENGLISH);
163 return IsolationLevel.valueOf(upperCaseIsolationLevelName).getLevelId();
164 } catch (IllegalArgumentException e) {
165
166 try {
167 final int level = Integer.parseInt(transactionIsolationName);
168 for (IsolationLevel iso : IsolationLevel.values()) {
169 if (iso.getLevelId() == level) {
170 return iso.getLevelId();
171 }
172 }
173
174 throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName);
175 }
176 catch (NumberFormatException nfe) {
177 throw new IllegalArgumentException("Invalid transaction isolation value: " + transactionIsolationName, nfe);
178 }
179 }
180 }
181
182 return -1;
183 }
184
185 public static final class DefaultThreadFactory implements ThreadFactory {
186
187 private final String threadName;
188 private final boolean daemon;
189
190 public DefaultThreadFactory(String threadName, boolean daemon) {
191 this.threadName = threadName;
192 this.daemon = daemon;
193 }
194
195 @Override
196 public Thread newThread(Runnable r) {
197 Thread thread = new Thread(r, threadName);
198 thread.setDaemon(daemon);
199 return thread;
200 }
201 }
202 }
203