I have a really strange problem, the problem is that I am iterating over the list inside the object inside the list, and there is a condition for it, the problem is that the second iteration has a condition for setting the value, but applying it to all elements even if the condition is false .
I have the following classes:
EventFeedKey (used as a key for hashmap):
public class EventFeedKey {
private final int eventId;
private final int triggerId;
public EventFeedKey(int eventId, int triggerId) {
this.eventId = eventId;
this.triggerId = triggerId;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof EventFeedKey)) {
return false;
}
EventFeedKey otherKey = (EventFeedKey) object;
return this.eventId == otherKey.eventId && this.triggerId == otherKey.triggerId;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + Integer.valueOf(this.eventId).hashCode();
result = 31 * result + Integer.valueOf(this.triggerId).hashCode();
return result;
}
}
Event:
@Entity
@Table(name = "events")
public class Event {
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false)
private String title;
@ManyToOne(optional = false)
@JoinColumn(name = "type_id")
private EventType type;
}
EventType:
@Entity
@Table(name = "event_types")
public class EventType {
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false)
private String title;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "eventType", cascade = CascadeType.ALL)
@JsonManagedReference
private List<EventTypeTrigger> triggers = new ArrayList<>();
}
EventTypeTrigger:
@Entity
@Table(name = "event_type_triggers")
public class EventTypeTrigger {
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false)
private String title;
@Transient
private int count;
}
And the following should return the list and triggers populated with count from triggersFeed.
List<Event> eventList = new ArrayList<Event>();
if (eventList != null) {
HashMap<EventFeedKey, Integer> triggersFeed = getTriggersWithCounts();
eventList.stream().forEach(event -> {
event.getType().getTriggers().stream().forEach(eventTypeTrigger -> {
EventFeedKey key = new EventFeedKey(event.getId(), eventTypeTrigger.getId());
eventTypeTrigger.setCount(triggersFeed.get(key) != null ? (Integer) triggersFeed.get(key) : 0);
});
});
return eventList;
}
And inside getTriggersWithCounts():
public HashMap<EventFeedKey, Integer> getTriggersWithCounts() {
HashMap<EventFeedKey, Integer> eventTriggers = new HashMap<>();
eventTriggers.put(new EventFeedKey(1,7), 5);
return eventTriggers;
}
Lets say that I have the following data:
for eventList:
[
{ "id":1, "title":"Team A vs Team X",
"type":{
"id":3,
"title":"Baseball",
"triggers":[
{
"id":7,
"title":"Base Reached",
"count":0
},
{
"id":8,
"title":"Out",
"count":0
}
]
}
},
{
"id":2, "title":"Team A vs Team C",
"type":{
"id":3,
"title":"Baseball",
"triggers":[
{
"id":7,
"title":"Base Reached",
"count":0
},
{
"id":8,
"title":"Out",
"count":0
}
]
}
},
]
The result (which is incorrect) that I get from the method:
[
{ "id":1, "title":"Team A vs Team X",
"type":{
"id":3,
"title":"Baseball",
"triggers":[
{
"id":7,
"title":"Base Reached",
"count":5
},
{
"id":8,
"title":"Out",
"count":0
}
]
}
},
{
"id":2, "title":"Team A vs Team C",
"type":{
"id":3,
"title":"Baseball",
"triggers":[
{
"id":7,
"title":"Base Reached",
"count":5
},
{
"id":8,
"title":"Out",
"count":0
}
]
}
},
]
And the most annoying if I added this:
if (triggersFeed.get(key) != null) {
System.out.print(triggersFeed.get(key).toString());
}
!
, : id:2 ?
Edit:
, Ebean: http://ebean-orm.imtqy.com/, Playframework.