| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PendingMessage |
|
| 1.1818181818181819;1.182 |
| 1 | /* | |
| 2 | * #%L | |
| 3 | * PendingMessage.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.message; | |
| 21 | ||
| 22 | import com.allanbank.mongodb.client.Message; | |
| 23 | import com.allanbank.mongodb.client.callback.ReplyCallback; | |
| 24 | ||
| 25 | /** | |
| 26 | * Container for a pending message. Before the message is sent the message id | |
| 27 | * will be zero. After it will contain the assigned message id for the | |
| 28 | * connection. | |
| 29 | * | |
| 30 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
| 31 | * mutated in incompatible ways between any two releases of the driver. | |
| 32 | * @copyright 2011-2014, Allanbank Consulting, Inc., All Rights Reserved | |
| 33 | */ | |
| 34 | public class PendingMessage { | |
| 35 | ||
| 36 | /** The message sent. */ | |
| 37 | private Message myMessage; | |
| 38 | ||
| 39 | /** The message id assigned to the sent message. */ | |
| 40 | private int myMessageId; | |
| 41 | ||
| 42 | /** The callback for the reply to the message. */ | |
| 43 | private ReplyCallback myReplyCallback; | |
| 44 | ||
| 45 | /** The timestamp of the message. */ | |
| 46 | private long myTimestamp; | |
| 47 | ||
| 48 | /** | |
| 49 | * Create a new PendingMessage. | |
| 50 | */ | |
| 51 | public PendingMessage() { | |
| 52 | 30477 | this(0, null, null); |
| 53 | 30478 | } |
| 54 | ||
| 55 | /** | |
| 56 | * Create a new PendingMessage. | |
| 57 | * | |
| 58 | * @param messageId | |
| 59 | * The id assigned to the message. | |
| 60 | * @param message | |
| 61 | * The sent message. | |
| 62 | */ | |
| 63 | public PendingMessage(final int messageId, final Message message) { | |
| 64 | 2363893 | this(messageId, message, null); |
| 65 | 2363893 | } |
| 66 | ||
| 67 | /** | |
| 68 | * Create a new PendingMessage. | |
| 69 | * | |
| 70 | * @param messageId | |
| 71 | * The id assigned to the message. | |
| 72 | * @param message | |
| 73 | * The sent message. | |
| 74 | * @param replyCallback | |
| 75 | * The callback for the reply to the message. | |
| 76 | * | |
| 77 | */ | |
| 78 | public PendingMessage(final int messageId, final Message message, | |
| 79 | 2394372 | final ReplyCallback replyCallback) { |
| 80 | 2394372 | myMessageId = messageId; |
| 81 | 2394372 | myMessage = message; |
| 82 | 2394372 | myReplyCallback = replyCallback; |
| 83 | 2394372 | } |
| 84 | ||
| 85 | /** | |
| 86 | * Clears the state of the message allowing the referenced objects to be | |
| 87 | * garbage collected. | |
| 88 | */ | |
| 89 | public void clear() { | |
| 90 | 484877 | myTimestamp = 0; |
| 91 | 484877 | myMessageId = 0; |
| 92 | 484877 | myMessage = null; |
| 93 | 484877 | myReplyCallback = null; |
| 94 | 484877 | } |
| 95 | ||
| 96 | /** | |
| 97 | * Returns the sent message. | |
| 98 | * | |
| 99 | * @return The sent message. | |
| 100 | */ | |
| 101 | public Message getMessage() { | |
| 102 | 928894 | return myMessage; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Returns the message id assigned to the sent message. | |
| 107 | * | |
| 108 | * @return The message id assigned to the sent message. | |
| 109 | */ | |
| 110 | public int getMessageId() { | |
| 111 | 971487 | return myMessageId; |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Returns the callback for the reply to the message. | |
| 116 | * | |
| 117 | * @return The callback for the reply to the message. | |
| 118 | */ | |
| 119 | public ReplyCallback getReplyCallback() { | |
| 120 | 929427 | return myReplyCallback; |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Determines the latency of the message in nano-seconds. If the message | |
| 125 | * does not have a time stamp then zero is returned. | |
| 126 | * | |
| 127 | * @return The current latency for the message. | |
| 128 | */ | |
| 129 | public long latency() { | |
| 130 | 215 | final long timestamp = myTimestamp; |
| 131 | ||
| 132 | 215 | if (timestamp == 0) { |
| 133 | 2 | return 0; |
| 134 | } | |
| 135 | ||
| 136 | 213 | return System.nanoTime() - timestamp; |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * Sets the state of the pending message. | |
| 141 | * | |
| 142 | * @param messageId | |
| 143 | * The id of the sent message. | |
| 144 | * @param message | |
| 145 | * The sent message. | |
| 146 | * @param replyCallback | |
| 147 | * The callback for the message. May be null. | |
| 148 | */ | |
| 149 | public void set(final int messageId, final Message message, | |
| 150 | final ReplyCallback replyCallback) { | |
| 151 | 475549 | myMessageId = messageId; |
| 152 | 475565 | myMessage = message; |
| 153 | 475564 | myReplyCallback = replyCallback; |
| 154 | 475567 | myTimestamp = System.nanoTime(); |
| 155 | 475865 | } |
| 156 | ||
| 157 | /** | |
| 158 | * Sets the state of the pending message. | |
| 159 | * | |
| 160 | * @param other | |
| 161 | * The pending message to copy from. | |
| 162 | */ | |
| 163 | public void set(final PendingMessage other) { | |
| 164 | 930142 | myMessageId = other.getMessageId(); |
| 165 | 931118 | myMessage = other.getMessage(); |
| 166 | 928695 | myReplyCallback = other.getReplyCallback(); |
| 167 | 928796 | myTimestamp = other.myTimestamp; |
| 168 | 928801 | } |
| 169 | ||
| 170 | /** | |
| 171 | * Sets the time stamp of the message to now. | |
| 172 | */ | |
| 173 | public void timestampNow() { | |
| 174 | 326 | myTimestamp = System.nanoTime(); |
| 175 | 326 | } |
| 176 | } |