View Javadoc
1   /*
2    * Copyright 2016-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import com.sun.jna.Memory;
11  import com.sun.jna.Pointer;
12  
13  import oshi.annotation.concurrent.ThreadSafe;
14  
15  /**
16   * General utility methods
17   */
18  @ThreadSafe
19  public final class Util {
20      private static final Logger LOG = LoggerFactory.getLogger(Util.class);
21  
22      private Util() {
23      }
24  
25      /**
26       * Sleeps for the specified number of milliseconds.
27       *
28       * @param ms How long to sleep
29       */
30      public static void sleep(long ms) {
31          try {
32              LOG.trace("Sleeping for {} ms", ms);
33              Thread.sleep(ms);
34          } catch (InterruptedException e) { // NOSONAR squid:S2142
35              LOG.warn("Interrupted while sleeping for {} ms: {}", ms, e.getMessage());
36              Thread.currentThread().interrupt();
37          }
38      }
39  
40      /**
41       * Tests if a String matches another String with a wildcard pattern.
42       *
43       * @param text    The String to test
44       * @param pattern The String containing a wildcard pattern where ? represents a single character and * represents
45       *                any number of characters. If the first character of the pattern is a carat (^) the test is
46       *                performed against the remaining characters and the result of the test is the opposite.
47       * @return True if the String matches or if the first character is ^ and the remainder of the String does not match.
48       */
49      public static boolean wildcardMatch(String text, String pattern) {
50          if (pattern.length() > 0 && pattern.charAt(0) == '^') {
51              return !wildcardMatch(text, pattern.substring(1));
52          }
53          return text.matches(pattern.replace("?", ".?").replace("*", ".*?"));
54      }
55  
56      /**
57       * Tests if a String is either null or empty.
58       *
59       * @param s The string to test
60       * @return True if the String is either null or empty.
61       */
62      public static boolean isBlank(String s) {
63          return s == null || s.isEmpty();
64      }
65  
66      /**
67       * Tests if a String is either null or empty or the unknown constant.
68       *
69       * @param s The string to test
70       * @return True if the String is either null or empty or the unknown constant.
71       */
72      public static boolean isBlankOrUnknown(String s) {
73          return isBlank(s) || Constants.UNKNOWN.equals(s);
74      }
75  
76      /**
77       * If the given Pointer is of class Memory, executes the close method on it to free its native allocation
78       *
79       * @param p A pointer
80       */
81      public static void freeMemory(Pointer p) {
82          if (p instanceof Memory) {
83              ((Memory) p).close();
84          }
85      }
86  
87      /**
88       * Tests if session of a user logged in a device is valid or not.
89       *
90       * @param user      The user logged in
91       * @param device    The device used by user
92       * @param loginTime The login time of the user
93       * @return True if Session is valid or False if the user of device is empty or the login time is lesser than zero or
94       *         greater than current time.
95       */
96      public static boolean isSessionValid(String user, String device, Long loginTime) {
97          return !(user.isEmpty() || device.isEmpty() || loginTime < 0 || loginTime > System.currentTimeMillis());
98      }
99  }