]> git.basschouten.com Git - openhab-addons.git/blob
a1d36c4d9597cd69cd1040a7e128e646d932a911
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.astro.internal.job;
14
15 import java.util.List;
16 import java.util.stream.Collectors;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * {@link CompositeJob} comprises multiple {@link Job}s to be executed in order
22  *
23  * @author Markus Rathgeb - Initial contribution
24  * @author Amit Kumar Mondal - Minor modifications
25  */
26 @NonNullByDefault
27 public final class CompositeJob extends AbstractJob {
28
29     private final List<Job> jobs;
30
31     /**
32      * Constructor
33      *
34      * @param thingUID thing UID
35      * @param jobs the jobs to execute
36      * @throws IllegalArgumentException
37      *             if {@code jobs} is {@code null} or empty
38      */
39     public CompositeJob(String thingUID, List<Job> jobs) {
40         super(thingUID);
41
42         this.jobs = jobs;
43
44         boolean notMatched = jobs.stream().anyMatch(j -> !j.getThingUID().equals(thingUID));
45         checkArgument(!notMatched, "The jobs must associate the same thing UID");
46     }
47
48     @Override
49     public void run() {
50         jobs.forEach(j -> {
51             try {
52                 j.run();
53             } catch (RuntimeException ex) {
54                 LOGGER.warn("Job execution failed.", ex);
55             }
56         });
57     }
58
59     @Override
60     public String toString() {
61         return jobs.stream().map(j -> j.toString()).collect(Collectors.joining(" + "));
62     }
63 }