java 通配符的使用,如 private List<? extends BaseEntity> messageList;
1、定义统配类
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
}
2、定义实现统配类的基础类
@Data
@ApiModel("聊天记录窗口版本")
public class ImSessionMessageWinDTO extends BaseEntity implements Cloneable {
@ApiModelProperty("编号")
private Long id;
@ApiModelProperty("创建时间")
private String time;
@ApiModelProperty("是否已读")
private String status;
@ApiModelProperty("通信内容")
private String data;
@ApiModelProperty("是否我发送")
private String type;
@ApiModelProperty("用户名称")
private String userName;
@ApiModelProperty("用户头像")
private String userHead;
}
3、定义实体类
@Data
@ApiModel("会话")
public class ImSessionListDTO extends BaseDTO {
@ApiModelProperty("会话编号")
private Long id;
@ApiModelProperty("会话类型")
private String type;
@ApiModelProperty("用户编号")
private Long user;
@ApiModelProperty("用户显示名称")
private String userName;
@ApiModelProperty("用户类型")
private String userType;
@ApiModelProperty("用户头像")
private String userHead;
@ApiModelProperty("会话创建时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@ApiModelProperty("会话状态,0=创建未接入,1=创建已接入,2=已结束")
private String status;
@ApiModelProperty("未读消息条数")
private Integer userUnread;
@ApiModelProperty("最后一条消息时间")
private Date lastTime;
@ApiModelProperty("是否在线")
private int online;
@ApiModelProperty("消息列表")
//以下为统配符定义
private List<? extends BaseEntity> messageList;
@ApiModelProperty("当前分页编号")
private int currentNo;
@ApiModelProperty("等待时间")
private String waitTime;
@ApiModelProperty(value = "最新消息内容")
private String lastMessageContent;
@ApiModelProperty(value = "用户电话")
private String customerPhone;
@ApiModelProperty("接受会话时间")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date accessTime;
}
4、controller实现应用
private BaseResponse<ImSessionListDTO> parseUnreadList(ImSession imSession, User loginUser) {
try {
ImSessionListDTO imSessionListDTO = covert(imSession, loginUser);
User toUser = null;
if (imSession.getPisToUser().equals(loginUser.getId())) {
toUser = getUserByType(TerminalType.valueOf(imSession.getPisUserType()), imSession.getPisUser());
} else {
toUser = getUserByType(TerminalType.valueOf(imSession.getPisToUserType()), imSession.getPisToUser());
}
List<ImSessionMessage> messageList = service.findUnreadMessageList(imSession.getPisId(), imSession.getPisTableIndex());
List<ImSessionMessageWinDTO> list = new ArrayList<>(messageList.size());
for (ImSessionMessage item : messageList) {
ImSessionMessageWinDTO dto = null;
if (item.getPismFromUser().equals(loginUser.getId())) {
dto = new ImSessionMessageWinDTO(item, loginUser.getId(), loginUser.getDisplayName(), loginUser.getUserHead());
} else {
dto = new ImSessionMessageWinDTO(item, loginUser.getId(), toUser.getDisplayName(), toUser.getUserHead());
}
list.add(dto);
}
// 该位置为统配符使用
imSessionListDTO.setMessageList(list);
return new BaseResponse<ImSessionListDTO>(new BaseResult(BaseResultCode.SUCCESS, "获取会话记录成功"), imSessionListDTO);
} catch (Exception e) {
log.error("获取会话记录报错", e);
return new BaseResponse<ImSessionListDTO>(new BaseResult(BaseResultCode.FAULT, e.getMessage()), null);
}
}
5、总结
private List<? extends BaseEntity> messageList; 定义的统配符意思是该list中泛型可以添加继承过BaseEntity类的所有类,所以只要继承过BaseEntity类就可以作为list中的泛型。