Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl; |
michael@0 | 29 | |
michael@0 | 30 | import java.util.HashMap; |
michael@0 | 31 | import ch.boye.httpclientandroidlib.HttpConnectionMetrics; |
michael@0 | 32 | import ch.boye.httpclientandroidlib.io.HttpTransportMetrics; |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * Default implementation of the {@link HttpConnectionMetrics} interface. |
michael@0 | 36 | * |
michael@0 | 37 | * @since 4.0 |
michael@0 | 38 | */ |
michael@0 | 39 | public class HttpConnectionMetricsImpl implements HttpConnectionMetrics { |
michael@0 | 40 | |
michael@0 | 41 | public static final String REQUEST_COUNT = "http.request-count"; |
michael@0 | 42 | public static final String RESPONSE_COUNT = "http.response-count"; |
michael@0 | 43 | public static final String SENT_BYTES_COUNT = "http.sent-bytes-count"; |
michael@0 | 44 | public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count"; |
michael@0 | 45 | |
michael@0 | 46 | private final HttpTransportMetrics inTransportMetric; |
michael@0 | 47 | private final HttpTransportMetrics outTransportMetric; |
michael@0 | 48 | private long requestCount = 0; |
michael@0 | 49 | private long responseCount = 0; |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * The cache map for all metrics values. |
michael@0 | 53 | */ |
michael@0 | 54 | private HashMap metricsCache; |
michael@0 | 55 | |
michael@0 | 56 | public HttpConnectionMetricsImpl( |
michael@0 | 57 | final HttpTransportMetrics inTransportMetric, |
michael@0 | 58 | final HttpTransportMetrics outTransportMetric) { |
michael@0 | 59 | super(); |
michael@0 | 60 | this.inTransportMetric = inTransportMetric; |
michael@0 | 61 | this.outTransportMetric = outTransportMetric; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /* ------------------ Public interface method -------------------------- */ |
michael@0 | 65 | |
michael@0 | 66 | public long getReceivedBytesCount() { |
michael@0 | 67 | if (this.inTransportMetric != null) { |
michael@0 | 68 | return this.inTransportMetric.getBytesTransferred(); |
michael@0 | 69 | } else { |
michael@0 | 70 | return -1; |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | public long getSentBytesCount() { |
michael@0 | 75 | if (this.outTransportMetric != null) { |
michael@0 | 76 | return this.outTransportMetric.getBytesTransferred(); |
michael@0 | 77 | } else { |
michael@0 | 78 | return -1; |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | public long getRequestCount() { |
michael@0 | 83 | return this.requestCount; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | public void incrementRequestCount() { |
michael@0 | 87 | this.requestCount++; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | public long getResponseCount() { |
michael@0 | 91 | return this.responseCount; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | public void incrementResponseCount() { |
michael@0 | 95 | this.responseCount++; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | public Object getMetric(final String metricName) { |
michael@0 | 99 | Object value = null; |
michael@0 | 100 | if (this.metricsCache != null) { |
michael@0 | 101 | value = this.metricsCache.get(metricName); |
michael@0 | 102 | } |
michael@0 | 103 | if (value == null) { |
michael@0 | 104 | if (REQUEST_COUNT.equals(metricName)) { |
michael@0 | 105 | value = new Long(requestCount); |
michael@0 | 106 | } else if (RESPONSE_COUNT.equals(metricName)) { |
michael@0 | 107 | value = new Long(responseCount); |
michael@0 | 108 | } else if (RECEIVED_BYTES_COUNT.equals(metricName)) { |
michael@0 | 109 | if (this.inTransportMetric != null) { |
michael@0 | 110 | return new Long(this.inTransportMetric.getBytesTransferred()); |
michael@0 | 111 | } else { |
michael@0 | 112 | return null; |
michael@0 | 113 | } |
michael@0 | 114 | } else if (SENT_BYTES_COUNT.equals(metricName)) { |
michael@0 | 115 | if (this.outTransportMetric != null) { |
michael@0 | 116 | return new Long(this.outTransportMetric.getBytesTransferred()); |
michael@0 | 117 | } else { |
michael@0 | 118 | return null; |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | return value; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | public void setMetric(final String metricName, Object obj) { |
michael@0 | 126 | if (this.metricsCache == null) { |
michael@0 | 127 | this.metricsCache = new HashMap(); |
michael@0 | 128 | } |
michael@0 | 129 | this.metricsCache.put(metricName, obj); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | public void reset() { |
michael@0 | 133 | if (this.outTransportMetric != null) { |
michael@0 | 134 | this.outTransportMetric.reset(); |
michael@0 | 135 | } |
michael@0 | 136 | if (this.inTransportMetric != null) { |
michael@0 | 137 | this.inTransportMetric.reset(); |
michael@0 | 138 | } |
michael@0 | 139 | this.requestCount = 0; |
michael@0 | 140 | this.responseCount = 0; |
michael@0 | 141 | this.metricsCache = null; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | } |