Jonathan Hart
Committed by Brian O'Connor

Return null instead of throw exception for invalid event states.

Change-Id: Ie40ca4dc1c241aa4f27652aa4e8b3f99eb924169
......@@ -72,10 +72,26 @@ public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
super(type, intent);
}
/**
* Creates an IntentEvent based on the state contained in the given intent
* data. Some states are not sent as external events, and these states will
* return null events.
*
* @param data the intent data to create an event for
* @return new intent event if the state is valid, otherwise null.
*/
public static IntentEvent getEvent(IntentData data) {
return getEvent(data.state(), data.intent());
}
/**
* Creates an IntentEvent based on the given state and intent. Some states
* are not sent as external events, and these states will return null events.
*
* @param state new state of the intent
* @param intent intent to put in event
* @return new intent event if the state is valid, otherwise null.
*/
public static IntentEvent getEvent(IntentState state, Intent intent) {
Type type;
switch (state) {
......@@ -95,14 +111,13 @@ public class IntentEvent extends AbstractEvent<IntentEvent.Type, Intent> {
type = Type.FAILED;
break;
//fallthrough to default from here
// fallthrough to default from here
case COMPILING:
case INSTALLING:
case RECOMPILING:
case WITHDRAWING:
default:
throw new IllegalArgumentException(
"Intent event cannot have transient state: " + state);
return null;
}
return new IntentEvent(type, intent);
}
......
......@@ -268,9 +268,8 @@ public class GossipIntentStore
@Override
public void addPending(IntentData data) {
log.debug("new call to pending {}", data);
log.debug("new pending {} {} {}", data.key(), data.state(), data.version());
if (data.version() == null) {
log.debug("updating timestamp");
data.setVersion(new SystemClockTimestamp());
}
pending.put(data.key(), copyData(data));
......@@ -293,16 +292,9 @@ public class GossipIntentStore
public void event(
EventuallyConsistentMapEvent<Key, IntentData> event) {
if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
IntentEvent externalEvent;
IntentData intentData = event.value();
try {
externalEvent = IntentEvent.getEvent(intentData.state(), intentData.intent());
} catch (IllegalArgumentException e) {
externalEvent = null;
}
notifyDelegateIfNotNull(externalEvent);
notifyDelegateIfNotNull(IntentEvent.getEvent(intentData));
}
}
}
......@@ -322,12 +314,7 @@ public class GossipIntentStore
}
}
try {
notifyDelegate(IntentEvent.getEvent(event.value()));
} catch (IllegalArgumentException e) {
//no-op
log.trace("ignore this exception: {}", e);
}
notifyDelegateIfNotNull(IntentEvent.getEvent(event.value()));
}
}
}
......
......@@ -198,14 +198,15 @@ public class SimpleIntentStore
pending.remove(newData.key());
}
try {
notifyDelegate(IntentEvent.getEvent(newData));
} catch (IllegalArgumentException e) {
//no-op
log.trace("ignore this exception: {}", e);
notifyDelegateIfNotNull(IntentEvent.getEvent(newData));
}
}
}
private void notifyDelegateIfNotNull(IntentEvent event) {
if (event != null) {
notifyDelegate(event);
}
}
@Override
......@@ -241,7 +242,7 @@ public class SimpleIntentStore
pending.put(data.key(), data);
checkNotNull(delegate, "Store delegate is not set")
.process(data);
notifyDelegate(IntentEvent.getEvent(data));
notifyDelegateIfNotNull(IntentEvent.getEvent(data));
} else {
log.debug("IntentData {} is older than existing: {}",
data, existingData);
......