summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/commons/src/main/java/org/onap/sdc/dcae/catalog/commons/Futures.java
blob: a40593204c020c85be38c0dfc7140600fd886de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package org.onap.sdc.dcae.catalog.commons;

import java.util.List;
import java.util.LinkedList;
import java.util.Collections;

import java.util.concurrent.CountDownLatch;
import java.util.function.Function;

import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.Enums.LogLevel;


/**
 */
public class Futures<T> {

	private Futures() {
	}


	public static <T> Future<T>	failedFuture(Throwable theError) {
		return new BasicFuture<T>()
							.cause(theError);
	}

	public static <T> Future<T>	succeededFuture(T theResult) {
		return new BasicFuture<T>()
							.result(theResult);
	}
	
	public static <T> Future<T>	future() {
		return new BasicFuture<T>();
	}
	
	public static <U,V> Future<V> advance(Future<U> theStep,
																				final Function<U,V> theResultFunction) {
		return advance(theStep, theResultFunction, Function.identity());
	}

	public static <U,V> Future<V> advance(Future<U> theStep,
																				final Function<U,V> theResultFunction,
																				final Function<Throwable, Throwable> theErrorFunction) {
		final Future<V> adv = new BasicFuture<V>();
		theStep.setHandler(new FutureHandler<U>() {
															public void handle(Future<U> theResult) {
																if (theResult.failed())
																	adv.cause(theErrorFunction.apply(theResult.cause()));
																else
																	adv.result(theResultFunction.apply(theResult.result()));			
															}
													}); 
		return adv;
	}
	
	/** */
	public static class BasicFuture<T> implements Future<T> {

		protected boolean 		 succeeded,
													 failed;

		protected FutureHandler<T> handler;
		protected Throwable		 		cause;
		protected T						 		result;


		protected BasicFuture() {
		}

		public T result() {
			return this.result;
		}

		public Future<T> result(T theResult) {
			this.result = theResult;
			this.succeeded = true;
			this.cause = null;
			this.failed = false;
			callHandler();
			return this;
		}
	
		public Throwable cause() {
			return this.cause;
		}
		
		public Future<T> cause(Throwable theCause) {
			this.cause = theCause;
			this.failed = true;
			this.result = null;
			this.succeeded = false;
			callHandler();
			return this;
		}
	
		public boolean succeeded() {
			return this.succeeded;
		}

		public boolean failed() {
			return this.failed;
		}

		public boolean complete() {
			return this.failed || this.succeeded;
		}
 		
		public Future<T> setHandler(FutureHandler<T> theHandler) {
			this.handler = theHandler;
			callHandler();
			return this;
		}

		public T waitForResult() throws Exception {
			BasicHandler<T> hnd = buildHandler();
			setHandler(hnd);
			hnd.waitForCompletion();
			if (failed())
				throw (Exception)cause();
			else
				return result();
		}
	
		public Future<T> waitForCompletion() throws InterruptedException {
			BasicHandler<T> hnd = buildHandler();
			setHandler(hnd);
			hnd.waitForCompletion();
			return this;
		}
	
		protected void callHandler() {
			if (this.handler != null && complete()) {
				this.handler.handle(this);
			}
		}

		protected BasicHandler<T> buildHandler() {
			return new BasicHandler<T>();
		}
	}


	/** */
	public static class BasicHandler<T> 
												implements FutureHandler<T> {
		
		protected T 							result = null;
		protected Throwable				error = null;
		protected CountDownLatch 	latch = null;

		BasicHandler() {
			this(new CountDownLatch(1));
		}

		BasicHandler(CountDownLatch theLatch) {
			this.latch = theLatch;
		}

		public void handle(Future<T> theResult) {
			process(theResult);
			if (this.latch != null) {
				this.latch.countDown();
			}
		}

		protected void process(Future<T> theResult) {
			if (theResult.failed()) {
				this.error = theResult.cause();
			}
			else {
				this.result = theResult.result();
			}
		}

		public T result(boolean doWait)
															throws InterruptedException, RuntimeException {
			if (doWait) {
				waitForCompletion();
			}
			if (null == this.error)
				return this.result;

			throw new RuntimeException(this.error);
		}
		
		public T result()
															throws InterruptedException, RuntimeException {
			return result(true);
		}

		public BasicHandler<T> waitForCompletion() throws InterruptedException {
			this.latch.await();
			return this;
		}
	}

	public static class Accumulator<T>	extends BasicFuture<List<T>>		
																				implements Future<List<T>> {

		protected List<Future<T>> futures = new LinkedList<Future<T>>();
		protected BasicHandler<T> accumulatorHandler = null;

		private static OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

		public Accumulator() {
			this.result = new LinkedList<T>();
		}

		public Accumulator<T> add(Future<T> theFuture) {
			debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Intersection add");
			this.futures.add(theFuture);
			this.result.add(null);
			return this;
		}

		public Accumulator<T> addAll(Accumulator<T> theFutures) {

			debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Intersection addAll");

			return this;
		}

		public Future<List<T>> accumulate() {
			this.futures = Collections.unmodifiableList(this.futures);
			this.accumulatorHandler = new BasicHandler<T>(new CountDownLatch(this.futures.size())) {
												protected void process(Future<T> theResult) {
													if (theResult.failed()) {
														Accumulator.this.cause = theResult.cause();
													}
													else {
														Accumulator.this.result.set(
															Accumulator.this.futures.indexOf(theResult), theResult.result());
													}
													if (this.latch.getCount() == 1) {
														if (Accumulator.this.cause != null)
															Accumulator.this.cause(Accumulator.this.cause);
														else
															Accumulator.this.result(Accumulator.this.result);
													}
												}
										 };
			futures.stream()
							.forEach(f -> f.setHandler(this.accumulatorHandler));

			return this;
		}

	}


}