think0831 / javaPractice

자바 공부용
0 stars 0 forks source link

Java Practice #3

Open think0831 opened 5 years ago

think0831 commented 5 years ago

자바문법 Generic에 대해 학습한다.

think0831 commented 5 years ago

Generic


파라미터 타입이나 리턴타입 같은 클래스나 메소드에서 사용하는 타입을 유동적으로 바꿀 수 있는 기능

형식 public class 클래스명 {....} public interface 인터페이스명 {....}

이런식으로 사용한다.


class Box {
    private T t;
    public void set(T t) {
        this.t = t;
    }
    public T get() {
        return this.t;
    }
}

public static void main(String[] args) {
    Box box = new Box();
    box.set("hongildong");
    String name = (String)box.get();

    Box box2 = new Box<>();
    box2.set(new Apple());
    Apple apple = box2.get();
}

class 클래스이름<T, M> {....}

예제


class Product {
    private T kind;
    private M model;

    public T getKind() {return this.kind;}
    public M getModel() {return this.model;}

    public void setKind(T kind) {this.kind = kind;}

    public void setModel(M model) {this.model = model;}
}

public static void main(String[] args) {
    Product product1 = new Product();
    product1.setKind(new Tv());
    product1.setModel("smart Tv");
    Tv tv = product1.getKind();
    String tvModel = product1.getModel();

    Product product2 = new Product();
    product2.setKind(new Car());
    product2.setModel("dezel");
    Car car = product2.getKind();
    String carModel = product2.getModel();
}
think0831 commented 5 years ago

Enum


enum Type {
    ONE, TWO, THREE, FOUR
}

열거된 순서에 따라 0부터 차례로 증가하는 값을가짐

enum Digits {
    ONE(1, "1"),
    TEN(10, "10"),
    HUNDRED(100, "100");

    int intDigit;
    String strDgit;

    Digits(int intDigit, String strDgit) {
        this.intDigit = intDigit;
        this.strDgit = strDgit;
    }

    public void setIntDigit() {
        intDigit = 1011;
    }
}

enum 값 이외에도 다른 값을 가질 수 있음 클래스이기 때문에 메소드를 가질 수 있음 내부 값을 변경 할 수 있는데 변경할 수 있는 것이 옳은 것인지 모르겠음 enum을 쓰겠다는 것은 상수값을 클래스처럼 활용하기 위함이지 않은가 함

public static void main(String[] args) {
    Digits digits10 = Digits.TEN;
    Digits digits100 = Digits.HUNDRED;

    System.out.println(digits10);
    System.out.println(digits10.intDigit);
    System.out.println(digits10.strDigit);

    System.out.println(digits100);
    System.out.println(digits100.intDigit);
    System.out.println(digits100.strDigit);

    digits100.setIntDigit(20, "20");
    System.out.println(digits100.intDigit);
    System.out.println(digits100.strDigit);
}

요딴식으로 쓸수 있음 값을 변경하는건 생각해볼필요가 있을거 같음

메소드

1) values : 열거된 모든 원소를 담은 배열 을 반환

Number[] numbers = Number.values();

2) ordinal : 열거된 순서의 정수값으로 반환

Number number1 = Number.ONE;
Number number2 = Number.TWO;
Number number3 = Number.THREE;
Number number4 = Number.FOUR;

System.out.println(number1.ordinal());
System.out.println(number2.ordinal());
System.out.println(number3.ordinal());
System.out.println(number4.ordinal());
0
1
2
3

3) valueOf : 열거형의 이름이 일치하는 원소를 반환

Number.valueOf("ONE");
think0831 commented 4 years ago

LocalDate의 확장?

with(TemporalAdjusters) 정리

get(TemporalField)

think0831 commented 4 years ago

querydsl 서브쿼리

통계를 내기위한 쿼리를 작성 할 필요가 생김

sql문 작성 후 테스트 image

이슈 mysql/mariadb 에서 NULLIF, IFNULL은 다른 결과를 가져온다.

현재 querydsl에서는 IFNULL에 해당되는 메소드가 없기 때문에 coalesce 사용

querydsl로 작성 image

네모칸 부분이


IFNULL((entering_weight - (
    SELECT SUM(release_weigth)
    FROM release_history r
    WHERE p.product_id = r.product_id
        AND release_date >= "2007/10/10"
        AND release_date <= "2020/10/10"
    GROUP BY r.product_id
)), entering_weight) * progress_price_per_unit AS sum_progress_balance