Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 18 | import static java.nio.file.Files.delete; |
| 19 | import static java.nio.file.Files.walkFileTree; |
| 20 | import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory; |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 22 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 23 | import java.io.BufferedReader; |
| 24 | import java.io.File; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 25 | import java.io.FileInputStream; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 27 | import java.io.InputStreamReader; |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 28 | import java.nio.ByteBuffer; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 29 | import java.nio.charset.StandardCharsets; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 30 | import java.nio.file.FileVisitResult; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 31 | import java.nio.file.Files; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 32 | import java.nio.file.Path; |
| 33 | import java.nio.file.Paths; |
| 34 | import java.nio.file.SimpleFileVisitor; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 35 | import java.nio.file.StandardCopyOption; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 36 | import java.nio.file.attribute.BasicFileAttributes; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 38 | import java.util.Arrays; |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 39 | import java.util.Collection; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 40 | import java.util.Dictionary; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 41 | import java.util.List; |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 42 | import java.util.Random; |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 43 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 44 | import java.util.concurrent.ExecutionException; |
| 45 | import java.util.concurrent.Future; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 46 | import java.util.concurrent.ThreadFactory; |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
| 48 | import java.util.concurrent.TimeoutException; |
HIGUCHI Yuta | bfc8b7a | 2015-07-01 23:47:43 -0700 | [diff] [blame] | 49 | import java.util.stream.Stream; |
| 50 | import java.util.stream.StreamSupport; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 51 | |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
| 54 | import com.google.common.base.Strings; |
| 55 | import com.google.common.primitives.UnsignedLongs; |
| 56 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 57 | |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Miscellaneous utility methods. |
| 60 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 61 | public abstract class Tools { |
| 62 | |
| 63 | private Tools() { |
| 64 | } |
| 65 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | private static final Logger log = getLogger(Tools.class); |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 67 | |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 68 | private static Random random = new Random(); |
| 69 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Returns a thread factory that produces threads named according to the |
| 72 | * supplied name pattern. |
| 73 | * |
| 74 | * @param pattern name pattern |
| 75 | * @return thread factory |
| 76 | */ |
| 77 | public static ThreadFactory namedThreads(String pattern) { |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 78 | return new ThreadFactoryBuilder() |
| 79 | .setNameFormat(pattern) |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 80 | .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) |
| 81 | .build(); |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 82 | } |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 83 | |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Returns a thread factory that produces threads named according to the |
| 86 | * supplied name pattern and from the specified thread-group. The thread |
| 87 | * group name is expected to be specified in slash-delimited format, e.g. |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 88 | * {@code onos/intent}. The thread names will be produced by converting |
| 89 | * the thread group name into dash-delimited format and pre-pended to the |
| 90 | * specified pattern. |
Thomas Vachuska | 9c17a6d | 2015-02-17 23:36:43 -0800 | [diff] [blame] | 91 | * |
| 92 | * @param groupName group name in slash-delimited format to indicate hierarchy |
| 93 | * @param pattern name pattern |
| 94 | * @return thread factory |
| 95 | */ |
| 96 | public static ThreadFactory groupedThreads(String groupName, String pattern) { |
| 97 | return new ThreadFactoryBuilder() |
| 98 | .setThreadFactory(groupedThreadFactory(groupName)) |
Thomas Vachuska | c13b90a | 2015-02-18 18:19:55 -0800 | [diff] [blame] | 99 | .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern) |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 100 | .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e)) |
| 101 | .build(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 102 | } |
| 103 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 104 | /** |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 105 | * Returns a thread factory that produces threads with MIN_PRIORITY. |
| 106 | * |
| 107 | * @param factory backing ThreadFactory |
| 108 | * @return thread factory |
| 109 | */ |
| 110 | public static ThreadFactory minPriority(ThreadFactory factory) { |
| 111 | return new ThreadFactoryBuilder() |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 112 | .setThreadFactory(factory) |
| 113 | .setPriority(Thread.MIN_PRIORITY) |
| 114 | .build(); |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Brian O'Connor | e2eac10 | 2015-02-12 18:30:22 -0800 | [diff] [blame] | 118 | * Returns true if the collection is null or is empty. |
| 119 | * |
| 120 | * @param collection collection to test |
| 121 | * @return true if null or empty; false otherwise |
| 122 | */ |
| 123 | public static boolean isNullOrEmpty(Collection collection) { |
| 124 | return collection == null || collection.isEmpty(); |
| 125 | } |
| 126 | |
| 127 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 128 | * Returns the specified item if that item is not null; otherwise throws |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 129 | * not found exception. |
| 130 | * |
| 131 | * @param item item to check |
| 132 | * @param message not found message |
| 133 | * @param <T> item type |
| 134 | * @return item if not null |
| 135 | * @throws org.onlab.util.ItemNotFoundException if item is null |
| 136 | */ |
| 137 | public static <T> T nullIsNotFound(T item, String message) { |
| 138 | if (item == null) { |
| 139 | throw new ItemNotFoundException(message); |
| 140 | } |
| 141 | return item; |
| 142 | } |
| 143 | |
| 144 | /** |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 145 | * Returns the specified item if that item is not null; otherwise throws |
| 146 | * bad argument exception. |
| 147 | * |
| 148 | * @param item item to check |
| 149 | * @param message not found message |
| 150 | * @param <T> item type |
| 151 | * @return item if not null |
| 152 | * @throws IllegalArgumentException if item is null |
| 153 | */ |
| 154 | public static <T> T nullIsIllegal(T item, String message) { |
| 155 | if (item == null) { |
| 156 | throw new IllegalArgumentException(message); |
| 157 | } |
| 158 | return item; |
| 159 | } |
| 160 | |
| 161 | /** |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 162 | * Converts a string from hex to long. |
| 163 | * |
| 164 | * @param string hex number in string form; sans 0x |
| 165 | * @return long value |
| 166 | */ |
| 167 | public static long fromHex(String string) { |
| 168 | return UnsignedLongs.parseUnsignedLong(string, 16); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 173 | * |
| 174 | * @param value long value |
| 175 | * @return hex string |
| 176 | */ |
| 177 | public static String toHex(long value) { |
| 178 | return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0'); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 183 | * |
| 184 | * @param value long value |
| 185 | * @param width string width; zero padded |
| 186 | * @return hex string |
| 187 | */ |
| 188 | public static String toHex(long value, int width) { |
| 189 | return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0'); |
| 190 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 193 | * Get property as a string value. |
| 194 | * |
| 195 | * @param properties properties to be looked up |
| 196 | * @param propertyName the name of the property to look up |
| 197 | * @return value when the propertyName is defined or return null |
| 198 | */ |
| 199 | public static String get(Dictionary<?, ?> properties, String propertyName) { |
| 200 | Object v = properties.get(propertyName); |
| 201 | String s = (v instanceof String) ? (String) v : |
| 202 | v != null ? v.toString() : null; |
| 203 | return Strings.isNullOrEmpty(s) ? null : s.trim(); |
| 204 | } |
| 205 | |
| 206 | /** |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 207 | * Suspends the current thread for a specified number of millis. |
| 208 | * |
| 209 | * @param ms number of millis |
| 210 | */ |
| 211 | public static void delay(int ms) { |
| 212 | try { |
| 213 | Thread.sleep(ms); |
| 214 | } catch (InterruptedException e) { |
| 215 | throw new RuntimeException("Interrupted", e); |
| 216 | } |
| 217 | } |
| 218 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 219 | /** |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 220 | * Suspends the current thread for a random number of millis between 0 and |
| 221 | * the indicated limit. |
| 222 | * |
| 223 | * @param ms max number of millis |
| 224 | */ |
| 225 | public static void randomDelay(int ms) { |
| 226 | try { |
| 227 | Thread.sleep(random.nextInt(ms)); |
| 228 | } catch (InterruptedException e) { |
| 229 | throw new RuntimeException("Interrupted", e); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 234 | * Suspends the current thread for a specified number of millis and nanos. |
| 235 | * |
| 236 | * @param ms number of millis |
| 237 | * @param nanos number of nanos |
| 238 | */ |
| 239 | public static void delay(int ms, int nanos) { |
| 240 | try { |
| 241 | Thread.sleep(ms, nanos); |
| 242 | } catch (InterruptedException e) { |
| 243 | throw new RuntimeException("Interrupted", e); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 248 | * Slurps the contents of a file into a list of strings, one per line. |
| 249 | * |
| 250 | * @param path file path |
| 251 | * @return file contents |
| 252 | */ |
| 253 | public static List<String> slurp(File path) { |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 254 | try { |
| 255 | BufferedReader br = new BufferedReader( |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 256 | new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8)); |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 257 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 258 | List<String> lines = new ArrayList<>(); |
| 259 | String line; |
| 260 | while ((line = br.readLine()) != null) { |
| 261 | lines.add(line); |
| 262 | } |
| 263 | return lines; |
| 264 | |
| 265 | } catch (IOException e) { |
| 266 | return null; |
| 267 | } |
| 268 | } |
| 269 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 270 | /** |
| 271 | * Purges the specified directory path. Use with great caution since |
| 272 | * no attempt is made to check for symbolic links, which could result in |
| 273 | * deletion of unintended files. |
| 274 | * |
| 275 | * @param path directory to be removed |
| 276 | * @throws java.io.IOException if unable to remove contents |
| 277 | */ |
| 278 | public static void removeDirectory(String path) throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 279 | DirectoryDeleter visitor = new DirectoryDeleter(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 280 | File dir = new File(path); |
| 281 | if (dir.exists() && dir.isDirectory()) { |
| 282 | walkFileTree(Paths.get(path), visitor); |
| 283 | if (visitor.exception != null) { |
| 284 | throw visitor.exception; |
| 285 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 286 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Purges the specified directory path. Use with great caution since |
| 291 | * no attempt is made to check for symbolic links, which could result in |
| 292 | * deletion of unintended files. |
| 293 | * |
| 294 | * @param dir directory to be removed |
| 295 | * @throws java.io.IOException if unable to remove contents |
| 296 | */ |
| 297 | public static void removeDirectory(File dir) throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 298 | DirectoryDeleter visitor = new DirectoryDeleter(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 299 | if (dir.exists() && dir.isDirectory()) { |
| 300 | walkFileTree(Paths.get(dir.getAbsolutePath()), visitor); |
| 301 | if (visitor.exception != null) { |
| 302 | throw visitor.exception; |
| 303 | } |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 304 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 307 | // Auxiliary path visitor for recursive directory structure removal. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 308 | private static class DirectoryDeleter extends SimpleFileVisitor<Path> { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 309 | |
| 310 | private IOException exception; |
| 311 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 312 | @Override |
| 313 | public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) |
| 314 | throws IOException { |
| 315 | if (attributes.isRegularFile()) { |
| 316 | delete(file); |
| 317 | } |
| 318 | return FileVisitResult.CONTINUE; |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public FileVisitResult postVisitDirectory(Path directory, IOException ioe) |
| 323 | throws IOException { |
| 324 | delete(directory); |
| 325 | return FileVisitResult.CONTINUE; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public FileVisitResult visitFileFailed(Path file, IOException ioe) |
| 330 | throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 331 | this.exception = ioe; |
| 332 | return FileVisitResult.TERMINATE; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 336 | /** |
| 337 | * Returns a human friendly time ago string for a specified system time. |
Thomas Vachuska | 480adad | 2015-03-06 10:27:09 -0800 | [diff] [blame] | 338 | * |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 339 | * @param unixTime system time in millis |
| 340 | * @return human friendly time ago |
| 341 | */ |
| 342 | public static String timeAgo(long unixTime) { |
| 343 | long deltaMillis = System.currentTimeMillis() - unixTime; |
| 344 | long secondsSince = (long) (deltaMillis / 1000.0); |
| 345 | long minsSince = (long) (deltaMillis / (1000.0 * 60)); |
| 346 | long hoursSince = (long) (deltaMillis / (1000.0 * 60 * 60)); |
| 347 | long daysSince = (long) (deltaMillis / (1000.0 * 60 * 60 * 24)); |
| 348 | if (daysSince > 0) { |
| 349 | return String.format("%dd ago", daysSince); |
| 350 | } else if (hoursSince > 0) { |
| 351 | return String.format("%dh ago", hoursSince); |
| 352 | } else if (minsSince > 0) { |
| 353 | return String.format("%dm ago", minsSince); |
| 354 | } else if (secondsSince > 0) { |
| 355 | return String.format("%ds ago", secondsSince); |
| 356 | } else { |
| 357 | return "just now"; |
| 358 | } |
| 359 | } |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Copies the specified directory path. Use with great caution since |
| 363 | * no attempt is made to check for symbolic links, which could result in |
| 364 | * copy of unintended files. |
| 365 | * |
| 366 | * @param src directory to be copied |
| 367 | * @param dst destination directory to be removed |
| 368 | * @throws java.io.IOException if unable to remove contents |
| 369 | */ |
| 370 | public static void copyDirectory(String src, String dst) throws IOException { |
| 371 | walkFileTree(Paths.get(src), new DirectoryCopier(src, dst)); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Copies the specified directory path. Use with great caution since |
| 376 | * no attempt is made to check for symbolic links, which could result in |
| 377 | * copy of unintended files. |
| 378 | * |
| 379 | * @param src directory to be copied |
| 380 | * @param dst destination directory to be removed |
| 381 | * @throws java.io.IOException if unable to remove contents |
| 382 | */ |
| 383 | public static void copyDirectory(File src, File dst) throws IOException { |
| 384 | walkFileTree(Paths.get(src.getAbsolutePath()), |
| 385 | new DirectoryCopier(src.getAbsolutePath(), |
| 386 | dst.getAbsolutePath())); |
| 387 | } |
| 388 | |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Returns the future value when complete or if future |
| 391 | * completes exceptionally returns the defaultValue. |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 392 | * |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 393 | * @param future future |
| 394 | * @param defaultValue default value |
| 395 | * @param <T> future value type |
| 396 | * @return future value when complete or if future |
| 397 | * completes exceptionally returns the defaultValue. |
| 398 | */ |
| 399 | public static <T> T futureGetOrElse(Future<T> future, T defaultValue) { |
| 400 | try { |
| 401 | return future.get(); |
| 402 | } catch (InterruptedException e) { |
| 403 | Thread.currentThread().interrupt(); |
| 404 | return defaultValue; |
| 405 | } catch (ExecutionException e) { |
| 406 | return defaultValue; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Returns the future value when complete or if future |
| 412 | * completes exceptionally returns the defaultValue. |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 413 | * |
Madan Jampani | 2bfa94c | 2015-04-11 05:03:49 -0700 | [diff] [blame] | 414 | * @param future future |
| 415 | * @param timeout time to wait for successful completion |
| 416 | * @param timeUnit time unit |
| 417 | * @param defaultValue default value |
| 418 | * @param <T> future value type |
| 419 | * @return future value when complete or if future |
| 420 | * completes exceptionally returns the defaultValue. |
| 421 | */ |
| 422 | public static <T> T futureGetOrElse(Future<T> future, |
| 423 | long timeout, |
| 424 | TimeUnit timeUnit, |
| 425 | T defaultValue) { |
| 426 | try { |
| 427 | return future.get(timeout, timeUnit); |
| 428 | } catch (InterruptedException e) { |
| 429 | Thread.currentThread().interrupt(); |
| 430 | return defaultValue; |
| 431 | } catch (ExecutionException | TimeoutException e) { |
| 432 | return defaultValue; |
| 433 | } |
| 434 | } |
| 435 | |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Returns a future that is completed exceptionally. |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 438 | * |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 439 | * @param t exception |
| 440 | * @param <T> future value type |
| 441 | * @return future |
| 442 | */ |
| 443 | public static <T> CompletableFuture<T> exceptionalFuture(Throwable t) { |
| 444 | CompletableFuture<T> future = new CompletableFuture<>(); |
| 445 | future.completeExceptionally(t); |
| 446 | return future; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Returns the contents of {@code ByteBuffer} as byte array. |
| 451 | * <p> |
| 452 | * WARNING: There is a performance cost due to array copy |
| 453 | * when using this method. |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 454 | * |
Madan Jampani | 27b69c6 | 2015-05-15 15:49:02 -0700 | [diff] [blame] | 455 | * @param buffer byte buffer |
| 456 | * @return byte array containing the byte buffer contents |
| 457 | */ |
| 458 | public static byte[] byteBuffertoArray(ByteBuffer buffer) { |
| 459 | int length = buffer.remaining(); |
| 460 | if (buffer.hasArray()) { |
| 461 | int offset = buffer.arrayOffset() + buffer.position(); |
| 462 | return Arrays.copyOfRange(buffer.array(), offset, offset + length); |
| 463 | } |
| 464 | byte[] bytes = new byte[length]; |
| 465 | buffer.duplicate().get(bytes); |
| 466 | return bytes; |
| 467 | } |
| 468 | |
HIGUCHI Yuta | bfc8b7a | 2015-07-01 23:47:43 -0700 | [diff] [blame] | 469 | /** |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 470 | * Converts an iterable to a stream. |
HIGUCHI Yuta | bfc8b7a | 2015-07-01 23:47:43 -0700 | [diff] [blame] | 471 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 472 | * @param it iterable to convert |
| 473 | * @param <T> type if item |
| 474 | * @return iterable as a stream |
HIGUCHI Yuta | bfc8b7a | 2015-07-01 23:47:43 -0700 | [diff] [blame] | 475 | */ |
| 476 | public static <T> Stream<T> stream(Iterable<T> it) { |
| 477 | return StreamSupport.stream(it.spliterator(), false); |
| 478 | } |
| 479 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 480 | // Auxiliary path visitor for recursive directory structure copying. |
| 481 | private static class DirectoryCopier extends SimpleFileVisitor<Path> { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 482 | private Path src; |
| 483 | private Path dst; |
| 484 | private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING; |
| 485 | |
| 486 | DirectoryCopier(String src, String dst) { |
| 487 | this.src = Paths.get(src); |
| 488 | this.dst = Paths.get(dst); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 493 | Path targetPath = dst.resolve(src.relativize(dir)); |
| 494 | if (!Files.exists(targetPath)) { |
| 495 | Files.createDirectory(targetPath); |
| 496 | } |
| 497 | return FileVisitResult.CONTINUE; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 502 | Files.copy(file, dst.resolve(src.relativize(file)), copyOption); |
| 503 | return FileVisitResult.CONTINUE; |
| 504 | } |
| 505 | } |
| 506 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 507 | } |