1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. 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,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27 package org.apache.hc.client5.http.impl.classic;
28
29 import java.util.concurrent.FutureTask;
30
31 import org.apache.hc.core5.concurrent.Cancellable;
32 import org.apache.hc.core5.http.ClassicHttpRequest;
33
34 final class HttpRequestFutureTask<V> extends FutureTask<V> {
35
36 private final ClassicHttpRequest request;
37 private final HttpRequestTaskCallable<V> callable;
38
39 HttpRequestFutureTask(
40 final ClassicHttpRequest request,
41 final HttpRequestTaskCallable<V> httpCallable) {
42 super(httpCallable);
43 this.request = request;
44 this.callable = httpCallable;
45 }
46
47 @Override
48 public boolean cancel(final boolean mayInterruptIfRunning) {
49 callable.cancel();
50 if (mayInterruptIfRunning && request instanceof Cancellable) {
51 ((Cancellable) request).cancel();
52 }
53 return super.cancel(mayInterruptIfRunning);
54 }
55
56 /**
57 * @return the time in millis the task was scheduled.
58 */
59 public long scheduledTime() {
60 return callable.getScheduled();
61 }
62
63 /**
64 * @return the time in millis the task was started.
65 */
66 public long startedTime() {
67 return callable.getStarted();
68 }
69
70 /**
71 * @return the time in millis the task was finished/cancelled.
72 */
73 public long endedTime() {
74 if (isDone()) {
75 return callable.getEnded();
76 }
77 throw new IllegalStateException("Task is not done yet");
78 }
79
80 /**
81 * @return the time in millis it took to make the request (excluding the time it was
82 * scheduled to be executed).
83 */
84 public long requestDuration() {
85 if (isDone()) {
86 return endedTime() - startedTime();
87 }
88 throw new IllegalStateException("Task is not done yet");
89 }
90
91 /**
92 * @return the time in millis it took to execute the task from the moment it was scheduled.
93 */
94 public long taskDuration() {
95 if (isDone()) {
96 return endedTime() - scheduledTime();
97 }
98 throw new IllegalStateException("Task is not done yet");
99 }
100
101 @Override
102 public String toString() {
103 return request.toString();
104 }
105
106 }