| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ReplyIntegerCallback |
|
| 2.25;2.25 |
| 1 | /* | |
| 2 | * #%L | |
| 3 | * ReplyIntegerCallback.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.bson.Element; | |
| 29 | import com.allanbank.mongodb.bson.NumericElement; | |
| 30 | import com.allanbank.mongodb.client.message.Reply; | |
| 31 | import com.allanbank.mongodb.error.ReplyException; | |
| 32 | ||
| 33 | /** | |
| 34 | * Callback to expect and extract a single document from the reply and then | |
| 35 | * extract a contained {@link NumericElement} and coerce it to a integer value. | |
| 36 | * | |
| 37 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
| 38 | * mutated in incompatible ways between any two releases of the driver. | |
| 39 | * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved | |
| 40 | */ | |
| 41 | 27 | public class ReplyIntegerCallback extends AbstractReplyCallback<Integer> { |
| 42 | ||
| 43 | /** The default name for the long value to return. */ | |
| 44 | public static final String DEFAULT_NAME = "n"; | |
| 45 | ||
| 46 | /** | |
| 47 | * The name of the {@link NumericElement value} expected in the reply | |
| 48 | * document. | |
| 49 | */ | |
| 50 | private final String myName; | |
| 51 | ||
| 52 | /** | |
| 53 | * Create a new ReplyIntegerCallback. | |
| 54 | * | |
| 55 | * @param results | |
| 56 | * The callback to notify of the 'n' value. | |
| 57 | */ | |
| 58 | public ReplyIntegerCallback(final Callback<Integer> results) { | |
| 59 | 44 | this(DEFAULT_NAME, results); |
| 60 | 44 | } |
| 61 | ||
| 62 | /** | |
| 63 | * Create a new ReplyIntegerCallback. | |
| 64 | * | |
| 65 | * @param name | |
| 66 | * The name of the {@link NumericElement numeric} value. | |
| 67 | * @param results | |
| 68 | * The callback to notify of the 'n' value. | |
| 69 | */ | |
| 70 | public ReplyIntegerCallback(final String name, | |
| 71 | final Callback<Integer> results) { | |
| 72 | 48 | super(results); |
| 73 | ||
| 74 | 48 | myName = name; |
| 75 | 48 | } |
| 76 | ||
| 77 | /** | |
| 78 | * {@inheritDoc} | |
| 79 | * <p> | |
| 80 | * Creates an exception from the {@link Reply} if the 'n' field is missing. | |
| 81 | * </p> | |
| 82 | * | |
| 83 | * @param reply | |
| 84 | * The raw reply. | |
| 85 | * @return The exception created. | |
| 86 | */ | |
| 87 | @Override | |
| 88 | protected MongoDbException asError(final Reply reply) { | |
| 89 | 31 | MongoDbException error = super.asError(reply); |
| 90 | 31 | if (error == null) { |
| 91 | 30 | final List<Document> results = reply.getResults(); |
| 92 | 30 | if (results.size() == 1) { |
| 93 | 29 | final Document doc = results.get(0); |
| 94 | 29 | final Element nElem = doc.get(myName); |
| 95 | 29 | if (toInt(nElem) < 0) { |
| 96 | 1 | error = new ReplyException(reply, "Missing '" + myName |
| 97 | + "' field in reply."); | |
| 98 | } | |
| 99 | } | |
| 100 | } | |
| 101 | 31 | return error; |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * {@inheritDoc} | |
| 106 | * <p> | |
| 107 | * Overridden to return the 'n' field in the reply document. | |
| 108 | * </p> | |
| 109 | * | |
| 110 | * @see AbstractReplyCallback#convert(Reply) | |
| 111 | */ | |
| 112 | @Override | |
| 113 | protected Integer convert(final Reply reply) throws MongoDbException { | |
| 114 | 29 | final List<Document> results = reply.getResults(); |
| 115 | 29 | if (results.size() == 1) { |
| 116 | 28 | final Document doc = results.get(0); |
| 117 | 28 | final Element nElem = doc.get(myName); |
| 118 | 28 | return Integer.valueOf(toInt(nElem)); |
| 119 | } | |
| 120 | ||
| 121 | 1 | return Integer.valueOf(-1); |
| 122 | } | |
| 123 | } |