You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.5 KiB
48 lines
1.5 KiB
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.logging.Logger;
|
|
|
|
public class ArticleRepository11 {
|
|
private static final Logger logger = Logger.getLogger(ArticleRepository11.class.getName());
|
|
private final List<Article11> articles = new ArrayList<>();
|
|
|
|
public void add(Article11 article) {
|
|
if (article == null) {
|
|
logger.severe("添加失败:文章对象为 null");
|
|
throw new IllegalArgumentException("文章对象不能为空");
|
|
}
|
|
articles.add(article);
|
|
logger.info("成功添加文章:" + article.getTitle());
|
|
}
|
|
|
|
public void addAll(List<Article11> newArticles) {
|
|
if (newArticles == null) {
|
|
logger.severe("批量添加失败:传入列表为 null");
|
|
throw new IllegalArgumentException("文章列表不能为空");
|
|
}
|
|
int successCount = 0;
|
|
for (Article11 article : newArticles) {
|
|
if (article == null) {
|
|
logger.warning("列表中存在空对象,已跳过");
|
|
continue;
|
|
}
|
|
articles.add(article);
|
|
successCount++;
|
|
}
|
|
logger.info("批量添加完成,共成功添加 " + successCount + " 篇文章");
|
|
}
|
|
|
|
public List<Article11> getAll() {
|
|
return Collections.unmodifiableList(articles);
|
|
}
|
|
|
|
public int size() {
|
|
return articles.size();
|
|
}
|
|
|
|
public void clear() {
|
|
logger.info("清空文章集合,原有数量:" + articles.size());
|
|
articles.clear();
|
|
}
|
|
}
|