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