Skip to content

Commit

Permalink
refactor: 오브젝트 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
yj-leez committed Feb 2, 2024
1 parent a494c45 commit 6452f0b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 69 deletions.
82 changes: 82 additions & 0 deletions src/main/java/server/acode/domain/fragrance/dto/Extract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package server.acode.domain.fragrance.dto;

import java.util.ArrayList;
import java.util.List;

public class Extract {
private List<Long> one;
private List<Long> two;
private List<Long> three;
private List<Long> fourAnd;
private List<Long> fourOr;
private List<Long> fiveAnd;
private List<Long> fiveOr;

public Extract(List<Long> one, List<Long> two, List<Long> three, List<Long> fourAnd, List<Long> fourOr,
List<Long> fiveAnd, List<Long> fiveOr){
this.one = one;
this.two = two;
this.three = three;
this.fourAnd = fourAnd;
this.fourOr = fourOr;
this.fiveAnd = fiveAnd;
this.fiveOr = fiveOr;
}

public List<Long> dfs(int idx, List<Long> now){
List<Long> result = new ArrayList<>(now);

// 다섯번째 노드 도달
if(idx == 5){
result.retainAll(fiveAnd);
if(result.isEmpty()) {
result = new ArrayList<>(now);
result.retainAll(fiveOr);
if(result.isEmpty()){
return null; // 해당하는 값 없음
}
return result; // OR 결과 있음
}
else{
return result; } // AND 결과 있음
}

result.retainAll(relevant(idx)); // sout idx

// 4번째 노드는 AND 포함 결과 없을 때 OR 확인 필요
if(idx == 4 && result.isEmpty()){
result = new ArrayList<>(now);
result.retainAll(relevant(5));
}
System.out.println(idx + " : " + "result = "+result);



if(!result.isEmpty()) {
for (int i = idx + 1; i <= 5; i++) {
List<Long> newResult = dfs(i, result);
System.out.println(i + " : " + "new result = "+newResult);

// 아래 노드에서 다 포함하는 결과가 있다면 그걸로 리턴
if(newResult!=null) {
return newResult;
}
}
return result;
}
return null; // 해당하는 값 없음
}

private List<Long> relevant(int idx){
switch (idx){
case 1: return one;
case 2: return two;
case 3: return three;
case 4: return fourAnd;
case 5: return fourOr;
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import server.acode.domain.family.entity.Family;
import server.acode.domain.family.repository.FamilyRepository;
import server.acode.domain.family.repository.FragranceFamilyRepository;
import server.acode.domain.fragrance.dto.Extract;
import server.acode.domain.fragrance.dto.request.KeywordCond;
import server.acode.domain.fragrance.dto.response.ExtractFamily;
import server.acode.domain.fragrance.dto.response.ExtractFragrance;
Expand All @@ -28,13 +29,6 @@ public class ExtractService {
private final FragranceRepository fragranceRepository;
private final FragranceFamilyRepository fragranceFamilyRepository;
private final FamilyRepository familyRepository;
private static List<Long> one;
private static List<Long> two;
private static List<Long> three;
private static List<Long> fourAnd;
private static List<Long> fourOr;
private static List<Long> fiveAnd;
private static List<Long> fiveOr;

public ExtractResponse extractFamily(KeywordCond cond) {
List<Long> fragranceIdList = extractFragranceIdList(cond);
Expand Down Expand Up @@ -77,95 +71,42 @@ public ExtractResponse extractFamily(KeywordCond cond) {
*/
public List<Long> newExtractFragranceIdList(KeywordCond cond) {
/* concentration */
List<Long> one;
if(cond.getConcentration().size() == 2){
one = fragranceRepository.extractByConcentrationOr(cond.getConcentration().get(0), cond.getConcentration().get(1));
} else {
one = fragranceRepository.extractByConcentrationOr(cond.getConcentration().get(0), null);
}

/* season */
List<Long> two;
if(cond.getSeason().size() == 2){
two = fragranceRepository.extractByOnlySeasonOr(cond.getSeason().get(0), cond.getSeason().get(1));
} else {
two = fragranceRepository.extractByOnlySeasonOr(cond.getSeason().get(0), null);
}

/* main family */
List<Long> three;
if(cond.getMainFamily().size() == 2){
three = fragranceFamilyRepository.extractByOnlyMainFamilyOr(cond.getMainFamily().get(0), cond.getMainFamily().get(1));
} else {
three = fragranceFamilyRepository.extractByOnlyMainFamilyOr(cond.getMainFamily().get(0), null);
}

/* scent */
fourAnd = fragranceRepository.extractByOnlyScent(cond.getScent().get(0), cond.getScent().get(1));
fourOr = fragranceRepository.extractByOnlyScentOr(cond.getScent().get(0), cond.getScent().get(1));
List<Long> fourAnd = fragranceRepository.extractByOnlyScent(cond.getScent().get(0), cond.getScent().get(1));
List<Long> fourOr = fragranceRepository.extractByOnlyScentOr(cond.getScent().get(0), cond.getScent().get(1));

/* style */
fiveAnd = fragranceRepository.extractByOnlyStyle(cond.getStyle().get(0), cond.getStyle().get(1));
fiveOr = fragranceRepository.extractByOnlyStyleOr(cond.getStyle().get(0), cond.getStyle().get(1));
List<Long> fiveAnd = fragranceRepository.extractByOnlyStyle(cond.getStyle().get(0), cond.getStyle().get(1));
List<Long> fiveOr = fragranceRepository.extractByOnlyStyleOr(cond.getStyle().get(0), cond.getStyle().get(1));

return dfs(1, fragranceRepository.extractByConcentration(null, null)); // 모든 향수 id를 list에 넣어서 시작
Extract extract = new Extract(one, two, three, fourAnd, fourOr, fiveAnd, fiveOr);
return extract.dfs(1, fragranceRepository.extractByConcentration(null, null)); // 모든 향수 id를 list에 넣어서 시작

}

private List<Long> dfs(int idx, List<Long> now){
List<Long> result = new ArrayList<>(now);

// 다섯번째 노드 도달
if(idx == 5){
result.retainAll(fiveAnd);
if(result.isEmpty()) {
result = new ArrayList<>(now);
result.retainAll(fiveOr);
if(result.isEmpty()){
return null; // 해당하는 값 없음
}
return result; // OR 결과 있음
}
else{
return result; } // AND 결과 있음
}

result.retainAll(relevant(idx)); // sout idx

// 4번째 노드는 AND 포함 결과 없을 때 OR 확인 필요
if(idx == 4 && result.isEmpty()){
result = new ArrayList<>(now);
result.retainAll(relevant(5));
}
System.out.println(idx + " : " + "result = "+result);



if(!result.isEmpty()) {
for (int i = idx + 1; i <= 5; i++) {
List<Long> newResult = dfs(i, result);
System.out.println(i + " : " + "new result = "+newResult);

// 아래 노드에서 다 포함하는 결과가 있다면 그걸로 리턴
if(newResult!=null) {
return newResult;
}
}
return result;
}
return null; // 해당하는 값 없음
}

private List<Long> relevant(int idx){
switch (idx){
case 1: return one;
case 2: return two;
case 3: return three;
case 4: return fourAnd;
case 5: return fourOr;
}
return null;
}



private ExtractResponse getExtractResult (List<Long> familyIdList){
List<Family> families = familyRepository.findByIdIn(familyIdList);
List<ExtractFamily> extractFamilies = families.stream()
Expand Down

0 comments on commit 6452f0b

Please sign in to comment.