michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl; michael@0: michael@0: import java.util.HashMap; michael@0: import ch.boye.httpclientandroidlib.HttpConnectionMetrics; michael@0: import ch.boye.httpclientandroidlib.io.HttpTransportMetrics; michael@0: michael@0: /** michael@0: * Default implementation of the {@link HttpConnectionMetrics} interface. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public class HttpConnectionMetricsImpl implements HttpConnectionMetrics { michael@0: michael@0: public static final String REQUEST_COUNT = "http.request-count"; michael@0: public static final String RESPONSE_COUNT = "http.response-count"; michael@0: public static final String SENT_BYTES_COUNT = "http.sent-bytes-count"; michael@0: public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count"; michael@0: michael@0: private final HttpTransportMetrics inTransportMetric; michael@0: private final HttpTransportMetrics outTransportMetric; michael@0: private long requestCount = 0; michael@0: private long responseCount = 0; michael@0: michael@0: /** michael@0: * The cache map for all metrics values. michael@0: */ michael@0: private HashMap metricsCache; michael@0: michael@0: public HttpConnectionMetricsImpl( michael@0: final HttpTransportMetrics inTransportMetric, michael@0: final HttpTransportMetrics outTransportMetric) { michael@0: super(); michael@0: this.inTransportMetric = inTransportMetric; michael@0: this.outTransportMetric = outTransportMetric; michael@0: } michael@0: michael@0: /* ------------------ Public interface method -------------------------- */ michael@0: michael@0: public long getReceivedBytesCount() { michael@0: if (this.inTransportMetric != null) { michael@0: return this.inTransportMetric.getBytesTransferred(); michael@0: } else { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: public long getSentBytesCount() { michael@0: if (this.outTransportMetric != null) { michael@0: return this.outTransportMetric.getBytesTransferred(); michael@0: } else { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: public long getRequestCount() { michael@0: return this.requestCount; michael@0: } michael@0: michael@0: public void incrementRequestCount() { michael@0: this.requestCount++; michael@0: } michael@0: michael@0: public long getResponseCount() { michael@0: return this.responseCount; michael@0: } michael@0: michael@0: public void incrementResponseCount() { michael@0: this.responseCount++; michael@0: } michael@0: michael@0: public Object getMetric(final String metricName) { michael@0: Object value = null; michael@0: if (this.metricsCache != null) { michael@0: value = this.metricsCache.get(metricName); michael@0: } michael@0: if (value == null) { michael@0: if (REQUEST_COUNT.equals(metricName)) { michael@0: value = new Long(requestCount); michael@0: } else if (RESPONSE_COUNT.equals(metricName)) { michael@0: value = new Long(responseCount); michael@0: } else if (RECEIVED_BYTES_COUNT.equals(metricName)) { michael@0: if (this.inTransportMetric != null) { michael@0: return new Long(this.inTransportMetric.getBytesTransferred()); michael@0: } else { michael@0: return null; michael@0: } michael@0: } else if (SENT_BYTES_COUNT.equals(metricName)) { michael@0: if (this.outTransportMetric != null) { michael@0: return new Long(this.outTransportMetric.getBytesTransferred()); michael@0: } else { michael@0: return null; michael@0: } michael@0: } michael@0: } michael@0: return value; michael@0: } michael@0: michael@0: public void setMetric(final String metricName, Object obj) { michael@0: if (this.metricsCache == null) { michael@0: this.metricsCache = new HashMap(); michael@0: } michael@0: this.metricsCache.put(metricName, obj); michael@0: } michael@0: michael@0: public void reset() { michael@0: if (this.outTransportMetric != null) { michael@0: this.outTransportMetric.reset(); michael@0: } michael@0: if (this.inTransportMetric != null) { michael@0: this.inTransportMetric.reset(); michael@0: } michael@0: this.requestCount = 0; michael@0: this.responseCount = 0; michael@0: this.metricsCache = null; michael@0: } michael@0: michael@0: }