| 142 | } |
| 143 | |
| 144 | @SuppressWarnings("unchecked") |
| 145 | public static <E> E randomChoice(Collection<? extends E> input) { |
| 146 | if (input == null || input.isEmpty()) { |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | int idx = new SecureRandom().nextInt(input.size()); |
| 151 | |
| 152 | if (input instanceof List) { |
| 153 | return ((List<E>) input).get(idx); |
| 154 | } |
| 155 | |
| 156 | // 如果不是 List,则转换为 ArrayList 后再获取 |
| 157 | return new ArrayList<>(input).get(idx); |
| 158 | } |
| 159 | |
| 160 | public static String exceptionToString(Throwable throwable) { |
| 161 | StringWriter sw = new StringWriter(); |