From cc41efa103817cb495360c5356a34fc74292c903 Mon Sep 17 00:00:00 2001 From: Geoffrey De Smet Date: Tue, 27 Oct 2015 11:56:24 +0100 Subject: [PATCH] PLANNER-471 BZ-1273783 Refactor out Map in VariableListenerSupport (cherry picked from commit 8d203779249c5f6bf31119c0784a11f8c443a6e1) --- .../support/VariableListenerNotifiable.java | 10 +++++++ .../support/VariableListenerSupport.java | 29 +++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerNotifiable.java b/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerNotifiable.java index 2d3466517b6..d2766b23a2c 100644 --- a/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerNotifiable.java +++ b/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerNotifiable.java @@ -16,6 +16,9 @@ package org.optaplanner.core.impl.domain.variable.listener.support; +import java.util.LinkedHashSet; +import java.util.Set; + import org.optaplanner.core.impl.domain.variable.listener.VariableListener; public class VariableListenerNotifiable implements Comparable { @@ -23,9 +26,12 @@ public class VariableListenerNotifiable implements Comparable notificationQueue; + public VariableListenerNotifiable(VariableListener variableListener, int globalOrder) { this.variableListener = variableListener; this.globalOrder = globalOrder; + notificationQueue = new LinkedHashSet(); } public VariableListener getVariableListener() { @@ -36,6 +42,10 @@ public int getGlobalOrder() { return globalOrder; } + public Set getNotificationQueue() { + return notificationQueue; + } + @Override public int compareTo(VariableListenerNotifiable other) { int otherGlobalOrder = other.getGlobalOrder(); diff --git a/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerSupport.java b/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerSupport.java index cf5f88a28ff..8741c02c522 100644 --- a/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerSupport.java +++ b/optaplanner-core/src/main/java/org/optaplanner/core/impl/domain/variable/listener/support/VariableListenerSupport.java @@ -17,13 +17,13 @@ package org.optaplanner.core.impl.domain.variable.listener.support; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.SortedMap; import java.util.TreeMap; import org.optaplanner.core.impl.domain.entity.descriptor.EntityDescriptor; @@ -43,23 +43,23 @@ public class VariableListenerSupport implements SupplyManager { protected final InnerScoreDirector scoreDirector; + protected final List notifiableList; protected final Map> sourceVariableToNotifiableMap; protected final Map> sourceEntityToNotifiableMap; protected final Map supplyMap; protected int nextGlobalOrder = 0; - protected SortedMap> notificationQueueMap; protected boolean notificationQueuesAreEmpty; public VariableListenerSupport(InnerScoreDirector scoreDirector) { this.scoreDirector = scoreDirector; + notifiableList = new ArrayList(); sourceVariableToNotifiableMap = new LinkedHashMap>(); sourceEntityToNotifiableMap = new LinkedHashMap>(); supplyMap = new LinkedHashMap(); } public void linkVariableListeners() { - notificationQueueMap = new TreeMap>(); notificationQueuesAreEmpty = true; for (EntityDescriptor entityDescriptor : scoreDirector.getSolutionDescriptor().getEntityDescriptors()) { for (VariableDescriptor variableDescriptor : entityDescriptor.getDeclaredVariableDescriptors()) { @@ -87,11 +87,11 @@ public void linkVariableListeners() { entityNotifiableList.add(notifiable); } } - Set notificationQueue = new LinkedHashSet(); - notificationQueueMap.put(notifiable, notificationQueue); + notifiableList.add(notifiable); } } } + Collections.sort(notifiableList); } public S demand(Demand demand) { @@ -110,8 +110,8 @@ public S demand(Demand demand) { if (!entityNotifiableList.contains(notifiable)) { entityNotifiableList.add(notifiable); } - Set notificationQueue = new LinkedHashSet(); - notificationQueueMap.put(notifiable, notificationQueue); + notifiableList.add(notifiable); + // No need to sort notifiableList again because notifiable's globalOrder is highest } supplyMap.put(demand, supply); } @@ -123,7 +123,7 @@ public S demand(Demand demand) { // ************************************************************************ public void resetWorkingSolution() { - for (VariableListenerNotifiable notifiable : notificationQueueMap.keySet()) { + for (VariableListenerNotifiable notifiable : notifiableList) { VariableListener variableListener = notifiable.getVariableListener(); if (variableListener instanceof StatefulVariableListener) { ((StatefulVariableListener) variableListener).resetWorkingSolution(scoreDirector); @@ -132,7 +132,7 @@ public void resetWorkingSolution() { } public void clearWorkingSolution() { - for (VariableListenerNotifiable notifiable : notificationQueueMap.keySet()) { + for (VariableListenerNotifiable notifiable : notifiableList) { VariableListener variableListener = notifiable.getVariableListener(); if (variableListener instanceof StatefulVariableListener) { ((StatefulVariableListener) variableListener).clearWorkingSolution(scoreDirector); @@ -143,7 +143,7 @@ public void clearWorkingSolution() { public void beforeEntityAdded(EntityDescriptor entityDescriptor, Object entity) { List notifiableList = sourceEntityToNotifiableMap.get(entityDescriptor); for (VariableListenerNotifiable notifiable : notifiableList) { - Set notificationQueue = notificationQueueMap.get(notifiable); + Set notificationQueue = notifiable.getNotificationQueue(); boolean added = notificationQueue.add( new VariableListenerNotification(entity, VariableListenerNotificationType.ENTITY_ADDED)); if (added) { @@ -160,7 +160,7 @@ public void afterEntityAdded(EntityDescriptor entityDescriptor, Object entity) { public void beforeVariableChanged(VariableDescriptor variableDescriptor, Object entity) { List notifiableList = sourceVariableToNotifiableMap.get(variableDescriptor); for (VariableListenerNotifiable notifiable : notifiableList) { - Set notificationQueue = notificationQueueMap.get(notifiable); + Set notificationQueue = notifiable.getNotificationQueue(); boolean added = notificationQueue.add( new VariableListenerNotification(entity, VariableListenerNotificationType.VARIABLE_CHANGED)); if (added) { @@ -177,7 +177,7 @@ public void afterVariableChanged(VariableDescriptor variableDescriptor, Object e public void beforeEntityRemoved(EntityDescriptor entityDescriptor, Object entity) { List notifiableList = sourceEntityToNotifiableMap.get(entityDescriptor); for (VariableListenerNotifiable notifiable : notifiableList) { - Set notificationQueue = notificationQueueMap.get(notifiable); + Set notificationQueue = notifiable.getNotificationQueue(); boolean added = notificationQueue.add( new VariableListenerNotification(entity, VariableListenerNotificationType.ENTITY_REMOVED)); if (added) { @@ -192,9 +192,8 @@ public void afterEntityRemoved(EntityDescriptor entityDescriptor, Object entity) } public void triggerVariableListenersInNotificationQueues() { - for (Map.Entry> entry : notificationQueueMap.entrySet()) { - VariableListenerNotifiable notifiable = entry.getKey(); - Set notificationQueue = entry.getValue(); + for (VariableListenerNotifiable notifiable : notifiableList) { + Set notificationQueue = notifiable.getNotificationQueue(); VariableListener variableListener = notifiable.getVariableListener(); for (Iterator it = notificationQueue.iterator(); it.hasNext(); ) { VariableListenerNotification notification = it.next();