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.
163 lines
4.1 KiB
163 lines
4.1 KiB
package com.example.datacollect.model;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
public class Article {
|
|
private String title;
|
|
private String url;
|
|
private String content;
|
|
private String author;
|
|
private String publicDate;
|
|
|
|
public Article() {
|
|
this.title = "";
|
|
this.url = "";
|
|
this.content = "";
|
|
this.author = "";
|
|
this.publicDate = "";
|
|
}
|
|
|
|
public Article(String title, String url, String content) {
|
|
this.title = sanitize(title);
|
|
this.url = sanitize(url);
|
|
this.content = sanitize(content);
|
|
this.author = "";
|
|
this.publicDate = "";
|
|
}
|
|
|
|
public Article(String title, String url, String content, String author, String publicDate) {
|
|
this.title = sanitize(title);
|
|
this.url = sanitize(url);
|
|
this.content = sanitize(content);
|
|
this.author = sanitize(author);
|
|
this.publicDate = sanitize(publicDate);
|
|
}
|
|
|
|
private String sanitize(String value) {
|
|
return value == null ? "" : value.trim();
|
|
}
|
|
|
|
public String getTitle() {
|
|
return title;
|
|
}
|
|
|
|
public void setTitle(String title) {
|
|
this.title = sanitize(title);
|
|
}
|
|
|
|
public String getUrl() {
|
|
return url;
|
|
}
|
|
|
|
public void setUrl(String url) {
|
|
this.url = sanitize(url);
|
|
}
|
|
|
|
public String getContent() {
|
|
return content;
|
|
}
|
|
|
|
public void setContent(String content) {
|
|
this.content = sanitize(content);
|
|
}
|
|
|
|
public String getAuthor() {
|
|
return author;
|
|
}
|
|
|
|
public void setAuthor(String author) {
|
|
this.author = sanitize(author);
|
|
}
|
|
|
|
public String getPublicDate() {
|
|
return publicDate;
|
|
}
|
|
|
|
public void setPublicDate(String publicDate) {
|
|
this.publicDate = sanitize(publicDate);
|
|
}
|
|
|
|
public boolean hasTitle() {
|
|
return title != null && !title.isEmpty();
|
|
}
|
|
|
|
public boolean hasUrl() {
|
|
return url != null && !url.isEmpty();
|
|
}
|
|
|
|
public boolean hasContent() {
|
|
return content != null && !content.isEmpty();
|
|
}
|
|
|
|
public boolean hasAuthor() {
|
|
return author != null && !author.isEmpty();
|
|
}
|
|
|
|
public boolean hasPublicDate() {
|
|
return publicDate != null && !publicDate.isEmpty();
|
|
}
|
|
|
|
public boolean isValid() {
|
|
return hasTitle() && hasUrl();
|
|
}
|
|
|
|
public static List<Article> createList(int count) {
|
|
List<Article> articles = new ArrayList<>();
|
|
for (int i = 0; i < count; i++) {
|
|
articles.add(new Article());
|
|
}
|
|
return articles;
|
|
}
|
|
|
|
public static Article of(String title, String url) {
|
|
return new Article(title, url, "");
|
|
}
|
|
|
|
public static Article of(String title, String url, String content) {
|
|
return new Article(title, url, content);
|
|
}
|
|
|
|
public static Article of(String title, String url, String content, String author, String publicDate) {
|
|
return new Article(title, url, content, author, publicDate);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
Article article = (Article) o;
|
|
return Objects.equals(title, article.title) &&
|
|
Objects.equals(url, article.url);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(title, url);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "Article{" +
|
|
"title='" + title + '\'' +
|
|
", url='" + url + '\'' +
|
|
", author='" + author + '\'' +
|
|
", publicDate='" + publicDate + '\'' +
|
|
", contentLength=" + (content != null ? content.length() : 0) +
|
|
'}';
|
|
}
|
|
|
|
public String toSummary() {
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append("标题: ").append(title).append("\n");
|
|
sb.append("URL: ").append(url).append("\n");
|
|
if (hasAuthor()) {
|
|
sb.append("作者: ").append(author).append("\n");
|
|
}
|
|
if (hasPublicDate()) {
|
|
sb.append("日期: ").append(publicDate).append("\n");
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|