NIMSDK-AOS  10.9.90
V2NIMTopicMessageListOption.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.v2.topic.option;
2 
3 import android.text.TextUtils;
8 import com.netease.nimlib.util.StringUtil;
9 
10 /**
11  * Topic 消息列表查询参数。
12  *
13  * <p>用于查询某个 Topic 下的消息列表。</p>
14  */
16  private static final int MAX_LIMIT = 100;
17 
18  /**
19  * 查询目标 Topic。
20  */
21  private final V2NIMTopic topic;
22  /**
23  * 查询起始时间,单位毫秒。
24  */
25  private final long beginTime;
26  /**
27  * 查询结束时间,单位毫秒,0 表示不限制。
28  */
29  private final long endTime;
30  /**
31  * 锚点消息;传入后查询结果不包含该消息。
32  */
33  private final V2NIMMessage anchorMessage;
34  /**
35  * 单次查询上限。
36  *
37  * <p>取值范围为 1 到 100。</p>
38  */
39  private final int limit;
40  /**
41  * 查询方向,传空时默认为倒序方向。
42  */
43  private final V2NIMQueryDirection direction;
44  /**
45  * 结果排序方式,传空时默认升序。
46  */
47  private final V2NIMSortOrder sortOrder;
48 
49  /**
50  * 构造 Topic 消息列表查询参数。
51  *
52  * @param topic 查询目标 Topic
53  * @param beginTime 查询起始时间,单位毫秒
54  * @param endTime 查询结束时间,单位毫秒,0 表示不限制
55  * @param anchorMessage 锚点消息,查询结果不包含该消息
56  * @param limit 单次查询上限,取值范围为 1 到 100
57  * @param direction 查询方向,传空时默认 {@link V2NIMQueryDirection#V2NIM_QUERY_DIRECTION_DESC}
58  * @param sortOrder 结果排序方式,传空时默认 {@link V2NIMSortOrder#V2NIM_SORT_ORDER_ASC}
59  */
60  public V2NIMTopicMessageListOption(V2NIMTopic topic, long beginTime, long endTime, V2NIMMessage anchorMessage, int limit,
61  V2NIMQueryDirection direction, V2NIMSortOrder sortOrder) {
62  this.topic = topic;
63  this.beginTime = beginTime;
64  this.endTime = endTime;
65  this.anchorMessage = anchorMessage;
66  this.limit = limit;
67  this.direction = direction == null ? V2NIMQueryDirection.V2NIM_QUERY_DIRECTION_DESC : direction;
68  this.sortOrder = sortOrder == null ? V2NIMSortOrder.V2NIM_SORT_ORDER_ASC : sortOrder;
69  }
70 
71  /**
72  * 返回查询目标 Topic。
73  *
74  * @return 查询目标 Topic
75  */
76  public V2NIMTopic getTopic() {
77  return topic;
78  }
79 
80  /**
81  * 返回起始时间。
82  *
83  * @return 起始时间,单位毫秒
84  */
85  public long getBeginTime() {
86  return beginTime;
87  }
88 
89  /**
90  * 返回结束时间。
91  *
92  * @return 结束时间,单位毫秒
93  */
94  public long getEndTime() {
95  return endTime;
96  }
97 
98  /**
99  * 返回锚点消息。
100  *
101  * @return 锚点消息
102  */
104  return anchorMessage;
105  }
106 
107  /**
108  * 返回查询上限。
109  *
110  * @return 单次查询上限,取值范围为 1 到 100
111  */
112  public int getLimit() {
113  return limit;
114  }
115 
116  /**
117  * 返回查询方向。
118  *
119  * @return 查询方向
120  */
122  return direction;
123  }
124 
125  /**
126  * 返回结果排序。
127  *
128  * @return 结果排序方式
129  */
131  return sortOrder;
132  }
133 
134  /**
135  * 返回参数非法原因。
136  *
137  * @return 参数合法时返回 `null`,否则返回具体错误原因
138  */
139  public String isValidWithErrorMsg() {
140  if (topic == null) {
141  return "topic is null";
142  }
143  if (TextUtils.isEmpty(topic.getConversationId())) {
144  return "topic conversationId is empty";
145  }
146  if (topic.getTopicId() <= 0) {
147  return "topicId must be greater than 0";
148  }
149  if (topic.getCreateTime() <= 0) {
150  return "topic createTime must be greater than 0";
151  }
152  if (TextUtils.isEmpty(topic.getMessageClientId())) {
153  return "topic messageClientId is empty";
154  }
155  if (StringUtil.parseLong(topic.getMessageServerId(), 0L) <= 0) {
156  return "topic messageServerId must be greater than 0";
157  }
158  if (topic.getMessageTime() <= 0) {
159  return "topic messageTime must be greater than 0";
160  }
161  if (limit <= 0) {
162  return "limit must be greater than 0";
163  }
164  if (limit > MAX_LIMIT) {
165  return "limit must be less than or equal to 100";
166  }
167  if (beginTime < 0) {
168  return "beginTime must be greater than or equal to 0";
169  }
170  if (endTime < 0) {
171  return "endTime must be greater than or equal to 0";
172  }
173  if (endTime != 0 && beginTime > endTime) {
174  return "beginTime must be less than or equal to endTime";
175  }
176  if (anchorMessage == null) {
177  return null;
178  }
179  long anchorTime = anchorMessage.getCreateTime();
180  if (anchorTime < beginTime) {
181  return "anchorMessage createTime must be greater than or equal to beginTime";
182  }
183  if (endTime != 0 && anchorTime > endTime) {
184  return "anchorMessage createTime must be less than or equal to endTime";
185  }
186  return null;
187  }
188 
189  /**
190  * 参数是否合法。
191  *
192  * @return `true` 表示参数合法,否则为 `false`
193  */
194  public boolean isValid() {
195  return isValidWithErrorMsg() == null;
196  }
197 }
String getMessageServerId()
返回根消息服务端 ID。
long getMessageTime()
返回根消息时间戳。
String getMessageClientId()
返回根消息客户端 ID。
long getCreateTime()
获取消息时间,服务器器时间
String getConversationId()
返回 Topic 所属会话 ID。
V2NIMTopicMessageListOption(V2NIMTopic topic, long beginTime, long endTime, V2NIMMessage anchorMessage, int limit, V2NIMQueryDirection direction, V2NIMSortOrder sortOrder)
构造 Topic 消息列表查询参数。
long getCreateTime()
返回 Topic 创建时间。