From d4a8b7e39ce277dd04276ac434fec4c318229cf6 Mon Sep 17 00:00:00 2001 From: zhouzihao Date: Tue, 19 May 2026 17:06:20 +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/repository'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W11/repository/ArticleRepository.java | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 W11/repository/ArticleRepository.java diff --git a/W11/repository/ArticleRepository.java b/W11/repository/ArticleRepository.java new file mode 100644 index 0000000..e9a97db --- /dev/null +++ b/W11/repository/ArticleRepository.java @@ -0,0 +1,67 @@ +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.warn("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.warn("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 void addAll(List
articlesToAdd) { + if (articlesToAdd == null) { + logger.error("Attempted to add null list of articles"); + throw new IllegalArgumentException("Article list cannot be null"); + } + for (Article article : articlesToAdd) { + add(article); + } + logger.info("Added {} articles to repository", articlesToAdd.size()); + } + + public List
getAll() { + logger.debug("Retrieving all articles, count: {}", articles.size()); + return Collections.unmodifiableList(articles); + } + + public int size() { + return articles.size(); + } + + public void clear() { + logger.info("Clearing repository, removed {} articles", articles.size()); + articles.clear(); + } + + public boolean isEmpty() { + return articles.isEmpty(); + } + + public Article get(int index) { + if (index < 0 || index >= articles.size()) { + logger.error("Attempted to access article at invalid index: {}", index); + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + articles.size()); + } + return articles.get(index); + } +}