트리 구조 도구 - TreeUtil
트리 생성 예시
public class TreeExample {
public static void main(String[] args) {
List<TreeNode<String>> nodes = Arrays.asList(
new TreeNode<>("A1", "root", "시스템 설정", 10),
new TreeNode<>("A11", "A1", "사용자 관리", 100),
new TreeNode<>("A111", "A11", "사용자 추가", 0),
new TreeNode<>("B1", "root", "상점 관리", 20),
new TreeNode<>("B11", "B1", "상품 관리", 50)
);
TreeNodeConfig config = new TreeNodeConfig();
config.setWeightKey("priority");
config.setDepthLimit(4);
List<Tree<String>> result = TreeUtil.build(nodes, "root", (src, dest) -> {
dest.setId(src.getId());
dest.setParentId(src.getParentId());
dest.putAll(BeanUtil.beanToMap(src));
});
}
}
실행 결과
[
{
"id": "B1",
"parentId": "root",
"name": "상점 관리",
"priority": 20,
"children": [
{"id":"B11","parentId":"B1","name":"상품 관리","priority":50}
]
},
{
"id": "A1",
"parentId": "root",
"name": "시스템 설정",
"priority": 10,
"children": [
{
"id": "A11",
"parentId": "A1",
"name": "사용자 관리",
"priority": 100,
"children": [
{"id":"A111","parentId":"A11","name":"사용자 추가","priority":0}
]
}
]
}
]
빈 처리 도구 - BeanUtil
객체 변환
public class BeanConversion {
public static void main(String[] args) {
UserEntity user = new UserEntity();
user.setUserId(1001);
user.setUsername("홍길동");
Map<String, Object> map = BeanUtil.toMap(user);
System.out.println(map.get("username")); // 홍길동
}
}
캡차 생성 도구 - CaptchaUtil
캡차 이미지 생성
public class CaptchaGenerator {
public static void main(String[] args) {
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(300, 150);
System.out.println("생성된 캡차: " + captcha.getCode());
System.out.println("검증 결과: " + captcha.verify(captcha.getCode()));
}
}
데이터 변환 도구 - Convert
타입 변환
public class TypeConverter {
public static void main(String[] args) {
Object result = Convert.convert(Integer.class, "500");
System.out.println(result); // 500
}
}
객체 비교 도구 - ObjectUtil
동등성 검증
public class ObjectComparator {
public static void main(String[] args) {
String a = null;
String b = null;
System.out.println("동등 여부: " + ObjectUtil.equals(a, b)); // true
}
}
숫자 처리 도구 - NumberUtil
정밀 덧셈 연산
public class PrecisionCalculator {
public static void main(String[] args) {
double sum = NumberUtil.add(1.001, 2.002).doubleValue();
System.out.println(sum); // 3.003
}
}
날짜 도구 - DateUtil
현재 날짜 획득
public class CurrentDate {
public static void main(String[] args) {
String today = DateUtil.today();
System.out.println(today); // yyyy-MM-dd 형식
}
}
컬렉션 도구 - CollUtil
컬렉션 요소 확인
public class CollectionCheck {
public static void main(String[] args) {
List<String> data = Arrays.asList("apple", "banana", "cherry");
boolean exists = CollUtil.contains(data, item -> item.startsWith("a"));
System.out.println(exists); // true
}
}
컬렉션 스트림 - CollStreamUtil
맵 변환
public class CollectionToMap {
public static void main(String[] args) {
List<Product> products = Arrays.asList(
new Product(1, "Notebook"),
new Product(2, "Smartphone")
);
Map<Long, String> map = CollStreamUtil.toMap(
products,
Product::getId,
Product::getName
);
System.out.println(map.get(1L)); // Notebook
}
}
JSON 처리 도구 - JSONUtil
JSON 파싱
public class JsonParser {
public static void main(String[] args) {
String json = "{\"name\":\"Kim\",\"age\":30}";
JSONObject obj = JSONUtil.parseObj(json);
System.out.println(obj.getStr("name")); // Kim
}
}
고유 ID 생성 도구 - IdUtil
UUID 생성
public class UuidGenerator {
public static void main(String[] args) {
String uuid = IdUtil.randomUUID();
System.out.println(uuid);
}
}
열거형 도구 - EnumUtil
열거형 이름 목록
public class EnumNames {
enum Color { RED, GREEN, BLUE }
public static void main(String[] args) {
List<String> names = EnumUtil.getNames(Color.class);
System.out.println(names); // [RED, GREEN, BLUE]
}
}
HTTP 클라이언트
public class HttpService {
public static void main(String[] args) {
HttpResponse response = HttpRequest.post("https://api.example.com/data")
.header("Content-Type", "application/json")
.body("{ \"key\":\"value\" }")
.timeout(3000)
.execute();
if(response.getStatus() == HttpStatus.HTTP_OK) {
System.out.println(response.body());
}
}
}