blob: 596e34bdb4501f6a744ff1f717a645a6cf84d173 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom5f38b3a2014-08-27 23:50:54 -070016package org.onlab.util;
17
Ray Milkey86ee5e82018-04-02 15:33:07 -070018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hartcc962d82016-08-09 16:52:22 -070020import com.google.common.base.Charsets;
21import com.google.common.base.Strings;
22import com.google.common.collect.Lists;
23import com.google.common.primitives.UnsignedLongs;
24import com.google.common.util.concurrent.ThreadFactoryBuilder;
25import org.slf4j.Logger;
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080026
tom53efab52014-10-07 17:43:48 -070027import java.io.File;
tom53efab52014-10-07 17:43:48 -070028import java.io.IOException;
Ray Milkey86ee5e82018-04-02 15:33:07 -070029import java.io.InputStream;
Madan Jampani27b69c62015-05-15 15:49:02 -070030import java.nio.ByteBuffer;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031import java.nio.file.FileVisitResult;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080032import java.nio.file.Files;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080033import java.nio.file.Path;
34import java.nio.file.Paths;
35import java.nio.file.SimpleFileVisitor;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080036import java.nio.file.StandardCopyOption;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import java.nio.file.attribute.BasicFileAttributes;
Ray Milkeyb68bbbc2017-12-18 10:05:49 -080038import java.security.SecureRandom;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070039import java.time.Instant;
40import java.time.OffsetDateTime;
41import java.time.ZoneId;
Madan Jampani27b69c62015-05-15 15:49:02 -070042import java.util.Arrays;
Brian O'Connore2eac102015-02-12 18:30:22 -080043import java.util.Collection;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070044import java.util.Dictionary;
tom53efab52014-10-07 17:43:48 -070045import java.util.List;
Sho SHIMIZUb5638b82016-02-11 14:55:05 -080046import java.util.Optional;
Thomas Vachuskaadba1522015-06-04 15:08:30 -070047import java.util.Random;
Ray Milkey36992c82015-11-17 13:31:15 -080048import java.util.Set;
Madan Jampani27b69c62015-05-15 15:49:02 -070049import java.util.concurrent.CompletableFuture;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070050import java.util.concurrent.ExecutionException;
Jordan Halterman9bdc24f2017-04-19 23:45:12 -070051import java.util.concurrent.Executor;
Charles Chan9797ebb2020-02-14 13:23:57 -080052import java.util.concurrent.Executors;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070053import java.util.concurrent.Future;
Charles Chan9797ebb2020-02-14 13:23:57 -080054import java.util.concurrent.ScheduledExecutorService;
tom5f38b3a2014-08-27 23:50:54 -070055import java.util.concurrent.ThreadFactory;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070056import java.util.concurrent.TimeUnit;
57import java.util.concurrent.TimeoutException;
Madan Jampani307a21e2016-09-01 15:49:47 -070058import java.util.function.BinaryOperator;
Madan Jampania29c6772015-08-17 13:17:07 -070059import java.util.function.Function;
60import java.util.function.Supplier;
Sho SHIMIZU85803e22016-01-13 21:53:43 -080061import java.util.stream.Collectors;
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -070062import java.util.stream.Stream;
63import java.util.stream.StreamSupport;
tom5f38b3a2014-08-27 23:50:54 -070064
Yuta HIGUCHI47d96092017-11-17 14:05:26 -080065import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hartcc962d82016-08-09 16:52:22 -070066import static java.nio.file.Files.delete;
67import static java.nio.file.Files.walkFileTree;
68import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory;
69import static org.slf4j.LoggerFactory.getLogger;
Ray Milkey705d9bc2014-11-18 08:19:00 -080070
Thomas Vachuskac13b90a2015-02-18 18:19:55 -080071/**
72 * Miscellaneous utility methods.
73 */
tom5f38b3a2014-08-27 23:50:54 -070074public abstract class Tools {
75
76 private Tools() {
77 }
78
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 private static final Logger log = getLogger(Tools.class);
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080080
Ray Milkeyb68bbbc2017-12-18 10:05:49 -080081 private static Random random = new SecureRandom();
Thomas Vachuskaadba1522015-06-04 15:08:30 -070082
Ray Milkey86ee5e82018-04-02 15:33:07 -070083 private static final String INPUT_JSON_CANNOT_BE_NULL = "Input JSON cannot be null";
84
Charles Chan9797ebb2020-02-14 13:23:57 -080085 private static ScheduledExecutorService timer = Executors.newScheduledThreadPool(
86 Runtime.getRuntime().availableProcessors(), groupedThreads("onos/tool", "timer"));
87
tom5f38b3a2014-08-27 23:50:54 -070088 /**
89 * Returns a thread factory that produces threads named according to the
90 * supplied name pattern.
91 *
92 * @param pattern name pattern
93 * @return thread factory
94 */
95 public static ThreadFactory namedThreads(String pattern) {
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080096 return new ThreadFactoryBuilder()
97 .setNameFormat(pattern)
Thomas Vachuska480adad2015-03-06 10:27:09 -080098 .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
99 .build();
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -0800100 }
Yuta HIGUCHI683e9782014-11-25 17:26:36 -0800101
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -0800102 /**
103 * Returns a thread factory that produces threads named according to the
104 * supplied name pattern and from the specified thread-group. The thread
105 * group name is expected to be specified in slash-delimited format, e.g.
Thomas Vachuskac13b90a2015-02-18 18:19:55 -0800106 * {@code onos/intent}. The thread names will be produced by converting
107 * the thread group name into dash-delimited format and pre-pended to the
108 * specified pattern.
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -0800109 *
110 * @param groupName group name in slash-delimited format to indicate hierarchy
111 * @param pattern name pattern
112 * @return thread factory
113 */
114 public static ThreadFactory groupedThreads(String groupName, String pattern) {
Jian Li03e9fb02016-03-01 17:13:54 -0800115 return groupedThreads(groupName, pattern, log);
116 }
117
118 /**
119 * Returns a thread factory that produces threads named according to the
120 * supplied name pattern and from the specified thread-group. The thread
121 * group name is expected to be specified in slash-delimited format, e.g.
122 * {@code onos/intent}. The thread names will be produced by converting
123 * the thread group name into dash-delimited format and pre-pended to the
124 * specified pattern. If a logger is specified, it will use the logger to
125 * print out the exception if it has any.
126 *
127 * @param groupName group name in slash-delimited format to indicate hierarchy
128 * @param pattern name pattern
129 * @param logger logger
130 * @return thread factory
131 */
132 public static ThreadFactory groupedThreads(String groupName, String pattern, Logger logger) {
133 if (logger == null) {
134 return groupedThreads(groupName, pattern);
135 }
Thomas Vachuska9c17a6d2015-02-17 23:36:43 -0800136 return new ThreadFactoryBuilder()
137 .setThreadFactory(groupedThreadFactory(groupName))
Thomas Vachuskac13b90a2015-02-18 18:19:55 -0800138 .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern)
Jian Li03e9fb02016-03-01 17:13:54 -0800139 .setUncaughtExceptionHandler((t, e) -> logger.error("Uncaught exception on " + t.getName(), e))
Thomas Vachuska480adad2015-03-06 10:27:09 -0800140 .build();
tom5f38b3a2014-08-27 23:50:54 -0700141 }
142
tom782a7cf2014-09-11 23:58:38 -0700143 /**
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800144 * Returns a thread factory that produces threads with MIN_PRIORITY.
145 *
146 * @param factory backing ThreadFactory
147 * @return thread factory
148 */
149 public static ThreadFactory minPriority(ThreadFactory factory) {
150 return new ThreadFactoryBuilder()
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800151 .setThreadFactory(factory)
152 .setPriority(Thread.MIN_PRIORITY)
153 .build();
Yuta HIGUCHI06586272014-11-25 14:27:03 -0800154 }
155
156 /**
Yuta HIGUCHIa2a11cd2016-12-19 14:19:11 -0800157 * Returns a thread factory that produces threads with MAX_PRIORITY.
158 *
159 * @param factory backing ThreadFactory
160 * @return thread factory
161 */
162 public static ThreadFactory maxPriority(ThreadFactory factory) {
163 return new ThreadFactoryBuilder()
164 .setThreadFactory(factory)
165 .setPriority(Thread.MAX_PRIORITY)
166 .build();
167 }
168
169 /**
Brian O'Connore2eac102015-02-12 18:30:22 -0800170 * Returns true if the collection is null or is empty.
171 *
172 * @param collection collection to test
173 * @return true if null or empty; false otherwise
174 */
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800175 public static boolean isNullOrEmpty(Collection<?> collection) {
Brian O'Connore2eac102015-02-12 18:30:22 -0800176 return collection == null || collection.isEmpty();
177 }
178
179 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700180 * Returns the specified item if that item is not null; otherwise throws
Thomas Vachuskaca88bb72015-04-08 19:38:02 -0700181 * not found exception.
182 *
183 * @param item item to check
184 * @param message not found message
185 * @param <T> item type
186 * @return item if not null
187 * @throws org.onlab.util.ItemNotFoundException if item is null
188 */
189 public static <T> T nullIsNotFound(T item, String message) {
190 if (item == null) {
191 throw new ItemNotFoundException(message);
192 }
193 return item;
194 }
195
196 /**
Ray Milkey36992c82015-11-17 13:31:15 -0800197 * Returns the specified set if the set is not null and not empty;
198 * otherwise throws a not found exception.
199 *
200 * @param item set to check
201 * @param message not found message
202 * @param <T> Set item type
203 * @return item if not null and not empty
204 * @throws org.onlab.util.ItemNotFoundException if set is null or empty
205 */
206 public static <T> Set<T> emptyIsNotFound(Set<T> item, String message) {
207 if (item == null || item.isEmpty()) {
208 throw new ItemNotFoundException(message);
209 }
210 return item;
211 }
212
213 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700214 * Returns the specified item if that item is not null; otherwise throws
215 * bad argument exception.
216 *
217 * @param item item to check
218 * @param message not found message
219 * @param <T> item type
220 * @return item if not null
221 * @throws IllegalArgumentException if item is null
222 */
223 public static <T> T nullIsIllegal(T item, String message) {
224 if (item == null) {
225 throw new IllegalArgumentException(message);
226 }
227 return item;
228 }
229
230 /**
Ray Milkey86ee5e82018-04-02 15:33:07 -0700231 * Utility to convert a mapper and an input stream into a JSON tree,
232 * and be tolerant of a null tree being returned.
233 *
234 * @param mapper JSON object mapper
235 * @param stream IO stream containing the JSON
236 * @return object node for the given
237 * @throws IOException if JSON parsing fails
238 */
239 public static ObjectNode readTreeFromStream(ObjectMapper mapper, InputStream stream) throws IOException {
240 return nullIsIllegal((ObjectNode) mapper.readTree(stream), INPUT_JSON_CANNOT_BE_NULL);
241 }
242
243 /**
tom782a7cf2014-09-11 23:58:38 -0700244 * Converts a string from hex to long.
245 *
246 * @param string hex number in string form; sans 0x
247 * @return long value
248 */
249 public static long fromHex(String string) {
250 return UnsignedLongs.parseUnsignedLong(string, 16);
251 }
252
253 /**
254 * Converts a long value to hex string; 16 wide and sans 0x.
255 *
256 * @param value long value
257 * @return hex string
258 */
259 public static String toHex(long value) {
260 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
261 }
262
263 /**
264 * Converts a long value to hex string; 16 wide and sans 0x.
265 *
266 * @param value long value
267 * @param width string width; zero padded
268 * @return hex string
269 */
270 public static String toHex(long value, int width) {
271 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
272 }
tomf110fff2014-09-26 00:38:18 -0700273
274 /**
Jonathan Hartcc962d82016-08-09 16:52:22 -0700275 * Returns a string encoding in hex of the given long value with prefix
276 * '0x'.
277 *
278 * @param value long value to encode as hex string
279 * @return hex string
280 */
281 public static String toHexWithPrefix(long value) {
282 return "0x" + Long.toHexString(value);
283 }
284
285 /**
Madan Jampanif2f086c2016-01-13 16:15:39 -0800286 * Returns the UTF-8 encoded byte[] representation of a String.
Jian Lidfba7392016-01-22 16:46:58 -0800287 * @param input input string
288 * @return UTF-8 encoded byte array
Madan Jampanif2f086c2016-01-13 16:15:39 -0800289 */
290 public static byte[] getBytesUtf8(String input) {
291 return input.getBytes(Charsets.UTF_8);
292 }
293
294 /**
295 * Returns the String representation of UTF-8 encoded byte[].
Jian Lidfba7392016-01-22 16:46:58 -0800296 * @param input input byte array
297 * @return UTF-8 encoded string
Madan Jampanif2f086c2016-01-13 16:15:39 -0800298 */
299 public static String toStringUtf8(byte[] input) {
300 return new String(input, Charsets.UTF_8);
301 }
302
303 /**
Madan Jampani9eb55d12015-08-14 07:47:56 -0700304 * Returns a copy of the input byte array.
305 *
306 * @param original input
307 * @return copy of original
308 */
309 public static byte[] copyOf(byte[] original) {
310 return Arrays.copyOf(original, original.length);
311 }
312
313 /**
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700314 * Get property as a string value.
315 *
316 * @param properties properties to be looked up
317 * @param propertyName the name of the property to look up
318 * @return value when the propertyName is defined or return null
319 */
320 public static String get(Dictionary<?, ?> properties, String propertyName) {
321 Object v = properties.get(propertyName);
322 String s = (v instanceof String) ? (String) v :
323 v != null ? v.toString() : null;
324 return Strings.isNullOrEmpty(s) ? null : s.trim();
325 }
326
327 /**
Jian Lid9b5f552016-03-11 18:15:31 -0800328 * Get Integer property from the propertyName
329 * Return null if propertyName is not found.
330 *
331 * @param properties properties to be looked up
332 * @param propertyName the name of the property to look up
333 * @return value when the propertyName is defined or return null
334 */
335 public static Integer getIntegerProperty(Dictionary<?, ?> properties,
336 String propertyName) {
337 Integer value;
338 try {
339 String s = get(properties, propertyName);
340 value = Strings.isNullOrEmpty(s) ? null : Integer.valueOf(s);
341 } catch (NumberFormatException | ClassCastException e) {
342 value = null;
343 }
344 return value;
345 }
346
347 /**
348 * Get Integer property from the propertyName
349 * Return default value if propertyName is not found.
350 *
351 * @param properties properties to be looked up
352 * @param propertyName the name of the property to look up
353 * @param defaultValue the default value that to be assigned
354 * @return value when the propertyName is defined or return default value
355 */
356 public static int getIntegerProperty(Dictionary<?, ?> properties,
357 String propertyName,
358 int defaultValue) {
359 try {
360 String s = get(properties, propertyName);
361 return Strings.isNullOrEmpty(s) ? defaultValue : Integer.valueOf(s);
362 } catch (NumberFormatException | ClassCastException e) {
363 return defaultValue;
364 }
365 }
366
367 /**
368 * Check property name is defined and set to true.
369 *
370 * @param properties properties to be looked up
371 * @param propertyName the name of the property to look up
372 * @return value when the propertyName is defined or return null
373 */
374 public static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
375 String propertyName) {
376 Boolean value;
377 try {
378 String s = get(properties, propertyName);
379 value = Strings.isNullOrEmpty(s) ? null : Boolean.valueOf(s);
380 } catch (ClassCastException e) {
381 value = null;
382 }
383 return value;
384 }
385
386 /**
387 * Check property name is defined as set to true.
388 *
389 * @param properties properties to be looked up
390 * @param propertyName the name of the property to look up
391 * @param defaultValue the default value that to be assigned
392 * @return value when the propertyName is defined or return the default value
393 */
394 public static boolean isPropertyEnabled(Dictionary<?, ?> properties,
395 String propertyName,
396 boolean defaultValue) {
397 try {
398 String s = get(properties, propertyName);
399 return Strings.isNullOrEmpty(s) ? defaultValue : Boolean.valueOf(s);
400 } catch (ClassCastException e) {
401 return defaultValue;
402 }
403 }
404
405 /**
tomf110fff2014-09-26 00:38:18 -0700406 * Suspends the current thread for a specified number of millis.
407 *
408 * @param ms number of millis
409 */
410 public static void delay(int ms) {
411 try {
412 Thread.sleep(ms);
413 } catch (InterruptedException e) {
Ray Milkey5c7d4882018-02-05 14:50:39 -0800414 Thread.currentThread().interrupt();
Ray Milkey986a47a2018-01-25 11:38:51 -0800415 throw new IllegalStateException("Interrupted", e);
tomf110fff2014-09-26 00:38:18 -0700416 }
417 }
418
tom53efab52014-10-07 17:43:48 -0700419 /**
sdn94b00152016-08-30 02:12:32 -0700420 * Get Long property from the propertyName
421 * Return null if propertyName is not found.
422 *
423 * @param properties properties to be looked up
424 * @param propertyName the name of the property to look up
425 * @return value when the propertyName is defined or return null
426 */
427 public static Long getLongProperty(Dictionary<?, ?> properties,
428 String propertyName) {
429 Long value;
430 try {
431 String s = get(properties, propertyName);
432 value = Strings.isNullOrEmpty(s) ? null : Long.valueOf(s);
433 } catch (NumberFormatException | ClassCastException e) {
434 value = null;
435 }
436 return value;
437 }
438
439 /**
Georgios Katsikas5e2d40f2018-07-29 18:28:01 +0200440 * Get Float property from the propertyName
441 * Return null if propertyName is not found.
442 *
443 * @param properties properties to be looked up
444 * @param propertyName the name of the property to look up
445 * @return value when the propertyName is defined or return null
446 */
447 public static Float getFloatProperty(Dictionary<?, ?> properties,
448 String propertyName) {
449 Float value;
450 try {
451 String s = get(properties, propertyName);
452 value = Strings.isNullOrEmpty(s) ? null : Float.valueOf(s);
453 } catch (NumberFormatException | ClassCastException e) {
454 value = null;
455 }
456 return value;
457 }
458
459 /**
Madan Jampania29c6772015-08-17 13:17:07 -0700460 * Returns a function that retries execution on failure.
461 * @param base base function
462 * @param exceptionClass type of exception for which to retry
463 * @param maxRetries max number of retries before giving up
464 * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from
465 * the interval (0, maxDelayBetweenRetries]
466 * @return function
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700467 * @param <U> type of function input
468 * @param <V> type of function output
Madan Jampania29c6772015-08-17 13:17:07 -0700469 */
470 public static <U, V> Function<U, V> retryable(Function<U, V> base,
471 Class<? extends Throwable> exceptionClass,
472 int maxRetries,
473 int maxDelayBetweenRetries) {
474 return new RetryingFunction<>(base, exceptionClass, maxRetries, maxDelayBetweenRetries);
475 }
476
477 /**
478 * Returns a Supplier that retries execution on failure.
479 * @param base base supplier
480 * @param exceptionClass type of exception for which to retry
481 * @param maxRetries max number of retries before giving up
482 * @param maxDelayBetweenRetries max delay between successive retries. The actual delay is randomly picked from
483 * the interval (0, maxDelayBetweenRetries]
484 * @return supplier
Thomas Vachuska87ae1d92015-08-19 17:39:11 -0700485 * @param <V> type of supplied result
Madan Jampania29c6772015-08-17 13:17:07 -0700486 */
487 public static <V> Supplier<V> retryable(Supplier<V> base,
488 Class<? extends Throwable> exceptionClass,
489 int maxRetries,
490 int maxDelayBetweenRetries) {
491 return () -> new RetryingFunction<>(v -> base.get(),
492 exceptionClass,
493 maxRetries,
494 maxDelayBetweenRetries).apply(null);
495 }
496
497 /**
Thomas Vachuskaadba1522015-06-04 15:08:30 -0700498 * Suspends the current thread for a random number of millis between 0 and
499 * the indicated limit.
500 *
501 * @param ms max number of millis
502 */
503 public static void randomDelay(int ms) {
504 try {
505 Thread.sleep(random.nextInt(ms));
506 } catch (InterruptedException e) {
Ray Milkey5c7d4882018-02-05 14:50:39 -0800507 Thread.currentThread().interrupt();
Ray Milkey986a47a2018-01-25 11:38:51 -0800508 throw new IllegalStateException("Interrupted", e);
Thomas Vachuskaadba1522015-06-04 15:08:30 -0700509 }
510 }
511
512 /**
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700513 * Suspends the current thread for a specified number of millis and nanos.
514 *
515 * @param ms number of millis
516 * @param nanos number of nanos
517 */
518 public static void delay(int ms, int nanos) {
519 try {
520 Thread.sleep(ms, nanos);
521 } catch (InterruptedException e) {
Ray Milkey5c7d4882018-02-05 14:50:39 -0800522 Thread.currentThread().interrupt();
Ray Milkey986a47a2018-01-25 11:38:51 -0800523 throw new IllegalStateException("Interrupted", e);
Thomas Vachuskac40d4632015-04-09 16:55:03 -0700524 }
525 }
526
527 /**
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800528 * Purges the specified directory path.&nbsp;Use with great caution since
529 * no attempt is made to check for symbolic links, which could result in
530 * deletion of unintended files.
531 *
532 * @param path directory to be removed
533 * @throws java.io.IOException if unable to remove contents
534 */
535 public static void removeDirectory(String path) throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800536 DirectoryDeleter visitor = new DirectoryDeleter();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700537 File dir = new File(path);
538 if (dir.exists() && dir.isDirectory()) {
539 walkFileTree(Paths.get(path), visitor);
540 if (visitor.exception != null) {
541 throw visitor.exception;
542 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800543 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800544 }
545
546 /**
547 * Purges the specified directory path.&nbsp;Use with great caution since
548 * no attempt is made to check for symbolic links, which could result in
549 * deletion of unintended files.
550 *
551 * @param dir directory to be removed
552 * @throws java.io.IOException if unable to remove contents
553 */
554 public static void removeDirectory(File dir) throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800555 DirectoryDeleter visitor = new DirectoryDeleter();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700556 if (dir.exists() && dir.isDirectory()) {
557 walkFileTree(Paths.get(dir.getAbsolutePath()), visitor);
558 if (visitor.exception != null) {
559 throw visitor.exception;
560 }
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800561 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800562 }
563
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800564 // Auxiliary path visitor for recursive directory structure removal.
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800565 private static class DirectoryDeleter extends SimpleFileVisitor<Path> {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800566
567 private IOException exception;
568
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800569 @Override
570 public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
571 throws IOException {
572 if (attributes.isRegularFile()) {
573 delete(file);
574 }
575 return FileVisitResult.CONTINUE;
576 }
577
578 @Override
579 public FileVisitResult postVisitDirectory(Path directory, IOException ioe)
580 throws IOException {
581 delete(directory);
582 return FileVisitResult.CONTINUE;
583 }
584
585 @Override
586 public FileVisitResult visitFileFailed(Path file, IOException ioe)
587 throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800588 this.exception = ioe;
589 return FileVisitResult.TERMINATE;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800590 }
591 }
592
Madan Jampani30a57f82015-03-02 12:19:41 -0800593 /**
594 * Returns a human friendly time ago string for a specified system time.
Thomas Vachuska480adad2015-03-06 10:27:09 -0800595 *
Madan Jampani30a57f82015-03-02 12:19:41 -0800596 * @param unixTime system time in millis
597 * @return human friendly time ago
598 */
599 public static String timeAgo(long unixTime) {
600 long deltaMillis = System.currentTimeMillis() - unixTime;
601 long secondsSince = (long) (deltaMillis / 1000.0);
602 long minsSince = (long) (deltaMillis / (1000.0 * 60));
603 long hoursSince = (long) (deltaMillis / (1000.0 * 60 * 60));
604 long daysSince = (long) (deltaMillis / (1000.0 * 60 * 60 * 24));
605 if (daysSince > 0) {
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800606 return String.format("%dd%dh ago", daysSince, hoursSince - daysSince * 24);
Madan Jampani30a57f82015-03-02 12:19:41 -0800607 } else if (hoursSince > 0) {
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800608 return String.format("%dh%dm ago", hoursSince, minsSince - hoursSince * 60);
Madan Jampani30a57f82015-03-02 12:19:41 -0800609 } else if (minsSince > 0) {
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800610 return String.format("%dm%ds ago", minsSince, secondsSince - minsSince * 60);
Madan Jampani30a57f82015-03-02 12:19:41 -0800611 } else if (secondsSince > 0) {
612 return String.format("%ds ago", secondsSince);
613 } else {
614 return "just now";
615 }
616 }
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800617
618 /**
619 * Copies the specified directory path.&nbsp;Use with great caution since
620 * no attempt is made to check for symbolic links, which could result in
621 * copy of unintended files.
622 *
623 * @param src directory to be copied
624 * @param dst destination directory to be removed
625 * @throws java.io.IOException if unable to remove contents
626 */
627 public static void copyDirectory(String src, String dst) throws IOException {
628 walkFileTree(Paths.get(src), new DirectoryCopier(src, dst));
629 }
630
631 /**
632 * Copies the specified directory path.&nbsp;Use with great caution since
633 * no attempt is made to check for symbolic links, which could result in
634 * copy of unintended files.
635 *
636 * @param src directory to be copied
637 * @param dst destination directory to be removed
638 * @throws java.io.IOException if unable to remove contents
639 */
640 public static void copyDirectory(File src, File dst) throws IOException {
641 walkFileTree(Paths.get(src.getAbsolutePath()),
642 new DirectoryCopier(src.getAbsolutePath(),
643 dst.getAbsolutePath()));
644 }
645
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700646 /**
647 * Returns the future value when complete or if future
648 * completes exceptionally returns the defaultValue.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700649 *
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700650 * @param future future
651 * @param defaultValue default value
652 * @param <T> future value type
653 * @return future value when complete or if future
654 * completes exceptionally returns the defaultValue.
655 */
656 public static <T> T futureGetOrElse(Future<T> future, T defaultValue) {
657 try {
658 return future.get();
659 } catch (InterruptedException e) {
660 Thread.currentThread().interrupt();
661 return defaultValue;
662 } catch (ExecutionException e) {
663 return defaultValue;
664 }
665 }
666
667 /**
668 * Returns the future value when complete or if future
669 * completes exceptionally returns the defaultValue.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700670 *
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700671 * @param future future
672 * @param timeout time to wait for successful completion
673 * @param timeUnit time unit
674 * @param defaultValue default value
675 * @param <T> future value type
676 * @return future value when complete or if future
677 * completes exceptionally returns the defaultValue.
678 */
679 public static <T> T futureGetOrElse(Future<T> future,
680 long timeout,
681 TimeUnit timeUnit,
682 T defaultValue) {
683 try {
684 return future.get(timeout, timeUnit);
685 } catch (InterruptedException e) {
686 Thread.currentThread().interrupt();
687 return defaultValue;
688 } catch (ExecutionException | TimeoutException e) {
689 return defaultValue;
690 }
691 }
692
Madan Jampani27b69c62015-05-15 15:49:02 -0700693 /**
694 * Returns a future that is completed exceptionally.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700695 *
Madan Jampani27b69c62015-05-15 15:49:02 -0700696 * @param t exception
697 * @param <T> future value type
698 * @return future
699 */
700 public static <T> CompletableFuture<T> exceptionalFuture(Throwable t) {
701 CompletableFuture<T> future = new CompletableFuture<>();
702 future.completeExceptionally(t);
703 return future;
704 }
705
706 /**
Charles Chan9797ebb2020-02-14 13:23:57 -0800707 * Returns a future that completes normally after given time period.
708 *
709 * @param timeout amount of time to wait before completing the future
710 * @param unit Time unit
711 * @return a future that completes after given time period
712 */
713 public static CompletableFuture<Void> completeAfter(long timeout, TimeUnit unit) {
714 CompletableFuture<Void> result = new CompletableFuture<>();
715 timer.schedule(() -> result.complete(null), timeout, unit);
716 return result;
717 }
718
719 /**
Jordan Halterman046faeb2017-05-01 15:10:13 -0700720 * Returns a future that's completed using the given {@code orderedExecutor} if the future is not blocked or the
721 * given {@code threadPoolExecutor} if the future is blocked.
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700722 * <p>
Jordan Halterman046faeb2017-05-01 15:10:13 -0700723 * This method allows futures to maintain single-thread semantics via the provided {@code orderedExecutor} while
724 * ensuring user code can block without blocking completion of futures. When the returned future or any of its
725 * descendants is blocked on a {@link CompletableFuture#get()} or {@link CompletableFuture#join()} call, completion
726 * of the returned future will be done using the provided {@code threadPoolExecutor}.
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700727 *
728 * @param future the future to convert into an asynchronous future
Jordan Halterman046faeb2017-05-01 15:10:13 -0700729 * @param orderedExecutor the ordered executor with which to attempt to complete the future
730 * @param threadPoolExecutor the backup executor with which to complete blocked futures
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700731 * @param <T> future value type
732 * @return a new completable future to be completed using the provided {@code executor} once the provided
733 * {@code future} is complete
734 */
Jordan Halterman046faeb2017-05-01 15:10:13 -0700735 public static <T> CompletableFuture<T> orderedFuture(
736 CompletableFuture<T> future,
737 Executor orderedExecutor,
738 Executor threadPoolExecutor) {
Jordan Haltermane265d372017-05-17 22:40:47 -0700739 if (future.isDone()) {
740 return future;
741 }
742
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700743 BlockingAwareFuture<T> newFuture = new BlockingAwareFuture<>();
Jordan Halterman046faeb2017-05-01 15:10:13 -0700744 future.whenComplete((result, error) -> {
745 Runnable completer = () -> {
746 if (future.isCompletedExceptionally()) {
747 newFuture.completeExceptionally(error);
748 } else {
749 newFuture.complete(result);
750 }
751 };
752
753 if (newFuture.isBlocked()) {
754 threadPoolExecutor.execute(completer);
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700755 } else {
Jordan Halterman046faeb2017-05-01 15:10:13 -0700756 orderedExecutor.execute(completer);
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700757 }
Jordan Halterman046faeb2017-05-01 15:10:13 -0700758 });
Jordan Halterman9bdc24f2017-04-19 23:45:12 -0700759 return newFuture;
760 }
761
762 /**
Sho SHIMIZU85803e22016-01-13 21:53:43 -0800763 * Returns a new CompletableFuture completed with a list of computed values
764 * when all of the given CompletableFuture complete.
765 *
766 * @param futures the CompletableFutures
767 * @param <T> value type of CompletableFuture
768 * @return a new CompletableFuture that is completed when all of the given CompletableFutures complete
769 */
770 public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures) {
771 return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]))
772 .thenApply(v -> futures.stream()
773 .map(CompletableFuture::join)
774 .collect(Collectors.toList())
775 );
776 }
777
778 /**
Madan Jampani307a21e2016-09-01 15:49:47 -0700779 * Returns a new CompletableFuture completed by reducing a list of computed values
780 * when all of the given CompletableFuture complete.
781 *
782 * @param futures the CompletableFutures
783 * @param reducer reducer for computing the result
784 * @param emptyValue zero value to be returned if the input future list is empty
785 * @param <T> value type of CompletableFuture
786 * @return a new CompletableFuture that is completed when all of the given CompletableFutures complete
787 */
788 public static <T> CompletableFuture<T> allOf(List<CompletableFuture<T>> futures,
789 BinaryOperator<T> reducer,
790 T emptyValue) {
791 return Tools.allOf(futures)
792 .thenApply(resultList -> resultList.stream().reduce(reducer).orElse(emptyValue));
793 }
794
795 /**
Jordan Halterman281dbf32018-06-15 17:46:28 -0700796 * Returns a new CompletableFuture completed with the first result from a list of futures. If no future
797 * is completed successfully, the returned future will be completed with the first exception.
798 *
799 * @param futures the input futures
800 * @param <T> future result type
801 * @return a new CompletableFuture
802 */
803 public static <T> CompletableFuture<T> firstOf(List<CompletableFuture<T>> futures) {
804 CompletableFuture<T> resultFuture = new CompletableFuture<>();
805 CompletableFuture.allOf(futures.stream()
806 .map(future -> future.thenAccept(r -> resultFuture.complete(r)))
807 .toArray(CompletableFuture[]::new))
808 .whenComplete((r, e) -> {
809 if (!resultFuture.isDone()) {
810 if (e != null) {
811 resultFuture.completeExceptionally(e);
812 } else {
813 resultFuture.complete(null);
814 }
815 }
816 });
817 return resultFuture;
818 }
819
820 /**
Madan Jampani307a21e2016-09-01 15:49:47 -0700821 * Returns a new CompletableFuture completed by with the first positive result from a list of
822 * input CompletableFutures.
823 *
824 * @param futures the input list of CompletableFutures
825 * @param positiveResultMatcher matcher to identify a positive result
826 * @param negativeResult value to complete with if none of the futures complete with a positive result
827 * @param <T> value type of CompletableFuture
828 * @return a new CompletableFuture
829 */
830 public static <T> CompletableFuture<T> firstOf(List<CompletableFuture<T>> futures,
831 Match<T> positiveResultMatcher,
832 T negativeResult) {
833 CompletableFuture<T> responseFuture = new CompletableFuture<>();
834 Tools.allOf(Lists.transform(futures, future -> future.thenAccept(r -> {
835 if (positiveResultMatcher.matches(r)) {
836 responseFuture.complete(r);
837 }
838 }))).whenComplete((r, e) -> {
839 if (!responseFuture.isDone()) {
840 if (e != null) {
841 responseFuture.completeExceptionally(e);
842 } else {
843 responseFuture.complete(negativeResult);
844 }
845 }
846 });
847 return responseFuture;
848 }
849
850 /**
Madan Jampani27b69c62015-05-15 15:49:02 -0700851 * Returns the contents of {@code ByteBuffer} as byte array.
852 * <p>
853 * WARNING: There is a performance cost due to array copy
854 * when using this method.
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700855 *
Madan Jampani27b69c62015-05-15 15:49:02 -0700856 * @param buffer byte buffer
857 * @return byte array containing the byte buffer contents
858 */
859 public static byte[] byteBuffertoArray(ByteBuffer buffer) {
860 int length = buffer.remaining();
861 if (buffer.hasArray()) {
862 int offset = buffer.arrayOffset() + buffer.position();
863 return Arrays.copyOfRange(buffer.array(), offset, offset + length);
864 }
865 byte[] bytes = new byte[length];
866 buffer.duplicate().get(bytes);
867 return bytes;
868 }
869
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -0700870 /**
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700871 * Converts an iterable to a stream.
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -0700872 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700873 * @param it iterable to convert
874 * @param <T> type if item
875 * @return iterable as a stream
HIGUCHI Yutabfc8b7a2015-07-01 23:47:43 -0700876 */
877 public static <T> Stream<T> stream(Iterable<T> it) {
878 return StreamSupport.stream(it.spliterator(), false);
879 }
880
Sho SHIMIZUb5638b82016-02-11 14:55:05 -0800881 /**
882 * Converts an optional to a stream.
883 *
884 * @param optional optional to convert
885 * @param <T> type of enclosed value
886 * @return optional as a stream
887 */
Sho SHIMIZU6ac20982016-05-04 09:50:54 -0700888 public static <T> Stream<T> stream(Optional<? extends T> optional) {
HIGUCHI Yuta0bc256f2016-05-06 15:28:26 -0700889 return optional.map(x -> Stream.<T>of(x)).orElse(Stream.empty());
Sho SHIMIZUb5638b82016-02-11 14:55:05 -0800890 }
891
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800892 // Auxiliary path visitor for recursive directory structure copying.
893 private static class DirectoryCopier extends SimpleFileVisitor<Path> {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800894 private Path src;
895 private Path dst;
896 private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING;
897
898 DirectoryCopier(String src, String dst) {
899 this.src = Paths.get(src);
900 this.dst = Paths.get(dst);
901 }
902
903 @Override
904 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
905 Path targetPath = dst.resolve(src.relativize(dir));
906 if (!Files.exists(targetPath)) {
907 Files.createDirectory(targetPath);
908 }
909 return FileVisitResult.CONTINUE;
910 }
911
912 @Override
913 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
914 Files.copy(file, dst.resolve(src.relativize(file)), copyOption);
915 return FileVisitResult.CONTINUE;
916 }
917 }
918
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700919 /**
920 * Creates OffsetDateTime instance from epoch milliseconds,
921 * using system default time zone.
922 *
923 * @param epochMillis to convert
924 * @return OffsetDateTime
925 */
926 public static OffsetDateTime defaultOffsetDataTime(long epochMillis) {
927 return OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMillis),
928 ZoneId.systemDefault());
929 }
930
Yuta HIGUCHI47d96092017-11-17 14:05:26 -0800931 /**
932 * Returns smaller of the two Comparable values.
933 *
934 * @param l an argument
935 * @param r another argument
936 * @return the smaller of {@code l} or {@code r}
937 * @param <C> Comparable type
938 * @throws NullPointerException if any of the arguments were null.
939 */
940 public static <C extends Comparable<? super C>> C min(C l, C r) {
941 checkNotNull(l, "l cannot be null");
942 checkNotNull(r, "r cannot be null");
943 return l.compareTo(r) <= 0 ? l : r;
944 }
945
946 /**
947 * Returns larger of the two Comparable values.
948 *
949 * @param l an argument
950 * @param r another argument
951 * @return the larger of {@code l} or {@code r}
952 * @param <C> Comparable type
953 * @throws NullPointerException if any of the arguments were null.
954 */
955 public static <C extends Comparable<? super C>> C max(C l, C r) {
956 checkNotNull(l, "l cannot be null");
957 checkNotNull(r, "r cannot be null");
958 return l.compareTo(r) >= 0 ? l : r;
959 }
Charles Chan58a33da2018-04-04 17:26:46 -0700960
961 /**
962 * Log level for the customized logger.
963 */
964 public enum LogLevel {
965 TRACE, DEBUG, INFO, WARN, ERROR
966 }
967
968 /**
969 * Wrapper function that enables logger invocation with log level as a parameter.
970 *
971 * @param logger logger
972 * @param level log level
973 * @param format format string
974 * @param args objects
975 */
976 public static void log(Logger logger, LogLevel level, String format, Object... args) {
977 switch (level) {
978 case TRACE:
979 logger.trace(format, args);
980 break;
981 case DEBUG:
982 logger.debug(format, args);
983 break;
984 case INFO:
985 logger.info(format, args);
986 break;
987 case WARN:
988 logger.warn(format, args);
989 break;
990 case ERROR:
991 logger.error(format, args);
992 break;
993 default:
994 log.error("Unknown log level {}", level);
995 break;
996 }
997 }
tom5f38b3a2014-08-27 23:50:54 -0700998}