further optimisation by using iterator rather than forloop with if-clauses

This commit is contained in:
leriomaggio
2021-08-25 18:44:59 +01:00
parent eff9bc4d89
commit 7b70a34da0
2 changed files with 30 additions and 22 deletions

View File

@@ -90,6 +90,12 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
this.collectorNumber = collectorNumber; this.collectorNumber = collectorNumber;
} }
public static String compose(String cardName, boolean isFoil){
if (isFoil)
return cardName+foilSuffix;
return cardName;
}
public static String compose(String cardName, String setCode) { public static String compose(String cardName, String setCode) {
setCode = setCode != null ? setCode : ""; setCode = setCode != null ? setCode : "";
cardName = cardName != null ? cardName : ""; cardName = cardName != null ? cardName : "";
@@ -462,15 +468,15 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
} }
// 2. Card lookup in edition with specified filter didn't work. // 2. Card lookup in edition with specified filter didn't work.
// So now check whether the cards exists in the DB first, // So now check whether the cards exist in the DB first,
// and select pick the card based on current SetPreference policy as a fallback // and select pick the card based on current SetPreference policy as a fallback
Collection<PaperCard> cards = getAllCards(request.cardName); Collection<PaperCard> cards = getAllCards(request.cardName);
if (cards.isEmpty()) // Never null being this a view in MultiMap if (cards.isEmpty()) // Never null being this a view in MultiMap
return null; return null;
// Either No Edition has been specified OR as a fallback in case of any error! // Either No Edition has been specified OR as a fallback in case of any error!
// get card using the default card art preference // get card using the default card art preference
result = getCardFromEditions(request.cardName, this.defaultCardArtPreference, request.artIndex); String cardRequest = CardRequest.compose(request.cardName, request.isFoil);
return result != null && request.isFoil ? result.getFoiled() : result; return getCardFromEditions(cardRequest, this.defaultCardArtPreference, request.artIndex);
} }
/* /*
@@ -567,8 +573,8 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
} }
@Override @Override
public PaperCard getCardFromEditions(final String cardName, final CardArtPreference artPreference, int artIndex) { public PaperCard getCardFromEditions(final String cardInfo, final CardArtPreference artPreference, int artIndex) {
return this.tryToGetCardFromEditions(cardName, artPreference, artIndex); return this.tryToGetCardFromEditions(cardInfo, artPreference, artIndex);
} }
/* /*
@@ -651,7 +657,6 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
/* 2. Retrieve cards based of [Frame]Set Preference /* 2. Retrieve cards based of [Frame]Set Preference
================================================ */ ================================================ */
// Collect the list of all editions found for target card // Collect the list of all editions found for target card
List<CardEdition> cardEditions = new ArrayList<>(); List<CardEdition> cardEditions = new ArrayList<>();
Map<String, PaperCard> candidatesCard = new HashMap<>(); Map<String, PaperCard> candidatesCard = new HashMap<>();
@@ -659,7 +664,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
if (card.getArtIndex() != cr.artIndex) if (card.getArtIndex() != cr.artIndex)
continue; continue;
String setCode = card.getEdition(); String setCode = card.getEdition();
CardEdition ed = null; CardEdition ed;
if (setCode.equals(CardEdition.UNKNOWN.getCode())) if (setCode.equals(CardEdition.UNKNOWN.getCode()))
ed = CardEdition.UNKNOWN; ed = CardEdition.UNKNOWN;
else else
@@ -669,6 +674,9 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
candidatesCard.put(setCode, card); candidatesCard.put(setCode, card);
} }
} }
if (cardEditions.isEmpty())
return null; // nothing to do
// Filter Cards Editions based on set preferences // Filter Cards Editions based on set preferences
List<CardEdition> acceptedEditions = Lists.newArrayList(Iterables.filter(cardEditions, new Predicate<CardEdition>() { List<CardEdition> acceptedEditions = Lists.newArrayList(Iterables.filter(cardEditions, new Predicate<CardEdition>() {
@Override @Override
@@ -693,22 +701,14 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
Collections.reverse(acceptedEditions); // newest editions first Collections.reverse(acceptedEditions); // newest editions first
} }
PaperCard candidate = null; final Iterator<CardEdition> editionIterator = acceptedEditions.iterator();
for (CardEdition ed : acceptedEditions) { CardEdition ed = editionIterator.next();
PaperCard cardFromSet = candidatesCard.get(ed.getCode()); //getCardFromSet(cr.cardName, ed, cr.artIndex, cr.isFoil); PaperCard candidate = candidatesCard.get(ed.getCode());
if (candidate == null) while (!candidate.hasImage() && editionIterator.hasNext()) {
// save the first card found, as the last backup in case no other candidate *with image* will be found ed = editionIterator.next();
candidate = cardFromSet; candidate = candidatesCard.get(ed.getCode());
if (cardFromSet != null && cardFromSet.hasImage()) {
candidate = cardFromSet;
break; // we're done here: found card **with Image**
}
} }
if (candidate == null) //If any, we're sure that at least one candidate is always returned despite it having any image
return null;
//If any, we're sure that at least one candidate is always returned nevertheless it has image or not
return cr.isFoil ? candidate.getFoiled() : candidate; return cr.isFoil ? candidate.getFoiled() : candidate;
} }

View File

@@ -83,4 +83,12 @@ public class CardDbPerformanceTests extends CardDbTestCase {
assertNotNull(card); assertNotNull(card);
} }
} }
@Test
public void testGetCardFullDbLegacyImplementationToProfile(){
for (String name : this.fullDbCardNames) {
PaperCard card = this.legacyCardDb.getCard(name);
assertNotNull(card);
}
}
} }