public boolean updateNewsStates(Collection<StatesUpdateInfo> statesUpdateInfos) { boolean changed = false; for (StatesUpdateInfo info : statesUpdateInfos) { long newsId = info.getNewsReference().getId(); if (info.getOldState() == null) { boolean itemRemoved = fNewsIds[INews.State.NEW.ordinal()].removeByElement(newsId); if (!itemRemoved) { EnumSet<State> remainingStates = EnumSet.allOf(INews.State.class); remainingStates.remove(INews.State.NEW); remainingStates.remove(info.getNewState()); for (INews.State state : remainingStates) { if (fNewsIds[state.ordinal()].removeByElement(newsId)) itemRemoved = true; } } if (itemRemoved) { changed = true; fNewsIds[info.getNewState().ordinal()].add(newsId); } } else if (fNewsIds[info.getOldState().ordinal()].removeByElement(newsId)) { changed = true; fNewsIds[info.getNewState().ordinal()].add(newsId); } } return changed; }
public List<NewsReference> removeNews(Set<INews.State> states) { List<NewsReference> newsRefs = getNews(states); for (INews.State state : states) { int index = state.ordinal(); fNewsIds[index].clear(); } return newsRefs; }
public int getNewsCount(Set<INews.State> states) { Assert.isNotNull(states, "states"); // $NON-NLS-1$ int count = 0; for (INews.State state : states) { count += fNewsIds[state.ordinal()].size(); } return count; }
public List<NewsReference> getNews(Set<INews.State> states) { List<NewsReference> newsRefs = new ArrayList<NewsReference>(getNewsCount(states)); for (INews.State state : states) { int index = state.ordinal(); LongArrayList newsIds = fNewsIds[index]; for (int i = 0, c = newsIds.size(); i < c; ++i) { long newsId = newsIds.get(i); Assert.isLegal(newsId != 0); newsRefs.add(new NewsReference(newsId)); } } return newsRefs; }
public Pair<Boolean, Boolean> setNews(Map<INews.State, List<NewsReference>> newsMap) { Assert.isNotNull(newsMap, "newsMap"); // $NON-NLS-1$ boolean changed = false; boolean isNewNewsAdded = false; Set<INews.State> statesToReset = EnumSet.allOf(INews.State.class); statesToReset.removeAll(newsMap.keySet()); /* Reset all ArrayLists whose state is not present in newsMap */ for (INews.State state : statesToReset) { int index = state.ordinal(); LongArrayList currentArrayList = fNewsIds[index]; if (currentArrayList.size() > 0) { currentArrayList.clear(); changed = true; } } /* For each Result */ for (Map.Entry<INews.State, List<NewsReference>> mapEntry : newsMap.entrySet()) { List<NewsReference> news = mapEntry.getValue(); INews.State state = mapEntry.getKey(); Assert.isNotNull(news, "news"); // $NON-NLS-1$ Assert.isNotNull(state, "state"); // $NON-NLS-1$ long[] newArray = new long[news.size()]; /* Fill Bucket */ for (int i = 0; i < news.size(); i++) { newArray[i] = news.get(i).getId(); } int index = state.ordinal(); LongArrayList currentArrayList = fNewsIds[index]; if (state.equals(INews.State.NEW)) { /* Check for added *new* News */ if (newArray.length > currentArrayList.size()) isNewNewsAdded = true; else { for (long value : newArray) { if (currentArrayList.indexOf(value) < 0) { isNewNewsAdded = true; break; } } } if (isNewNewsAdded || (newArray.length != currentArrayList.size())) { changed = true; currentArrayList.setAll(newArray); } } else { if (!currentArrayList.elementsEqual(newArray)) { currentArrayList.setAll(newArray); changed = true; } } } return Pair.create(changed, isNewNewsAdded); }