1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36
37
38
39
40
41 public class ReplyIntegerCallback extends AbstractReplyCallback<Integer> {
42
43
44 public static final String DEFAULT_NAME = "n";
45
46
47
48
49
50 private final String myName;
51
52
53
54
55
56
57
58 public ReplyIntegerCallback(final Callback<Integer> results) {
59 this(DEFAULT_NAME, results);
60 }
61
62
63
64
65
66
67
68
69
70 public ReplyIntegerCallback(final String name,
71 final Callback<Integer> results) {
72 super(results);
73
74 myName = name;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @Override
88 protected MongoDbException asError(final Reply reply) {
89 MongoDbException error = super.asError(reply);
90 if (error == null) {
91 final List<Document> results = reply.getResults();
92 if (results.size() == 1) {
93 final Document doc = results.get(0);
94 final Element nElem = doc.get(myName);
95 if (toInt(nElem) < 0) {
96 error = new ReplyException(reply, "Missing '" + myName
97 + "' field in reply.");
98 }
99 }
100 }
101 return error;
102 }
103
104
105
106
107
108
109
110
111
112 @Override
113 protected Integer convert(final Reply reply) throws MongoDbException {
114 final List<Document> results = reply.getResults();
115 if (results.size() == 1) {
116 final Document doc = results.get(0);
117 final Element nElem = doc.get(myName);
118 return Integer.valueOf(toInt(nElem));
119 }
120
121 return Integer.valueOf(-1);
122 }
123 }