Handle merging badges when adding to account
This commit is contained in:
parent
d5ebf2f2ed
commit
16dba09b61
|
@ -324,7 +324,24 @@ public class Account {
|
||||||
public void addBadge(AccountBadge badge) {
|
public void addBadge(AccountBadge badge) {
|
||||||
requireNotStale();
|
requireNotStale();
|
||||||
|
|
||||||
badges.add(badge);
|
boolean added = false;
|
||||||
|
for (int i = 0; i < badges.size(); i++) {
|
||||||
|
AccountBadge badgeInList = badges.get(i);
|
||||||
|
if (Objects.equals(badgeInList.getId(), badge.getId())) {
|
||||||
|
if (added) {
|
||||||
|
badges.remove(i);
|
||||||
|
i--;
|
||||||
|
} else {
|
||||||
|
badges.set(i, badgeInList.mergeWith(badge));
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!added) {
|
||||||
|
badges.add(badge);
|
||||||
|
}
|
||||||
|
|
||||||
purgeStaleBadges();
|
purgeStaleBadges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,32 @@ public class AccountBadge {
|
||||||
this.visible = visible;
|
this.visible = visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new AccountBadge that is a merging of the two originals. IDs must match for this operation to make sense.
|
||||||
|
* The expiration will be the later of the two.
|
||||||
|
* Visibility will be set if either of the passed in objects is visible.
|
||||||
|
*/
|
||||||
|
public AccountBadge mergeWith(AccountBadge other) {
|
||||||
|
if (!Objects.equals(other.id, id)) {
|
||||||
|
throw new IllegalArgumentException("merging badges should only take place for same id");
|
||||||
|
}
|
||||||
|
|
||||||
|
final Instant latestExpiration;
|
||||||
|
if (expiration == null || other.expiration == null) {
|
||||||
|
latestExpiration = null;
|
||||||
|
} else if (expiration.isAfter(other.expiration)) {
|
||||||
|
latestExpiration = expiration;
|
||||||
|
} else {
|
||||||
|
latestExpiration = other.expiration;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AccountBadge(
|
||||||
|
id,
|
||||||
|
latestExpiration,
|
||||||
|
visible || other.visible
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue