mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/tsccm/BasicPoolEntry.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 *
michael@0 4 * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0 5 * contributor license agreements. See the NOTICE file distributed with
michael@0 6 * this work for additional information regarding copyright ownership.
michael@0 7 * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0 8 * (the "License"); you may not use this file except in compliance with
michael@0 9 * 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, software
michael@0 14 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 16 * See the License for the specific language governing permissions and
michael@0 17 * limitations under the License.
michael@0 18 * ====================================================================
michael@0 19 *
michael@0 20 * This software consists of voluntary contributions made by many
michael@0 21 * individuals on behalf of the Apache Software Foundation. For more
michael@0 22 * information on the Apache Software Foundation, please see
michael@0 23 * <http://www.apache.org/>.
michael@0 24 *
michael@0 25 */
michael@0 26
michael@0 27 package ch.boye.httpclientandroidlib.impl.conn.tsccm;
michael@0 28
michael@0 29 import java.lang.ref.ReferenceQueue;
michael@0 30 import java.util.concurrent.TimeUnit;
michael@0 31
michael@0 32 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
michael@0 33 import ch.boye.httpclientandroidlib.conn.OperatedClientConnection;
michael@0 34 import ch.boye.httpclientandroidlib.conn.ClientConnectionOperator;
michael@0 35 import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
michael@0 36 import ch.boye.httpclientandroidlib.impl.conn.AbstractPoolEntry;
michael@0 37
michael@0 38 /**
michael@0 39 * Basic implementation of a connection pool entry.
michael@0 40 *
michael@0 41 * @since 4.0
michael@0 42 */
michael@0 43 @NotThreadSafe
michael@0 44 public class BasicPoolEntry extends AbstractPoolEntry {
michael@0 45
michael@0 46 private final long created;
michael@0 47
michael@0 48 private long updated;
michael@0 49 private long validUntil;
michael@0 50 private long expiry;
michael@0 51
michael@0 52 /**
michael@0 53 * @deprecated do not use
michael@0 54 */
michael@0 55 @Deprecated
michael@0 56 public BasicPoolEntry(ClientConnectionOperator op,
michael@0 57 HttpRoute route,
michael@0 58 ReferenceQueue<Object> queue) {
michael@0 59 super(op, route);
michael@0 60 if (route == null) {
michael@0 61 throw new IllegalArgumentException("HTTP route may not be null");
michael@0 62 }
michael@0 63 this.created = System.currentTimeMillis();
michael@0 64 this.validUntil = Long.MAX_VALUE;
michael@0 65 this.expiry = this.validUntil;
michael@0 66 }
michael@0 67
michael@0 68 /**
michael@0 69 * Creates a new pool entry.
michael@0 70 *
michael@0 71 * @param op the connection operator
michael@0 72 * @param route the planned route for the connection
michael@0 73 */
michael@0 74 public BasicPoolEntry(ClientConnectionOperator op,
michael@0 75 HttpRoute route) {
michael@0 76 this(op, route, -1, TimeUnit.MILLISECONDS);
michael@0 77 }
michael@0 78
michael@0 79 /**
michael@0 80 * Creates a new pool entry with a specified maximum lifetime.
michael@0 81 *
michael@0 82 * @param op the connection operator
michael@0 83 * @param route the planned route for the connection
michael@0 84 * @param connTTL maximum lifetime of this entry, <=0 implies "infinity"
michael@0 85 * @param timeunit TimeUnit of connTTL
michael@0 86 *
michael@0 87 * @since 4.1
michael@0 88 */
michael@0 89 public BasicPoolEntry(ClientConnectionOperator op,
michael@0 90 HttpRoute route, long connTTL, TimeUnit timeunit) {
michael@0 91 super(op, route);
michael@0 92 if (route == null) {
michael@0 93 throw new IllegalArgumentException("HTTP route may not be null");
michael@0 94 }
michael@0 95 this.created = System.currentTimeMillis();
michael@0 96 if (connTTL > 0) {
michael@0 97 this.validUntil = this.created + timeunit.toMillis(connTTL);
michael@0 98 } else {
michael@0 99 this.validUntil = Long.MAX_VALUE;
michael@0 100 }
michael@0 101 this.expiry = this.validUntil;
michael@0 102 }
michael@0 103
michael@0 104 protected final OperatedClientConnection getConnection() {
michael@0 105 return super.connection;
michael@0 106 }
michael@0 107
michael@0 108 protected final HttpRoute getPlannedRoute() {
michael@0 109 return super.route;
michael@0 110 }
michael@0 111
michael@0 112 @Deprecated
michael@0 113 protected final BasicPoolEntryRef getWeakRef() {
michael@0 114 return null;
michael@0 115 }
michael@0 116
michael@0 117 @Override
michael@0 118 protected void shutdownEntry() {
michael@0 119 super.shutdownEntry();
michael@0 120 }
michael@0 121
michael@0 122 /**
michael@0 123 * @since 4.1
michael@0 124 */
michael@0 125 public long getCreated() {
michael@0 126 return this.created;
michael@0 127 }
michael@0 128
michael@0 129 /**
michael@0 130 * @since 4.1
michael@0 131 */
michael@0 132 public long getUpdated() {
michael@0 133 return this.updated;
michael@0 134 }
michael@0 135
michael@0 136 /**
michael@0 137 * @since 4.1
michael@0 138 */
michael@0 139 public long getExpiry() {
michael@0 140 return this.expiry;
michael@0 141 }
michael@0 142
michael@0 143 public long getValidUntil() {
michael@0 144 return this.validUntil;
michael@0 145 }
michael@0 146
michael@0 147 /**
michael@0 148 * @since 4.1
michael@0 149 */
michael@0 150 public void updateExpiry(long time, TimeUnit timeunit) {
michael@0 151 this.updated = System.currentTimeMillis();
michael@0 152 long newExpiry;
michael@0 153 if (time > 0) {
michael@0 154 newExpiry = this.updated + timeunit.toMillis(time);
michael@0 155 } else {
michael@0 156 newExpiry = Long.MAX_VALUE;
michael@0 157 }
michael@0 158 this.expiry = Math.min(validUntil, newExpiry);
michael@0 159 }
michael@0 160
michael@0 161 /**
michael@0 162 * @since 4.1
michael@0 163 */
michael@0 164 public boolean isExpired(long now) {
michael@0 165 return now >= this.expiry;
michael@0 166 }
michael@0 167
michael@0 168 }
michael@0 169
michael@0 170

mercurial