ProcessStatus { public static ProcessStatus aggregate(ProcessStatus... statuses) { assertStatusesNotNull(statuses); if (statuses.length == 0) { return UNKNOWN; } else if (statuses.length == 1) { return statuses[0]; } float averageProgress = getAverageProgress(statuses); for (ProcessStatus status : statuses) { if (status.getState() == ProcessState.CANCELLED) { return new ProcessStatus(status.getState(), averageProgress, status.getMessage()); } } for (ProcessStatus status : statuses) { if (status.getState() == ProcessState.ERROR) { return new ProcessStatus(status.getState(), averageProgress, status.getMessage()); } } String message = ""; for (ProcessStatus status : statuses) { message = status.getMessage(); if (!message.isEmpty()) { break; } } final ProcessState state = getCummulativeState(statuses); return new ProcessStatus(state, averageProgress, message); } ProcessStatus(ProcessState state);  ProcessStatus(ProcessState state, float progress);  ProcessStatus(ProcessState state, float progress, String message); static ProcessStatus aggregate(ProcessStatus... statuses); ProcessState getState(); boolean isDone(); String getMessage(); float getProgress(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static ProcessStatus aggregateUnsustainable(ProcessStatus... statuses); final static ProcessStatus UNKNOWN; final static ProcessStatus SCHEDULED; }
@Test public void testAggregate() { assertEquals(ProcessStatus.UNKNOWN, ProcessStatus.aggregate()); assertEquals(new ProcessStatus(ProcessState.RUNNING, 0.4f, "Hello!"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.RUNNING, 0.4f, "Hello!"))); assertEquals(new ProcessStatus(ProcessState.UNKNOWN, 0.0f, "Starting"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.UNKNOWN, 0.0f, "Starting"), new ProcessStatus(ProcessState.UNKNOWN, 0.0f, ""), new ProcessStatus(ProcessState.UNKNOWN, 0.0f, ""))); assertEquals(new ProcessStatus(ProcessState.SCHEDULED, 0.0f, "Waiting"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.SCHEDULED, 0.0f, "Waiting"), new ProcessStatus(ProcessState.UNKNOWN, 0.0f, ""), new ProcessStatus(ProcessState.UNKNOWN, 0.0f, ""))); assertEquals(new ProcessStatus(ProcessState.RUNNING, 0.3f, ""), ProcessStatus.aggregate(new ProcessStatus(ProcessState.RUNNING, 0.2f, ""), new ProcessStatus(ProcessState.RUNNING, 0.4f, ""), new ProcessStatus(ProcessState.RUNNING, 0.3f, ""))); assertEquals(new ProcessStatus(ProcessState.RUNNING, 0.8f, "Wait"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.RUNNING, 0.4f, "Wait"), new ProcessStatus(ProcessState.COMPLETED, 1.0f, ""), new ProcessStatus(ProcessState.COMPLETED, 1.0f, ""))); assertEquals(new ProcessStatus(ProcessState.COMPLETED, 1.0f, "This was hard"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.COMPLETED, 1.0f, ""), new ProcessStatus(ProcessState.COMPLETED, 1.0f, "This was hard"), new ProcessStatus(ProcessState.COMPLETED, 1.0f, ""))); assertEquals(new ProcessStatus(ProcessState.ERROR, 0.8f, "I/O problem"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.COMPLETED, 1.0f, ""), new ProcessStatus(ProcessState.ERROR, 0.4f, "I/O problem"), new ProcessStatus(ProcessState.COMPLETED, 1.0f, ""))); assertEquals(new ProcessStatus(ProcessState.CANCELLED, 0.3f, "Go away"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.RUNNING, 0.2f, ""), new ProcessStatus(ProcessState.RUNNING, 0.6f, ""), new ProcessStatus(ProcessState.CANCELLED, 0.1f, "Go away"))); assertEquals(new ProcessStatus(ProcessState.CANCELLED, 0.3f, "Go away"), ProcessStatus.aggregate(new ProcessStatus(ProcessState.RUNNING, 0.2f, ""), new ProcessStatus(ProcessState.ERROR, 0.6f, ""), new ProcessStatus(ProcessState.CANCELLED, 0.1f, "Go away"))); try { ProcessStatus.aggregate(ProcessStatus.UNKNOWN, ProcessStatus.UNKNOWN, null); fail("NPE expected"); } catch (NullPointerException e) { } }