package com.example.datacollect.repository; import com.example.datacollect.model.Article; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArticleRepository { private final List
articles = new ArrayList<>(); public void add(Article article) { if (article == null) { throw new IllegalArgumentException("Article cannot be null"); } articles.add(article); } // ★ 新增:批量添加方法以及注意防御 null public void addAll(List
articles) { if (articles == null) { throw new IllegalArgumentException("Articles list cannot be null"); } for (Article article : articles) { if (article == null) { throw new IllegalArgumentException("Article in list cannot be null"); } this.articles.add(article); } } public List
getAll() { return Collections.unmodifiableList(articles); } public int size() { return articles.size(); } public void clear() { articles.clear(); } }