From 30d24834c6a90dbf18a5e9d01441e7090737d2a2 Mon Sep 17 00:00:00 2001 From: wangbo <1248863822@qq.com> Date: Wed, 20 May 2026 22:12:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w11/resources'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w11/resources/ArticleRepository.java.java | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 w11/resources/ArticleRepository.java.java 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