Skip to content

Commit

Permalink
refactor: NativePokemonComparator 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
jongmee committed Oct 13, 2024
1 parent db8ab5d commit 7548b68
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Field;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class NativePokemon {

private static final String BOSS = "보스";

@Field("tier")
private Tier tier;

Expand All @@ -25,4 +25,8 @@ public boolean isWild() {
public boolean isBoss() {
return tier.isBoss();
}

public int getRarity() {
return this.tier.getRarity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
@Getter
public enum Tier {

COMMON("보통"),
UNCOMMON("드묾"),
RARE("레어"),
SUPER_RARE("슈퍼 레어"),
ULTRA_RARE("울트라 레어"),
BOSS("보스"),
BOSS_RARE("레어 보스"),
BOSS_SUPER_RARE("슈퍼 레어 보스"),
BOSS_ULTRA_RARE("슈퍼 울트라 레어 보스"),
COMMON("보통", 1),
UNCOMMON("드묾", 2),
RARE("레어", 3),
SUPER_RARE("슈퍼 레어", 4),
ULTRA_RARE("울트라 레어", 5),
BOSS("보스", 6),
BOSS_RARE("레어 보스", 7),
BOSS_SUPER_RARE("슈퍼 레어 보스", 8),
BOSS_ULTRA_RARE("슈퍼 울트라 레어 보스", 9),
;

private final String name;
private final int rarity;

Tier(String name) {
Tier(String name, int rarity) {
this.name = name;
this.rarity = rarity;
}

public boolean isWild() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.pokerogue.helper.biome.service;

import com.pokerogue.helper.biome.data.NativePokemon;
import java.util.Comparator;

public class NativePokemonComparator implements Comparator<NativePokemon> {

public static final String ASCENDING = "asc";
public static final String DESCENDING = "desc";
private static final NativePokemonComparator ASCENDING_COMPARATOR = new NativePokemonComparator(ASCENDING);
private static final NativePokemonComparator DESCENDING_COMPARATOR = new NativePokemonComparator(DESCENDING);

private final String criteria;

private NativePokemonComparator(String criteria) {
this.criteria = criteria;
}

public static NativePokemonComparator of(String criteria) {
if (criteria.equals(ASCENDING)) {
return ASCENDING_COMPARATOR;
}
return DESCENDING_COMPARATOR;
}

@Override
public int compare(NativePokemon firstPokemon, NativePokemon secondPokemon) {
if (this.criteria.equals(ASCENDING)) {
return Integer.compare(firstPokemon.getRarity(), secondPokemon.getRarity());
}

return Integer.compare(secondPokemon.getRarity(), firstPokemon.getRarity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.pokerogue.helper.biome.service;

import static com.pokerogue.helper.biome.service.NativePokemonComparator.ASCENDING;
import static com.pokerogue.helper.biome.service.NativePokemonComparator.DESCENDING;
import static org.assertj.core.api.Assertions.assertThat;

import com.pokerogue.helper.biome.data.NativePokemon;
import com.pokerogue.helper.biome.data.Tier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class NativePokemonComparatorTest {

private final List<NativePokemon> nativePokemons = new ArrayList<>(List.of(
new NativePokemon(Tier.RARE, List.of("bulbasaur", "venusaur")),
new NativePokemon(Tier.COMMON, List.of("pikachu", "raichu")),
new NativePokemon(Tier.BOSS_RARE, List.of("charmander")),
new NativePokemon(Tier.BOSS_SUPER_RARE, List.of("roserade"))
));

@Test
@DisplayName("바이옴 포켓몬을 티어의 희귀도가 낮은 순으로 정렬한다.")
void sortNativePokemonsAscending() {
Collections.sort(nativePokemons, NativePokemonComparator.of(ASCENDING));

assertThat(nativePokemons).extracting(NativePokemon::getTier)
.containsExactly(Tier.COMMON, Tier.RARE, Tier.BOSS_RARE, Tier.BOSS_SUPER_RARE);
}

@Test
@DisplayName("바이옴 포켓몬을 티어의 희귀도가 높은 순으로 정렬한다.")
void sortNativePokemonsDescending() {
Collections.sort(nativePokemons, NativePokemonComparator.of(DESCENDING));

assertThat(nativePokemons).extracting(NativePokemon::getTier)
.containsExactly(Tier.BOSS_SUPER_RARE, Tier.BOSS_RARE, Tier.RARE, Tier.COMMON);
}
}

0 comments on commit 7548b68

Please sign in to comment.