1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.github.dexecutor.executor;
19
20 import java.util.concurrent.ExecutorService;
21
22 import com.github.dexecutor.executor.graph.CyclicValidator;
23 import com.github.dexecutor.executor.graph.LevelOrderTraversar;
24 import com.github.dexecutor.executor.graph.Traversar;
25 import com.github.dexecutor.executor.graph.Validator;
26 import static com.github.dexecutor.executor.support.Preconditions.*;
27
28 /**
29 * <p>Configuration Object for Dexecutor framework. At a minimum it needs {@code ExecutorService} and {@code TaskProvider}, rest are optional and takes default values</p>
30 * <p>This provides way to hook in your own {@code Validator} and {@code Traversar}</p>
31 *
32 * @author Nadeem Mohammad
33 *
34 * @param <T> Type of Node/Task ID
35 * @param <R> Type of Node/Task result
36 */
37 public class DependentTasksExecutorConfig<T extends Comparable<T>, R> {
38 /**
39 * executorService is the main platform on which tasks are executed
40 */
41 private ExecutorService executorService;
42 /**
43 * When it comes to task execution, task provider would be consulted to provide task objects for execution
44 */
45 private TaskProvider<T, R> taskProvider;
46 /**
47 * Validator for validating the consturcted graph, defaults to detecting Cyclic checks
48 */
49 private Validator<T, R> validator = new CyclicValidator<T, R>();
50 /**
51 * Traversar used to traverse the graph while printing it on a Writer
52 */
53 private Traversar<T, R> traversar = new LevelOrderTraversar<T, R>();
54 /**
55 * Construct the object with mandatory params, rest are optional
56 * @param executorService
57 * @param taskProvider
58 */
59 public DependentTasksExecutorConfig(final ExecutorService executorService, final TaskProvider<T, R> taskProvider) {
60 this.executorService = executorService;
61 this.taskProvider = taskProvider;
62 }
63
64 void validate() {
65 checkNotNull(this.executorService, "Executer Service should not be null");
66 checkNotNull(this.taskProvider, "Task Provider should not be null");
67 checkNotNull(this.validator, "Validator should not be null");
68 checkNotNull(this.traversar, "Traversar should not be null");
69 }
70
71 ExecutorService getExecutorService() {
72 return executorService;
73 }
74
75 TaskProvider<T, R> getTaskProvider() {
76 return taskProvider;
77 }
78
79 Validator<T, R> getValidator() {
80 return validator;
81 }
82 /**
83 * change the validator to that of specified
84 * @param validator
85 */
86 public void setValidator(final Validator<T, R> validator) {
87 this.validator = validator;
88 }
89 Traversar<T, R> getTraversar() {
90 return traversar;
91 }
92 /**
93 * Change the traversar to that of specified
94 * @param traversar
95 */
96 public void setTraversar(final Traversar<T, R> traversar) {
97 this.traversar = traversar;
98 }
99 }