1 /*
2 * #%L
3 * LambdaCallbackAdapter.java - mongodb-async-driver - Allanbank Consulting, Inc.
4 * %%
5 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6 * %%
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
19 */
20 package com.allanbank.mongodb.client;
21
22 import com.allanbank.mongodb.Callback;
23 import com.allanbank.mongodb.LambdaCallback;
24 import com.allanbank.mongodb.StreamCallback;
25
26 /**
27 * LambdaCallbackAdapter provides an adapter for the {@link LambdaCallback} to a
28 * {@link Callback} or {@link StreamCallback}.
29 *
30 * @param <V>
31 * The type of the operation's result.
32 *
33 * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
34 * mutated in incompatible ways between any two releases of the driver.
35 * @copyright 2013, Allanbank Consulting, Inc., All Rights Reserved
36 */
37 public final class LambdaCallbackAdapter<V> implements StreamCallback<V> {
38
39 /** The {@link LambdaCallback} to trigger. */
40 private final LambdaCallback<V> myLambda;
41
42 /**
43 * Creates a new LambdaCallbackAdapter.
44 *
45 * @param lambda
46 * The {@link LambdaCallback} to trigger.
47 */
48 public LambdaCallbackAdapter(final LambdaCallback<V> lambda) {
49 myLambda = lambda;
50 }
51
52 /**
53 * {@inheritDoc}
54 * <p>
55 * Overridden to call {@link LambdaCallback#accept(Throwable, Object)
56 * accept(null, result)} on the wrapped {@link LambdaCallback}.
57 * </p>
58 */
59 @Override
60 public void callback(final V result) {
61 myLambda.accept(null, result);
62 }
63
64 /**
65 * {@inheritDoc}
66 * <p>
67 * Overridden to call {@link LambdaCallback#accept(Throwable, Object)
68 * accept(null, null)} on the wrapped {@link LambdaCallback}.
69 * </p>
70 */
71 @Override
72 public void done() {
73 myLambda.accept(null, null);
74 }
75
76 /**
77 * {@inheritDoc}
78 * <p>
79 * Overridden to call {@link LambdaCallback#accept(Throwable, Object)
80 * accept(thrown, null)} on the wrapped {@link LambdaCallback}.
81 * </p>
82 */
83 @Override
84 public void exception(final Throwable thrown) {
85 myLambda.accept(thrown, null);
86 }
87
88 }