public synchronized ArticlesIndex getIndex(OperationObserver observer) { if (index == null) { boolean imported = importFromResource(); File indexFile = getIndexFile(); observer.onOperationInfoChanged(OperationObserver.LOADING_CACHE, indexFile.getAbsolutePath()); if (!indexFile.exists()) { log.info("Article index file does not exist:", indexFile); index = new ArticlesIndex(); return index; } try { index = JsonMapper.deserialize(indexFile, ArticlesIndex.class, typeResolver); log.info("Article index loaded:", index.getArticlesCount(), "articles"); } catch (Exception ex) { log.error("Article indes loading failed:", ex); indexFile.delete(); return getIndex(observer); } if (imported) { viewed.addAll(index.getArticles()); } else { for (ArticleRef article : index.getArticles()) { article.repairTitle(); } } } return index; }
public List<ArticleRef> getNewArticles(OperationObserver observer) { ArticlesIndex index = getIndex(observer); List<ArticleRef> ret = new ArrayList<ArticleRef>(); for (ArticleRef ref : index.getArticles()) { if (viewed.contains(ref)) continue; ret.add(ref); } return ret; }