diff --git a/w11/resources/ArticleRepository.java.java b/w11/resources/ArticleRepository.java.java new file mode 100644 index 0000000..8e7e71c --- /dev/null +++ b/w11/resources/ArticleRepository.java.java @@ -0,0 +1,47 @@ +package com.example.datacollect.repository; + +import com.example.datacollect.model.Article; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ArticleRepository { + private static final Logger logger = LoggerFactory.getLogger(ArticleRepository.class); + private final List
articles = new ArrayList<>(); + + public void add(Article article) { + // 防御检查 + if (article == null) { + logger.error("Attempted to add null article"); + throw new IllegalArgumentException("Article cannot be null"); + } + if (article.getTitle() == null || article.getTitle().isBlank()) { + logger.error("Attempted to add article with null or blank title: {}", article.getUrl()); + throw new IllegalArgumentException("Article title cannot be null or blank"); + } + if (article.getUrl() == null || article.getUrl().isBlank()) { + logger.error("Attempted to add article with null or blank URL"); + throw new IllegalArgumentException("Article URL cannot be null or blank"); + } + + articles.add(article); + logger.debug("Article added: {} - {}", article.getTitle(), article.getUrl()); + } + + public List
getAll() { + logger.debug("Retrieved {} articles", articles.size()); + return Collections.unmodifiableList(articles); + } + + public int size() { + return articles.size(); + } + + public void clear() { + logger.info("Clearing all articles, previous count: {}", articles.size()); + articles.clear(); + } +} \ No newline at end of file