|
@@ -0,0 +1,202 @@
|
|
|
+package com.its.op;
|
|
|
+
|
|
|
+import lombok.*;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.test.context.ActiveProfiles;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.function.*;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+@ActiveProfiles(profiles = "dev")
|
|
|
+public class UtilTest {
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test1() {
|
|
|
+ ZonedDateTime a = ZonedDateTime.now();
|
|
|
+ System.out.println(a);
|
|
|
+
|
|
|
+ ZonedDateTime i = ZonedDateTime.now(ZoneId.of("UTC"));
|
|
|
+ System.out.println(i);
|
|
|
+
|
|
|
+ LocalDateTime b = LocalDateTime.now();
|
|
|
+ System.out.println(b);
|
|
|
+
|
|
|
+ ZonedDateTime c = LocalDateTime.now().atZone(ZoneId.of("UTC"));
|
|
|
+ System.out.println(c);
|
|
|
+
|
|
|
+ String d = "2023-02-15 15:12:14";
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ LocalDateTime e = LocalDateTime.parse(d, formatter);
|
|
|
+ ZonedDateTime f = LocalDateTime.parse(d, formatter).atZone(ZoneId.of("Asia/Seoul"));
|
|
|
+ System.out.println(e);
|
|
|
+ System.out.println(f);
|
|
|
+
|
|
|
+ ZonedDateTime g = f.withZoneSameInstant(ZoneId.of("UTC"));
|
|
|
+ System.out.println("g : " + g);
|
|
|
+
|
|
|
+ LocalDateTime h = c.toLocalDateTime();
|
|
|
+ System.out.println(h);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test2() {
|
|
|
+ double plusDoubleValue = 0;
|
|
|
+ for (int i = 0; i < 100; i++) {
|
|
|
+ plusDoubleValue += 0.1;
|
|
|
+ }
|
|
|
+ double multipleDoubleValue = 0.1 * 100;
|
|
|
+ System.out.println("double plusValue : " + plusDoubleValue);
|
|
|
+ System.out.println("double multipleValue : " + multipleDoubleValue);
|
|
|
+
|
|
|
+ float plusFloatValue = 0;
|
|
|
+ for (int i = 0; i < 100; i++) {
|
|
|
+ plusFloatValue += 0.1;
|
|
|
+ }
|
|
|
+ float multipleFloatValue = 0.1f * 100;
|
|
|
+ System.out.println("float plusValue : " + plusFloatValue);
|
|
|
+ System.out.println("float multipleValue : " + multipleFloatValue);
|
|
|
+
|
|
|
+ BigDecimal plusBigDecimal = new BigDecimal("0");
|
|
|
+ for (int i = 0; i < 100; i++) {
|
|
|
+ plusBigDecimal = plusBigDecimal.add(new BigDecimal("0.1"));
|
|
|
+ }
|
|
|
+ BigDecimal multipleBigDecimal = new BigDecimal("10.0");
|
|
|
+
|
|
|
+ System.out.println("BigDecimal plusBigDecimal : " + plusBigDecimal);
|
|
|
+ System.out.println("BigDecimal multipleBigDecimal : " + multipleBigDecimal);
|
|
|
+ }
|
|
|
+
|
|
|
+ @FunctionalInterface // 함수형 인터페이스 요건에 맞지 않으면 에러가 발생
|
|
|
+ public interface CustomFunctionalInterface {
|
|
|
+
|
|
|
+ void called(String message, String data);
|
|
|
+
|
|
|
+ default void printHello() {
|
|
|
+ System.out.println("Hello Around Hub!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test3() {
|
|
|
+ CustomFunctionalInterface customInterface = (message, data) -> System.out.println(message + " ===> " + data);
|
|
|
+
|
|
|
+ String message = "Hello! Around Hub Studio";
|
|
|
+ customInterface.called(message, "dddd");
|
|
|
+
|
|
|
+ customInterface.printHello();
|
|
|
+
|
|
|
+ Predicate<Integer> greaterThanTen = (num) -> num > 10;
|
|
|
+ Predicate<Integer> lessThanTwenty = (num) -> num < 20;
|
|
|
+
|
|
|
+ Predicate<Integer> betweenTenAndTwenty = greaterThanTen.and(lessThanTwenty);
|
|
|
+ boolean result = betweenTenAndTwenty.test(15);
|
|
|
+ System.out.println(result); // true
|
|
|
+
|
|
|
+ Predicate<Integer> equalsCount = (count) -> count.equals(50);
|
|
|
+ System.out.println(equalsCount.test(50));
|
|
|
+ System.out.println(equalsCount.test(20));
|
|
|
+
|
|
|
+ Supplier<String> getMessage = () -> "Around Hub Studio!";
|
|
|
+ System.out.println(getMessage.get());
|
|
|
+
|
|
|
+ // param, result
|
|
|
+ Function<String, Integer> function = (msg) -> msg.length();//"Message : " + msg;
|
|
|
+ System.out.println(function.apply("Around Hub Studio!"));
|
|
|
+
|
|
|
+ // param1, param2, result
|
|
|
+ BiFunction<String, Integer, String> biFunction = (msg, count) -> {
|
|
|
+ int resultCount = count + 10;
|
|
|
+ return "Message : " + msg + ", Count : " + resultCount;
|
|
|
+ };
|
|
|
+
|
|
|
+ System.out.println(biFunction.apply("Around Hub Studio!", 5));
|
|
|
+
|
|
|
+ Consumer<String> print = (msg) -> System.out.println("Message : " + msg);
|
|
|
+ print.accept("Around Hub Studio!----------");
|
|
|
+ }
|
|
|
+
|
|
|
+ @NoArgsConstructor
|
|
|
+ @AllArgsConstructor
|
|
|
+ @Getter
|
|
|
+ @Setter
|
|
|
+ @ToString
|
|
|
+ public class SampleObject {
|
|
|
+ private String name;
|
|
|
+ private String age;
|
|
|
+ private String organization;
|
|
|
+ }
|
|
|
+
|
|
|
+ private SampleObject getNullObject() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private SampleObject getObject() {
|
|
|
+ return new SampleObject("flature", "10", "Around Hub Studio");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test4() {
|
|
|
+ // filter
|
|
|
+ Optional<SampleObject> sampleObject1 = Optional.of(getObject())
|
|
|
+ .filter(object -> object.getName().equals("flature"));
|
|
|
+ System.out.println(sampleObject1.get());
|
|
|
+
|
|
|
+ Optional<SampleObject> sampleObject2 = Optional.of(getObject())
|
|
|
+ .filter(object -> object.getName().equals("flature123"));
|
|
|
+ System.out.println(sampleObject2.isPresent());
|
|
|
+
|
|
|
+ // map
|
|
|
+ Optional<String> StringObject1 = Optional.of(getObject())
|
|
|
+ .map(sampleObject -> sampleObject.getName());
|
|
|
+ System.out.println(StringObject1.get());
|
|
|
+
|
|
|
+ // ifPresent
|
|
|
+ Optional.of(getObject())
|
|
|
+ .ifPresent(object -> System.out.println(object));
|
|
|
+
|
|
|
+ // ifPresentOrElse
|
|
|
+// Optional.ofNullable(getNullObject())
|
|
|
+// .ifPresent(object -> {
|
|
|
+// System.out.println(object);
|
|
|
+// }, () -> {
|
|
|
+// System.out.println("null object");
|
|
|
+// }
|
|
|
+// );
|
|
|
+
|
|
|
+ // isPresent
|
|
|
+ if (Optional.ofNullable(getObject()).isPresent()) {
|
|
|
+ System.out.println("is present!");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // get
|
|
|
+ Optional<SampleObject> sampleObject3 = Optional.of(getObject());
|
|
|
+ System.out.println(sampleObject3.get());
|
|
|
+
|
|
|
+ // orElse : 값이 있어도 실행됨
|
|
|
+ SampleObject sampleObject4 = Optional.ofNullable(getNullObject())
|
|
|
+ .orElse(new SampleObject("flature123", "around hub", "12"));
|
|
|
+ System.out.println(sampleObject4);
|
|
|
+
|
|
|
+ // orElseGet : supplier 를 인자로 받음, 값이 없을 때만 실행됨
|
|
|
+ SampleObject sampleObject5 = Optional.ofNullable(getNullObject())
|
|
|
+ .orElseGet(() -> new SampleObject("flature1234", "Around Hub", "14"));
|
|
|
+ System.out.println(sampleObject5);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void test5() {
|
|
|
+ Stream<Integer> stream1 = Stream.of(1, 2, 3);
|
|
|
+ Stream<Integer> stream2 = Stream.of(4, 5, 6);
|
|
|
+ Stream<Integer> concat = Stream.concat(stream1, stream2);
|
|
|
+
|
|
|
+ concat.filter(val -> (val % 2) == 0)
|
|
|
+ .forEach(System.out::println);
|
|
|
+ }
|
|
|
+}
|