View Javadoc
1   /*
2    * #%L
3    * SingleDocumentReplyCallback.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  
21  package com.allanbank.mongodb.client.callback;
22  
23  import java.util.List;
24  
25  import com.allanbank.mongodb.Callback;
26  import com.allanbank.mongodb.MongoDbException;
27  import com.allanbank.mongodb.bson.Document;
28  import com.allanbank.mongodb.client.message.Reply;
29  import com.allanbank.mongodb.error.ReplyException;
30  
31  /**
32   * Callback to expect and extract a single document from the 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 class SingleDocumentReplyCallback extends
39          AbstractReplyCallback<Document> {
40  
41      /**
42       * Create a new SingleDocumentReplyCallback.
43       * 
44       * @param results
45       *            The callback to notify of the reply document.
46       */
47      public SingleDocumentReplyCallback(final Callback<Document> results) {
48          super(results);
49      }
50  
51      /**
52       * {@inheritDoc}
53       * <p>
54       * Creates an exception if the {@link Reply} has less than or more than a
55       * single reply document.
56       * </p>
57       * 
58       * @param reply
59       *            The raw reply.
60       * @return The exception created.
61       */
62      @Override
63      protected MongoDbException asError(final Reply reply) {
64          MongoDbException error = super.asError(reply);
65          if (error == null) {
66              final List<Document> results = reply.getResults();
67              if (results.size() != 1) {
68                  error = new ReplyException(reply,
69                          "Should only be a single document in the reply.");
70              }
71          }
72          return error;
73      }
74  
75      /**
76       * {@inheritDoc}
77       * <p>
78       * Overridden to return the reply document.
79       * </p>
80       * 
81       * @see AbstractReplyCallback#convert(Reply)
82       */
83      @Override
84      protected Document convert(final Reply reply) throws MongoDbException {
85          final List<Document> results = reply.getResults();
86          if (results.size() == 1) {
87              return results.get(0);
88          }
89  
90          return null;
91      }
92  }