View Javadoc
1   /*
2    * #%L
3    * AbstractReplyCallback.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.callback;
21  
22  import com.allanbank.mongodb.Callback;
23  import com.allanbank.mongodb.MongoDbException;
24  import com.allanbank.mongodb.client.FutureCallback;
25  import com.allanbank.mongodb.client.message.Reply;
26  
27  /**
28   * Helper class for constructing callbacks that convert a {@link Reply} message
29   * into a different type of result.
30   * 
31   * @param <F>
32   *            The type for the converted {@link Reply}.
33   * 
34   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
35   *         mutated in incompatible ways between any two releases of the driver.
36   * @copyright 2011-2014, Allanbank Consulting, Inc., All Rights Reserved
37   */
38  public abstract class AbstractReplyCallback<F> extends
39          AbstractValidatingReplyCallback {
40  
41      /** The callback for the converted type. */
42      final Callback<F> myForwardCallback;
43  
44      /**
45       * Create a new AbstractReplyCallback.
46       * 
47       * @param forwardCallback
48       *            The callback for the converted type.
49       */
50      public AbstractReplyCallback(final Callback<F> forwardCallback) {
51          myForwardCallback = forwardCallback;
52      }
53  
54      /**
55       * {@inheritDoc}
56       * <p>
57       * Overridden to forward the exception to the {@link #myForwardCallback}.
58       * </p>
59       * 
60       * @see Callback#exception
61       */
62      @Override
63      public void exception(final Throwable thrown) {
64          getForwardCallback().exception(thrown);
65      }
66  
67      /**
68       * Returns the forwardCallback value.
69       * 
70       * @return the forwardCallback
71       */
72      public Callback<F> getForwardCallback() {
73          return myForwardCallback;
74      }
75  
76      /**
77       * {@inheritDoc}
78       * <p>
79       * Overridden to return true if the {@link #getForwardCallback()
80       * forwardCallback} is a {@link FutureCallback} or to forward the call if
81       * the {@code forwardCallback} is a {@link ReplyCallback}.
82       * </p>
83       */
84      @Override
85      public boolean isLightWeight() {
86          return (myForwardCallback instanceof FutureCallback)
87                  || ((myForwardCallback instanceof ReplyCallback) && ((ReplyCallback) myForwardCallback)
88                          .isLightWeight());
89      }
90  
91      /**
92       * Converts the {@link Reply} into the final response type.
93       * 
94       * @param reply
95       *            The reply to convert.
96       * @return The converted reply.
97       * @throws MongoDbException
98       *             On a failure converting the reply. Generally, the
99       *             {@link #verify(Reply)} method should be used to report
100      *             errors.
101      */
102     protected abstract F convert(Reply reply) throws MongoDbException;
103 
104     /**
105      * {@inheritDoc}
106      * <p>
107      * Overriden to {@link #convert(Reply) convert} and then pass the converted
108      * reply to the {@link #getForwardCallback() forward callback}.
109      * </p>
110      */
111     @Override
112     protected void handle(final Reply reply) {
113         getForwardCallback().callback(convert(reply));
114 
115     }
116 }