or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

additional-utilities.mdbean-object-manipulation.mdcollection-utilities.mdcore-string-operations.mdcryptographic-operations.mddatabase-access.mddate-time-handling.mdfile-io-operations.mdhttp-client-operations.mdindex.mdjson-processing.md

additional-utilities.mddocs/

0

# Additional Utilities

1

2

Specialized utilities for validation, random generation, reflection, system operations, and various other common programming tasks.

3

4

## Import

5

6

```java

7

import cn.hutool.core.util.*;

8

import cn.hutool.core.lang.Validator;

9

import cn.hutool.system.SystemUtil;

10

import cn.hutool.extra.qr.QrCodeUtil;

11

import cn.hutool.captcha.CaptchaUtil;

12

```

13

14

## Validation Utilities

15

16

### Data Format Validation

17

18

```java { .api }

19

// Email validation

20

public static boolean isEmail(CharSequence email);

21

22

// Phone number validation

23

public static boolean isPhone(CharSequence mobile);

24

public static boolean isMobile(CharSequence mobile);

25

26

// ID card validation

27

public static boolean isCitizenId(CharSequence citizenId);

28

public static boolean isValidCard(CharSequence cardNumber);

29

30

// URL validation

31

public static boolean isUrl(CharSequence url);

32

public static boolean isIpv4(CharSequence ipv4);

33

public static boolean isIpv6(CharSequence ipv6);

34

```

35

36

### Number and String Validation

37

38

```java { .api }

39

// Number validation

40

public static boolean isNumber(CharSequence str);

41

public static boolean isInteger(CharSequence str);

42

public static boolean isDouble(CharSequence str);

43

44

// String format validation

45

public static boolean isLetter(CharSequence value);

46

public static boolean isLetterUpper(CharSequence value);

47

public static boolean isLetterLower(CharSequence value);

48

public static boolean isChinese(CharSequence str);

49

public static boolean isWord(CharSequence value);

50

```

51

52

**Usage Examples:**

53

54

```java

55

// Validate user input

56

String email = "user@example.com";

57

boolean validEmail = Validator.isEmail(email); // true

58

59

String phone = "13812345678";

60

boolean validPhone = Validator.isMobile(phone); // true

61

62

String idCard = "110101199003077890";

63

boolean validId = Validator.isCitizenId(idCard); // depends on checksum

64

65

// Validate numeric input

66

String numberStr = "123.45";

67

boolean isNum = Validator.isNumber(numberStr); // true

68

boolean isInt = Validator.isInteger(numberStr); // false

69

```

70

71

## Random Generation Utilities

72

73

### Random Data Generation

74

75

```java { .api }

76

// Random strings

77

public static String randomString(int length);

78

public static String randomString(String baseString, int length);

79

public static String randomStringUpper(int length);

80

public static String randomNumbers(int length);

81

82

// Random numbers

83

public static int randomInt();

84

public static int randomInt(int min, int max);

85

public static long randomLong();

86

public static long randomLong(long min, long max);

87

public static double randomDouble();

88

public static double randomDouble(double min, double max);

89

90

// Random selection

91

public static <T> T randomEle(T[] array);

92

public static <T> T randomEle(List<T> list);

93

public static <T> List<T> randomEles(List<T> list, int count);

94

```

95

96

### UUID and ID Generation

97

98

```java { .api }

99

// UUID generation

100

public static String randomUUID();

101

public static String simpleUUID();

102

public static String fastUUID();

103

public static String fastSimpleUUID();

104

105

// Snowflake ID (distributed unique ID)

106

public static long getSnowflakeNextId();

107

public static String getSnowflakeNextIdStr();

108

109

// Object ID (MongoDB style)

110

public static String objectId();

111

```

112

113

**Usage Examples:**

114

115

```java

116

// Generate random data

117

String sessionId = RandomUtil.randomString(32);

118

String captcha = RandomUtil.randomNumbers(6);

119

int randomPort = RandomUtil.randomInt(8000, 9000);

120

121

// Generate unique IDs

122

String uuid = IdUtil.randomUUID();

123

String simpleId = IdUtil.simpleUUID(); // No dashes

124

long snowflakeId = IdUtil.getSnowflakeNextId();

125

String objectId = IdUtil.objectId();

126

127

// Random selection

128

List<String> colors = Arrays.asList("red", "green", "blue", "yellow");

129

String randomColor = RandomUtil.randomEle(colors);

130

List<String> twoColors = RandomUtil.randomEles(colors, 2);

131

```

132

133

## Reflection Utilities

134

135

### Class Operations

136

137

```java { .api }

138

// Class loading and inspection

139

public static Class<?> loadClass(String name);

140

public static boolean isPresent(String className);

141

public static <T> T newInstance(Class<T> clazz, Object... params);

142

143

// Method operations

144

public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes);

145

public static Object invoke(Object obj, String methodName, Object... args);

146

public static Object invokeStatic(Class<?> clazz, String methodName, Object... args);

147

148

// Field operations

149

public static Field getField(Class<?> clazz, String fieldName);

150

public static Object getFieldValue(Object obj, String fieldName);

151

public static void setFieldValue(Object obj, String fieldName, Object value);

152

```

153

154

### Type Utilities

155

156

```java { .api }

157

// Type checking

158

public static boolean isBasicType(Class<?> clazz);

159

public static boolean isWrapClass(Class<?> clazz);

160

public static boolean isPrimitiveWrapper(Class<?> clazz);

161

162

// Generic type operations

163

public static Type getTypeArgument(Type type);

164

public static Type getTypeArgument(Type type, int index);

165

public static Class<?> getClass(Type type);

166

```

167

168

**Usage Examples:**

169

170

```java

171

// Dynamic object creation

172

Class<?> userClass = ReflectUtil.loadClass("com.example.User");

173

Object user = ReflectUtil.newInstance(userClass, "John", 30);

174

175

// Dynamic method invocation

176

Object result = ReflectUtil.invoke(user, "getName");

177

ReflectUtil.invoke(user, "setAge", 31);

178

179

// Dynamic field access

180

String name = (String) ReflectUtil.getFieldValue(user, "name");

181

ReflectUtil.setFieldValue(user, "active", true);

182

183

// Check if class exists

184

boolean hasClass = ReflectUtil.isPresent("com.mysql.cj.jdbc.Driver");

185

```

186

187

## System Information Utilities

188

189

### JVM and Runtime Information

190

191

```java { .api }

192

// JVM information

193

public static JavaInfo getJavaInfo();

194

public static JvmInfo getJvmInfo();

195

public static RuntimeInfo getRuntimeInfo();

196

197

// Host information

198

public static HostInfo getHostInfo();

199

public static OsInfo getOsInfo();

200

public static UserInfo getUserInfo();

201

202

// Memory information

203

public static long getTotalMemory();

204

public static long getFreeMemory();

205

public static long getUsedMemory();

206

public static long getMaxMemory();

207

```

208

209

### System Properties

210

211

```java { .api }

212

// Get system properties

213

public static String get(String key);

214

public static String get(String key, String defaultValue);

215

public static void set(String key, String value);

216

217

// Common properties

218

public static String getJavaHome();

219

public static String getUserHome();

220

public static String getUserDir();

221

public static String getTmpDir();

222

public static String getFileSeparator();

223

public static String getPathSeparator();

224

public static String getLineSeparator();

225

```

226

227

**Usage Examples:**

228

229

```java

230

// System information

231

JavaInfo javaInfo = SystemUtil.getJavaInfo();

232

System.out.println("Java Version: " + javaInfo.getVersion());

233

234

OsInfo osInfo = SystemUtil.getOsInfo();

235

System.out.println("OS: " + osInfo.getName() + " " + osInfo.getVersion());

236

237

// Memory monitoring

238

long usedMemory = SystemUtil.getUsedMemory();

239

long totalMemory = SystemUtil.getTotalMemory();

240

double memoryUsage = (double) usedMemory / totalMemory * 100;

241

System.out.println("Memory usage: " + memoryUsage + "%");

242

243

// System properties

244

String javaHome = SystemUtil.getJavaHome();

245

String userHome = SystemUtil.getUserHome();

246

String tmpDir = SystemUtil.getTmpDir();

247

```

248

249

## QR Code and Captcha Utilities

250

251

### QR Code Generation

252

253

```java { .api }

254

// Generate QR codes

255

public static BufferedImage generate(String content, int width, int height);

256

public static void generate(String content, int width, int height, File file);

257

public static String generateAsBase64(String content, int width, int height);

258

259

// Decode QR codes

260

public static String decode(File qrCodeFile);

261

public static String decode(BufferedImage qrCodeImage);

262

```

263

264

### Captcha Generation

265

266

```java { .api }

267

// Create different types of captchas

268

public static LineCaptcha createLineCaptcha(int width, int height);

269

public static CircleCaptcha createCircleCaptcha(int width, int height);

270

public static ShearCaptcha createShearCaptcha(int width, int height);

271

public static GifCaptcha createGifCaptcha(int width, int height);

272

```

273

274

**Usage Examples:**

275

276

```java

277

// Generate QR code

278

String url = "https://hutool.cn";

279

QrCodeUtil.generate(url, 200, 200, new File("qrcode.png"));

280

281

// Generate captcha

282

LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);

283

String code = captcha.getCode(); // Get the answer

284

captcha.write(new File("captcha.png")); // Save image

285

286

// Verify captcha

287

boolean verified = captcha.verify(userInputCode);

288

```

289

290

## Coordinate and Geographic Utilities

291

292

### Coordinate Conversions

293

294

```java { .api }

295

// Coordinate system conversions

296

public static double[] wgs84ToGcj02(double lng, double lat);

297

public static double[] gcj02ToWgs84(double lng, double lat);

298

public static double[] gcj02ToBd09(double lng, double lat);

299

public static double[] bd09ToGcj02(double lng, double lat);

300

301

// Distance calculations

302

public static double distance(double lng1, double lat1, double lng2, double lat2);

303

```

304

305

## Number and Math Utilities

306

307

### Number Operations

308

309

```java { .api }

310

// Number parsing and formatting

311

public static Number parseNumber(String numberStr);

312

public static int parseInt(String numberStr);

313

public static long parseLong(String numberStr);

314

public static double parseDouble(String numberStr);

315

316

// Number validation

317

public static boolean isNumber(String str);

318

public static boolean isInteger(String str);

319

public static boolean isDouble(String str);

320

321

// Number comparison

322

public static boolean equals(Number num1, Number num2);

323

public static int compare(Number num1, Number num2);

324

325

// Number formatting

326

public static String decimalFormat(String format, Object value);

327

public static String formatPercent(double number, int decimalPlaces);

328

```

329

330

### Mathematical Operations

331

332

```java { .api }

333

// Basic math operations

334

public static double add(Number... numbers);

335

public static double subtract(Number num1, Number num2);

336

public static double multiply(Number... numbers);

337

public static double divide(Number num1, Number num2);

338

339

// Rounding operations

340

public static double roundUp(double value, int scale);

341

public static double roundDown(double value, int scale);

342

public static double round(double value, int scale);

343

344

// Range operations

345

public static boolean isIn(Number value, Number min, Number max);

346

public static Number max(Number... numbers);

347

public static Number min(Number... numbers);

348

```

349

350

**Usage Examples:**

351

352

```java

353

// Safe number parsing

354

String userInput = "123.45";

355

double value = NumberUtil.parseDouble(userInput); // Handles null and invalid formats

356

357

// Precise calculations (avoids floating point errors)

358

double result = NumberUtil.add(0.1, 0.2); // Returns exactly 0.3

359

double percentage = NumberUtil.formatPercent(0.1234, 2); // "12.34%"

360

361

// Number validation and comparison

362

boolean isNum = NumberUtil.isNumber("123.45");

363

boolean equal = NumberUtil.equals(123.0, 123); // true

364

365

// Geographic calculations

366

// Convert GPS coordinates between different systems

367

double[] wgs84 = {116.3974, 39.9093}; // Beijing in WGS84

368

double[] gcj02 = CoordinateUtil.wgs84ToGcj02(wgs84[0], wgs84[1]);

369

370

// Calculate distance between two points

371

double distance = CoordinateUtil.distance(116.3974, 39.9093, 121.4737, 31.2304); // Beijing to Shanghai

372

```

373

374

All utility classes provide null-safe operations and handle edge cases gracefully. They follow consistent naming conventions and provide both basic and advanced functionality for common programming tasks.