1 package com.foxinmy.weixin4j.tuple;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import javax.xml.bind.annotation.XmlElement;
7 import javax.xml.bind.annotation.XmlTransient;
8
9 import com.alibaba.fastjson.annotation.JSONField;
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class News implements NotifyTuple {
24
25 private static final long serialVersionUID = 3348756809039388415L;
26
27 @Override
28 public String getMessageType() {
29 return "news";
30 }
31
32
33
34
35 private static final int MAX_ARTICLE_COUNT = 8;
36
37
38
39
40
41
42 @JSONField(name = "articles")
43 @XmlElement(name = "Articles")
44 private LinkedList<Article> articles;
45
46 public News() {
47 this.articles = new LinkedList<Article>();
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61 public News addArticle(String title, String desc, String picUrl, String url) {
62 return addArticle(new Article(title, desc, picUrl, url));
63 }
64
65 public News addArticle(Article... articles) {
66 for (Article article : articles) {
67 this.articles.add(article);
68 }
69 return this;
70 }
71
72 public News addFirstArticle(Article article) {
73 articles.addFirst(article);
74 return this;
75 }
76
77 public void addLastArticle(Article article) {
78 articles.addLast(article);
79 }
80
81 public News removeFirstArticle() {
82 articles.removeFirst();
83 return this;
84 }
85
86 public News removeLastArticle() {
87 articles.removeLast();
88 return this;
89 }
90
91 @JSONField(serialize = false)
92 @XmlTransient
93 public boolean isMaxCount() {
94 return articles.size() == MAX_ARTICLE_COUNT;
95 }
96
97 public List<Article> getArticles() {
98 if (articles.size() > MAX_ARTICLE_COUNT) {
99 return articles.subList(0, MAX_ARTICLE_COUNT);
100 } else {
101 return articles;
102 }
103 }
104
105 @JSONField(serialize = false)
106 @XmlTransient
107 public List<Article> getFullArticles() {
108 return articles;
109 }
110
111 @Override
112 public String toString() {
113 StringBuilder sb = new StringBuilder();
114 for (Article article : articles) {
115 sb.append("{title=").append(article.getTitle());
116 sb.append(" ,description=").append(article.getDesc());
117 sb.append(" ,picUrl=").append(article.getPicUrl());
118 sb.append(" ,url=").append(article.getUrl()).append("}");
119 }
120 return sb.toString();
121 }
122 }