1 changed files with 47 additions and 0 deletions
@ -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<Article> 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<Article> 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(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue