NIMSDK-AOS  10.9.71
V2NIMCloudMessageListOption.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.v2.message.option;
2 
3 import androidx.annotation.NonNull;
4 import androidx.annotation.Nullable;
8 import java.io.Serializable;
9 import java.util.List;
10 
11 /**
12  * 云端消息查询相关参数
13  * <p>
14  * 该类用于设置云端消息查询的各种参数,包括会话ID、消息类型、时间范围、查询条数、锚点消息和查询方向等。
15  * 通过使用内部的Builder类,可以方便地构建查询选项。
16  */
17 public class V2NIMCloudMessageListOption implements Serializable {
18 
19  /**
20  * 消息所属会话ID
21  */
22  private final String conversationId;
23  /**
24  * 根据消息类型查询会话, 为null或空列表, 则表示查询所有消息类型
25  */
26  private final List<V2NIMMessageType> messageTypes;
27 
28  /**
29  * 消息查询开始时间,小于等于 endTime
30  * 默认从0开始查询,闭区间
31  */
32  private final long beginTime;
33 
34  /**
35  * 消息查询结束时间
36  * 默认当前时间,闭区间
37  */
38  private final long endTime;
39 
40  /**
41  * 每次查询条数,默认50,最大值100
42  */
43  private final int limit;
44 
45  /**
46  * 锚点消息,根据锚点消息查询,不包含该消息
47  * 如果anchor为空, 则以beginTime, endTime为查询范围, 闭区间
48  */
49  private final V2NIMMessage anchorMessage;
50 
51  /**
52  * 消息查询方向,如果其它参数都不填
53  * DESC: 按时间从大到小查询
54  * ASC:按时间从小到大查询
55  */
56  private final V2NIMMessageQueryDirection direction;
57 
58  /**
59  * 私有构造函数,使用默认值初始化
60  */
61  private V2NIMCloudMessageListOption() {
62  this("", null, DEFAULT_BEGIN_TIME, DEFAULT_END_TIME, DEFAULT_LIMIT, null, DEFAULT_DIRECTION);
63  }
64 
65  /**
66  * 私有构造函数,用于创建V2NIMCloudMessageListOption实例
67  *
68  * @param conversationId 会话ID
69  * @param messageTypes 消息类型列表
70  * @param beginTime 开始时间
71  * @param endTime 结束时间
72  * @param limit 查询条数限制
73  * @param anchorMessage 锚点消息
74  * @param direction 查询方向
75  */
76  private V2NIMCloudMessageListOption(@NonNull String conversationId, @Nullable List<V2NIMMessageType> messageTypes, long beginTime, long endTime,
77  int limit, @Nullable V2NIMMessage anchorMessage, @Nullable V2NIMMessageQueryDirection direction) {
78  this.conversationId = conversationId;
79  this.messageTypes = messageTypes;
80  this.beginTime = beginTime;
81  this.endTime = endTime;
82  this.limit = limit;
83  this.anchorMessage = anchorMessage;
84  this.direction = direction == null ? DEFAULT_DIRECTION : direction;
85  }
86 
87 
88  /**
89  * 获取会话ID
90  *
91  * @return 会话ID
92  */
93  public String getConversationId() {
94  return conversationId;
95  }
96 
97  /**
98  * 获取消息类型列表
99  *
100  * @return 消息类型列表
101  */
102  public List<V2NIMMessageType> getMessageTypes() {
103  return messageTypes;
104  }
105 
106  /**
107  * 获取查询开始时间
108  *
109  * @return 查询开始时间
110  */
111  public long getBeginTime() {
112  return beginTime;
113  }
114 
115  /**
116  * 获取查询结束时间
117  *
118  * @return 查询结束时间
119  */
120  public long getEndTime() {
121  return endTime;
122  }
123 
124  /**
125  * 获取查询条数限制
126  *
127  * @return 查询条数限制
128  */
129  public int getLimit() {
130  return limit;
131  }
132 
133  /**
134  * 获取锚点消息
135  *
136  * @return 锚点消息
137  */
139  return anchorMessage;
140  }
141 
142  /**
143  * 获取查询方向
144  *
145  * @return 查询方向,如果为null则返回默认方向
146  */
148  if (direction == null) {
149  return DEFAULT_DIRECTION;
150  }
151  else {
152  return direction;
153  }
154  }
155 
156  /**
157  * 默认开始时间
158  */
159  private static final long DEFAULT_BEGIN_TIME = 0;
160  /**
161  * 默认结束时间
162  */
163  private static final long DEFAULT_END_TIME = 0;
164  /**
165  * 默认查询条数限制
166  */
167  private static final int DEFAULT_LIMIT = 50;
168  /**
169  * 默认查询方向
170  */
172 
173  /**
174  * V2NIMCloudMessageListOption的构建器类
175  */
176  public static final class V2NIMCloudMessageListOptionBuilder {
177 
178  private final String conversationId;
179  private List<V2NIMMessageType> messageTypes;
180  private long beginTime = DEFAULT_BEGIN_TIME;
181  private long endTime = DEFAULT_END_TIME;
182  private int limit = DEFAULT_LIMIT;
183  private V2NIMMessage anchorMessage;
184  private V2NIMMessageQueryDirection direction;
185 
186  /**
187  * 私有构造函数
188  *
189  * @param conversationId 会话ID
190  */
191  private V2NIMCloudMessageListOptionBuilder(final String conversationId) {
192  this.conversationId = conversationId;
193  }
194 
195  /**
196  * 创建构建器实例
197  *
198  * @param conversationId 会话ID
199  *
200  * @return V2NIMCloudMessageListOptionBuilder实例
201  */
202  public static V2NIMCloudMessageListOptionBuilder builder(final String conversationId) {
203  return new V2NIMCloudMessageListOptionBuilder(conversationId);
204  }
205 
206  /**
207  * 设置消息类型列表
208  *
209  * @param messageTypes 消息类型列表
210  *
211  * @return 构建器实例
212  */
213  public V2NIMCloudMessageListOptionBuilder withMessageTypes(List<V2NIMMessageType> messageTypes) {
214  this.messageTypes = messageTypes;
215  return this;
216  }
217 
218  /**
219  * 设置查询开始时间
220  *
221  * @param beginTime 查询开始时间
222  *
223  * @return 构建器实例
224  */
225  public V2NIMCloudMessageListOptionBuilder withBeginTime(long beginTime) {
226  this.beginTime = beginTime;
227  return this;
228  }
229 
230  /**
231  * 设置查询结束时间
232  *
233  * @param endTime 查询结束时间
234  *
235  * @return 构建器实例
236  */
237  public V2NIMCloudMessageListOptionBuilder withEndTime(long endTime) {
238  this.endTime = endTime;
239  return this;
240  }
241 
242  /**
243  * 设置查询条数限制
244  *
245  * @param limit 查询条数限制
246  *
247  * @return 构建器实例
248  */
249  public V2NIMCloudMessageListOptionBuilder withLimit(int limit) {
250  this.limit = limit;
251  return this;
252  }
253 
254  /**
255  * 设置锚点消息
256  *
257  * @param anchorMessage 锚点消息
258  *
259  * @return 构建器实例
260  */
261  public V2NIMCloudMessageListOptionBuilder withAnchorMessage(V2NIMMessage anchorMessage) {
262  this.anchorMessage = anchorMessage;
263  return this;
264  }
265 
266  /**
267  * 设置查询方向
268  *
269  * @param direction 查询方向
270  *
271  * @return 构建器实例
272  */
273  public V2NIMCloudMessageListOptionBuilder withDirection(V2NIMMessageQueryDirection direction) {
274  this.direction = direction;
275  return this;
276  }
277 
278  /**
279  * 构建V2NIMCloudMessageListOption实例
280  *
281  * @return V2NIMCloudMessageListOption实例
282  */
283  public V2NIMCloudMessageListOption build() {
284  return new V2NIMCloudMessageListOption(conversationId, messageTypes, beginTime, endTime, limit, anchorMessage, direction);
285  }
286  }
287 }