You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.7 KiB
139 lines
3.7 KiB
package com.example.datacollect.repository;
|
|
|
|
import com.example.datacollect.model.Article;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
public class ArticleRepository {
|
|
private final List<Article> articles = new ArrayList<>();
|
|
|
|
public void add(Article article) {
|
|
if (article == null) {
|
|
throw new IllegalArgumentException("Article cannot be null");
|
|
}
|
|
if (!article.isValid()) {
|
|
throw new IllegalArgumentException("Article must have valid title and url");
|
|
}
|
|
articles.add(article);
|
|
}
|
|
|
|
public void addAll(List<Article> articleList) {
|
|
if (articleList == null) {
|
|
throw new IllegalArgumentException("Article list cannot be null");
|
|
}
|
|
for (Article article : articleList) {
|
|
if (article != null && article.isValid()) {
|
|
articles.add(article);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void addAll(Article... articles) {
|
|
if (articles == null) {
|
|
throw new IllegalArgumentException("Articles array cannot be null");
|
|
}
|
|
for (Article article : articles) {
|
|
add(article);
|
|
}
|
|
}
|
|
|
|
public List<Article> getAll() {
|
|
return Collections.unmodifiableList(articles);
|
|
}
|
|
|
|
public Article get(int index) {
|
|
if (index < 0 || index >= articles.size()) {
|
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + articles.size());
|
|
}
|
|
return articles.get(index);
|
|
}
|
|
|
|
public Article findById(String url) {
|
|
if (url == null || url.isEmpty()) {
|
|
return null;
|
|
}
|
|
for (Article article : articles) {
|
|
if (url.equals(article.getUrl())) {
|
|
return article;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<Article> findByTitle(String keyword) {
|
|
if (keyword == null || keyword.isEmpty()) {
|
|
return Collections.emptyList();
|
|
}
|
|
List<Article> result = new ArrayList<>();
|
|
for (Article article : articles) {
|
|
if (article.getTitle().contains(keyword)) {
|
|
result.add(article);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public boolean remove(Article article) {
|
|
if (article == null) {
|
|
return false;
|
|
}
|
|
return articles.remove(article);
|
|
}
|
|
|
|
public boolean removeByUrl(String url) {
|
|
if (url == null || url.isEmpty()) {
|
|
return false;
|
|
}
|
|
for (int i = 0; i < articles.size(); i++) {
|
|
if (url.equals(articles.get(i).getUrl())) {
|
|
articles.remove(i);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void clear() {
|
|
articles.clear();
|
|
}
|
|
|
|
public int size() {
|
|
return articles.size();
|
|
}
|
|
|
|
public boolean isEmpty() {
|
|
return articles.isEmpty();
|
|
}
|
|
|
|
public boolean contains(Article article) {
|
|
return articles.contains(article);
|
|
}
|
|
|
|
public boolean containsUrl(String url) {
|
|
if (url == null || url.isEmpty()) {
|
|
return false;
|
|
}
|
|
for (Article article : articles) {
|
|
if (url.equals(article.getUrl())) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "ArticleRepository{size=" + articles.size() + "}";
|
|
}
|
|
|
|
public String toDetailedString() {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append("ArticleRepository{size=").append(articles.size()).append("}\n");
|
|
for (int i = 0; i < articles.size(); i++) {
|
|
sb.append(i + 1).append(". ").append(articles.get(i).toSummary());
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|