From 64d64dbf2095372789361e99781bcab2148e77df Mon Sep 17 00:00:00 2001 From: zhangsiyuan <3837703520@qq.com> Date: Tue, 19 May 2026 16:45:00 +0800 Subject: [PATCH] =?UTF-8?q?w11-=E5=BC=A0=E6=80=9D=E6=B8=8A-202401070104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w11/ArticleRepository.java | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 w11/ArticleRepository.java diff --git a/w11/ArticleRepository.java b/w11/ArticleRepository.java new file mode 100644 index 0000000..427d664 --- /dev/null +++ b/w11/ArticleRepository.java @@ -0,0 +1,56 @@ +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().trim().isEmpty()) { + logger.error("Attempted to add article with empty title"); + throw new IllegalArgumentException("Article title cannot be null or empty"); + } + if (article.getUrl() == null || article.getUrl().trim().isEmpty()) { + logger.error("Attempted to add article with empty URL"); + throw new IllegalArgumentException("Article URL cannot be null or empty"); + } + articles.add(article); + logger.debug("Added article: {}", article.getTitle()); + } + + public List
getAll() { + logger.debug("Retrieving all articles, count: {}", articles.size()); + return Collections.unmodifiableList(articles); + } + + public int size() { + return articles.size(); + } + + public void addAll(List
articleList) { + if (articleList == null) { + logger.error("Attempted to add null article list"); + throw new IllegalArgumentException("Article list cannot be null"); + } + for (Article article : articleList) { + add(article); + } + logger.info("Added {} articles", articleList.size()); + } + + public void clear() { + int count = articles.size(); + articles.clear(); + logger.info("Cleared {} articles", count); + } +}