/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.addthis.hydra.job.alert;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.addthis.hydra.job.alert.types.OnErrorJobAlert;
import com.addthis.hydra.job.alert.types.RekickTimeoutJobAlert;
import com.addthis.hydra.job.alert.types.RuntimeExceededJobAlert;
/**
* BasicAlerts is used to choose the alerts that were auto generated (with the correct type) from
* a larger Collection of alerts. Any or all contained alerts may be null. If all alerts are null,
* {@link #isEmpty()} will return true. {@link #iterator()} allows easy iteration through all non-null
* contained alerts.
*/
public final class BasicAlerts implements Iterable<AbstractJobAlert> {
private OnErrorJobAlert errorAlert = null;
private RekickTimeoutJobAlert rekickAlert = null;
private RuntimeExceededJobAlert runtimeAlert = null;
private final List<AbstractJobAlert> alertList;
private BasicAlerts() {
this.alertList = new ArrayList<>(6);
}
@Nullable public OnErrorJobAlert getErrorAlert() {
return this.errorAlert;
}
@Nullable public RekickTimeoutJobAlert getRekickAlert() {
return this.rekickAlert;
}
@Nullable public RuntimeExceededJobAlert getRuntimeAlert() {
return this.runtimeAlert;
}
public static BasicAlerts create(@Nonnull Collection<AbstractJobAlert> alerts, AutoGenerated autoGenerated) {
BasicAlerts basicAlerts = new BasicAlerts();
alerts.forEach(alert -> {
if (alert.autoGenerated == autoGenerated) {
basicAlerts.alertList.add(alert);
if (alert instanceof OnErrorJobAlert) {
basicAlerts.errorAlert = (OnErrorJobAlert) alert;
} else if (alert instanceof RekickTimeoutJobAlert) {
basicAlerts.rekickAlert = (RekickTimeoutJobAlert) alert;
} else if (alert instanceof RuntimeExceededJobAlert) {
basicAlerts.runtimeAlert = (RuntimeExceededJobAlert) alert;
}
}
});
return basicAlerts;
}
@Override public Iterator<AbstractJobAlert> iterator() {
return alertList.iterator();
}
public boolean isEmpty() {
return alertList.isEmpty();
}
}