-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: NativePokemonComparator 생성
- Loading branch information
Showing
4 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...d/pokerogue/src/main/java/com/pokerogue/helper/biome/service/NativePokemonComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...kerogue/src/test/java/com/pokerogue/helper/biome/service/NativePokemonComparatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |