I have ChatDayWrappers
which contains all messages in the database between the two parties for one day.
How can I get the the chatRowWrappers with the 47 items from the object chatWrapper.getChatDayWrappers()[0];
?
Code
Object object = chatWrapper.getChatDayWrappers()[0];
getLogger().info("chatWrapper: " + object.getClass().getName());
output
java.util.TreeMap$Entry
Another try:
I have tired the following:
Object object[] = chatWrapper.getChatDayWrappers()[0].entrySet().toArray();
getLogger().info("chatWrapper: " + object.getClass().getName());
but I got this error:
No signature of method: java.util.TreeMap$Entry.entrySet() is applicable for argument types: () values: [] Possible solutions: every(): groovy.lang.MissingMethodException: No signature of method: java.util.TreeMap$Entry.entrySet() is applicable for argument types: () values: []
CockpitChatWrapper
public class CockpitChatWrapper
implements Serializable {
private static final long serialVersionUID = 1L;
public static final String STYLE_CHAT_UNKNOWN = "tpChatUnknown";
public static final String STYLE_CHAT_FROM = "tpChatFrom";
public static final String STYLE_CHAT_TO = "tpChatTo";
/** Used to format the dates in the report. */
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private final Map<String, ChatDisplayDayWrapper> chatDayWrappers;
private final Long currentUserId;
private final Long currentFlexiObjectId;
/**
* Constructor.
*
* @param currentUserId The user Id.
* @param currentFlexiObjectId The flexiObejct Id.
*/
public CockpitChatWrapper(final Long currentUserId, final Long currentFlexiObjectId) {
this.chatDayWrappers = new TreeMap<String, ChatDisplayDayWrapper>(new Comparator<String>() {
public int compare(final String o1, final String o2) {
return (o1.compareTo(o2) * -1);
}
});
this.currentUserId = currentUserId;
this.currentFlexiObjectId = currentFlexiObjectId;
}
/**
*
* Add a chat entry.
*
* @param chat The chat entry.
*/
public void addChatEntry(final Chat chat) {
String key = CockpitChatWrapper.DATE_FORMAT.get().format(chat.getLastWrite());
ChatDisplayDayWrapper entry = null;
if (this.chatDayWrappers.get(key) != null) {
entry = this.chatDayWrappers.get(key);
} else {
entry = new ChatDisplayDayWrapper(chat.getLastWrite());
this.chatDayWrappers.put(key, entry);
}
entry.addChat(chat, this.currentUserId, this.currentFlexiObjectId);
}
/**
* Returns the chatDayWrappers.
*
* @return Returns the chatDayWrappers.
*/
public Object[] getChatDayWrappers() {
return this.chatDayWrappers.entrySet().toArray();
}
public boolean getHasChatDayWrappers() {
return this.chatDayWrappers != null && this.chatDayWrappers.size() > 0;
}
/**
*
* Hold the chat entries for an day.
*
*/
public static class ChatDisplayDayWrapper
implements Serializable {
private static final long serialVersionUID = 1L;
private final Date day;
private final List<ChatDisplayRowWrapper> chatRowWrappers;
/**
*
* Constructor.
*
* @param day The day to set.
*/
public ChatDisplayDayWrapper(final Date day) {
this.day = day;
this.chatRowWrappers = new ArrayList<ChatDisplayRowWrapper>();
}
private void addChat(final Chat chat, final Long currentUserId, final Long currentFlexiObjectId) {
String styleClass = CockpitChatWrapper.STYLE_CHAT_UNKNOWN;
if (currentUserId != null && chat.getFromUser() != null
&& currentUserId.longValue() == chat.getFromUser().getId().longValue()) {
styleClass = CockpitChatWrapper.STYLE_CHAT_FROM;
} else if (currentFlexiObjectId != null && chat.getFromFlexiObject() != null
&& currentFlexiObjectId.longValue() == chat.getFromFlexiObject().getId().longValue()) {
styleClass = CockpitChatWrapper.STYLE_CHAT_TO;
}
this.chatRowWrappers.add(new ChatDisplayRowWrapper(chat, styleClass));
}
/**
* Returns the day.
*
* @return Returns the day.
*/
public Date getDay() {
return this.day;
}
/**
* Returns the chatRowWrappers.
*
* @return Returns the chatRowWrappers.
*/
public List<ChatDisplayRowWrapper> getChatRowWrappers() {
return this.chatRowWrappers;
}
}
}
screenshot
The issue here is:
public Object[] getChatDayWrappers() {
return this.chatDayWrappers.entrySet().toArray();
}
returns an Object array that you cannot directly access the ChatDisplayDayWrapper object inside the map. Instead, you could modify your getter to:
public List<ChatDisplayRowWrapper> getChatDayWrappers() {
return m.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList()); }
}
Your function Object[] getChatDayWrappers
:
this.chatDayWrappers.entrySet().toArray()
returns an array of Map$Entry
. Eg:
groovy:000> [1:1].entrySet().toArray().first().getClass()
===> class java.util.LinkedHashMap$Entry
An Entry
is not a map, it's just an entry. An entry holds key
and value
(but not entrySet()
; this is where your code fails). Eg
groovy:000> [1:1].entrySet().toArray().first().key
===> 1
groovy:000> [1:1].entrySet().toArray().first().value
===> 1
groovy:000> [1:1].entrySet().toArray().first().entrySet()
ERROR groovy.lang.MissingMethodException:
No signature of method: java.util.LinkedHashMap$Entry.entrySet() is applicable for argument types: () values: []
Possible solutions: every()
So to get the display wrapper you need to call value
in Groovy, or since your code looks more like Java anyway .getValue()
to get the ChatDisplayDayWrapper
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.