CardCollection removeCards( CardCollection oldCards, Iterable<Card> cardsToRemove, TrackableProperty key) { if (cardsToRemove == null || oldCards == null) { return oldCards; } TrackableCollection<CardView> views = get(key); boolean needFlagAsChanged = false; for (Card c : cardsToRemove) { if (oldCards.remove(c)) { if (views == null) { set(key, null); } else if (views.remove(c.getView())) { if (views.isEmpty()) { views = null; set(key, null); // avoid keeping around an empty collection needFlagAsChanged = false; // doesn't need to be flagged a second time } else { needFlagAsChanged = true; } } if (oldCards.isEmpty()) { oldCards = null; // avoid keeping around an empty collection break; } } } if (needFlagAsChanged) { flagAsChanged(key); } return oldCards; }
CardCollection addCards( CardCollection oldCards, Iterable<Card> cardsToAdd, TrackableProperty key) { if (cardsToAdd == null) { return oldCards; } TrackableCollection<CardView> views = get(key); if (oldCards == null) { oldCards = new CardCollection(); } boolean needFlagAsChanged = false; for (Card c : cardsToAdd) { if (c != null && oldCards.add(c)) { if (views == null) { views = new TrackableCollection<CardView>(); views.add(c.getView()); set(key, views); } else if (views.add(c.getView())) { needFlagAsChanged = true; } } } if (needFlagAsChanged) { flagAsChanged(key); } return oldCards; }
CardCollection setCards(CardCollection oldCards, CardCollection newCards, TrackableProperty key) { if (newCards == null || newCards.isEmpty()) { // avoid storing empty collections set(key, null); return null; } set(key, CardView.getCollection(newCards)); // TODO prevent overwriting list if not necessary return newCards; }
CardCollection removeCard(CardCollection oldCards, Card cardToRemove, TrackableProperty key) { if (cardToRemove == null || oldCards == null) { return oldCards; } if (oldCards.remove(cardToRemove)) { TrackableCollection<CardView> views = get(key); if (views == null) { set(key, null); } else if (views.remove(cardToRemove.getView())) { if (views.isEmpty()) { set(key, null); // avoid keeping around an empty collection } else { flagAsChanged(key); } } if (oldCards.isEmpty()) { oldCards = null; // avoid keeping around an empty collection } } return oldCards; }
CardCollection addCard(CardCollection oldCards, Card cardToAdd, TrackableProperty key) { if (cardToAdd == null) { return oldCards; } if (oldCards == null) { oldCards = new CardCollection(); } if (oldCards.add(cardToAdd)) { TrackableCollection<CardView> views = get(key); if (views == null) { views = new TrackableCollection<CardView>(); views.add(cardToAdd.getView()); ; set(key, views); } else if (views.add(cardToAdd.getView())) { flagAsChanged(key); } } return oldCards; }