|
1 /* |
|
2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.internal.net.http.common; |
|
27 |
|
28 import java.util.concurrent.atomic.AtomicLong; |
|
29 |
|
30 /** |
|
31 * Encapsulates operations with demand (Reactive Streams). |
|
32 * |
|
33 * <p> Demand is the aggregated number of elements requested by a Subscriber |
|
34 * which is yet to be delivered (fulfilled) by the Publisher. |
|
35 */ |
|
36 public final class Demand { |
|
37 |
|
38 private final AtomicLong val = new AtomicLong(); |
|
39 |
|
40 /** |
|
41 * Increases this demand by the specified positive value. |
|
42 * |
|
43 * @param n |
|
44 * increment {@code > 0} |
|
45 * |
|
46 * @return {@code true} iff prior to this operation this demand was fulfilled |
|
47 */ |
|
48 public boolean increase(long n) { |
|
49 if (n <= 0) { |
|
50 throw new IllegalArgumentException(String.valueOf(n)); |
|
51 } |
|
52 long prev = val.getAndAccumulate(n, (p, i) -> p + i < 0 ? Long.MAX_VALUE : p + i); |
|
53 return prev == 0; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Increases this demand by 1 but only if it is fulfilled. |
|
58 * @return true if the demand was increased, false otherwise. |
|
59 */ |
|
60 public boolean increaseIfFulfilled() { |
|
61 return val.compareAndSet(0, 1); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Tries to decrease this demand by the specified positive value. |
|
66 * |
|
67 * <p> The actual value this demand has been decreased by might be less than |
|
68 * {@code n}, including {@code 0} (no decrease at all). |
|
69 * |
|
70 * @param n |
|
71 * decrement {@code > 0} |
|
72 * |
|
73 * @return a value {@code m} ({@code 0 <= m <= n}) this demand has been |
|
74 * actually decreased by |
|
75 */ |
|
76 public long decreaseAndGet(long n) { |
|
77 if (n <= 0) { |
|
78 throw new IllegalArgumentException(String.valueOf(n)); |
|
79 } |
|
80 long p, d; |
|
81 do { |
|
82 d = val.get(); |
|
83 p = Math.min(d, n); |
|
84 } while (!val.compareAndSet(d, d - p)); |
|
85 return p; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Tries to decrease this demand by {@code 1}. |
|
90 * |
|
91 * @return {@code true} iff this demand has been decreased by {@code 1} |
|
92 */ |
|
93 public boolean tryDecrement() { |
|
94 return decreaseAndGet(1) == 1; |
|
95 } |
|
96 |
|
97 /** |
|
98 * @return {@code true} iff there is no unfulfilled demand |
|
99 */ |
|
100 public boolean isFulfilled() { |
|
101 return val.get() == 0; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Resets this object to its initial state. |
|
106 */ |
|
107 public void reset() { |
|
108 val.set(0); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Returns the current value of this demand. |
|
113 * |
|
114 * @return the current value of this demand |
|
115 */ |
|
116 public long get() { |
|
117 return val.get(); |
|
118 } |
|
119 |
|
120 @Override |
|
121 public String toString() { |
|
122 return String.valueOf(val.get()); |
|
123 } |
|
124 } |