1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.cache;
29
30 import org.apache.hc.core5.annotation.Contract;
31 import org.apache.hc.core5.annotation.Internal;
32 import org.apache.hc.core5.annotation.ThreadingBehavior;
33
34
35
36
37
38
39
40 @Internal
41 @Contract(threading = ThreadingBehavior.IMMUTABLE)
42 final class RequestCacheControl implements CacheControl {
43
44 private final long maxAge;
45 private final long maxStale;
46 private final long minFresh;
47 private final boolean noCache;
48 private final boolean noStore;
49 private final boolean onlyIfCached;
50 private final long staleIfError;
51
52
53
54
55
56
57 private final boolean noTransform;
58
59 RequestCacheControl(final long maxAge, final long maxStale, final long minFresh, final boolean noCache,
60 final boolean noStore, final boolean onlyIfCached, final long staleIfError, final boolean noTransform) {
61 this.maxAge = maxAge;
62 this.maxStale = maxStale;
63 this.minFresh = minFresh;
64 this.noCache = noCache;
65 this.noStore = noStore;
66 this.onlyIfCached = onlyIfCached;
67 this.staleIfError = staleIfError;
68 this.noTransform = noTransform;
69 }
70
71
72
73
74
75
76 @Override
77 public long getMaxAge() {
78 return maxAge;
79 }
80
81
82
83
84
85
86 public long getMaxStale() {
87 return maxStale;
88 }
89
90
91
92
93
94
95 public long getMinFresh() {
96 return minFresh;
97 }
98
99
100
101
102
103
104 @Override
105 public boolean isNoCache() {
106 return noCache;
107 }
108
109
110
111
112
113
114 @Override
115 public boolean isNoStore() {
116 return noStore;
117 }
118
119
120
121
122
123
124 public boolean isOnlyIfCached() {
125 return onlyIfCached;
126 }
127
128
129
130
131
132
133 @Override
134 public long getStaleIfError() {
135 return staleIfError;
136 }
137
138 @Override
139 public String toString() {
140 final StringBuilder buf = new StringBuilder();
141 buf.append("[");
142 if (maxAge >= 0) {
143 buf.append("max-age=").append(maxAge).append(",");
144 }
145 if (maxStale >= 0) {
146 buf.append("max-stale=").append(maxStale).append(",");
147 }
148 if (minFresh >= 0) {
149 buf.append("max-fresh=").append(minFresh).append(",");
150 }
151 if (noCache) {
152 buf.append("no-cache").append(",");
153 }
154 if (noStore) {
155 buf.append("no-store").append(",");
156 }
157 if (onlyIfCached) {
158 buf.append("only-if-cached").append(",");
159 }
160 if (staleIfError >= 0) {
161 buf.append("stale-if-error").append(staleIfError).append(",");
162 }
163 if (noTransform) {
164 buf.append("no-transform").append(",");
165 }
166 if (buf.charAt(buf.length() - 1) == ',') {
167 buf.setLength(buf.length() - 1);
168 }
169 buf.append("]");
170 return buf.toString();
171 }
172
173 static Builder builder() {
174 return new Builder();
175 }
176
177 static class Builder {
178
179 private long maxAge = -1;
180 private long maxStale = -1;
181 private long minFresh = -1;
182 private boolean noCache;
183 private boolean noStore;
184 private boolean onlyIfCached;
185 private long staleIfError = -1;
186 private boolean noTransform;
187
188 Builder() {
189 }
190
191 public long getMaxAge() {
192 return maxAge;
193 }
194
195 public Builder setMaxAge(final long maxAge) {
196 this.maxAge = maxAge;
197 return this;
198 }
199
200 public long getMaxStale() {
201 return maxStale;
202 }
203
204 public Builder setMaxStale(final long maxStale) {
205 this.maxStale = maxStale;
206 return this;
207 }
208
209 public long getMinFresh() {
210 return minFresh;
211 }
212
213 public Builder setMinFresh(final long minFresh) {
214 this.minFresh = minFresh;
215 return this;
216 }
217
218 public boolean isNoCache() {
219 return noCache;
220 }
221
222 public Builder setNoCache(final boolean noCache) {
223 this.noCache = noCache;
224 return this;
225 }
226
227 public boolean isNoStore() {
228 return noStore;
229 }
230
231 public Builder setNoStore(final boolean noStore) {
232 this.noStore = noStore;
233 return this;
234 }
235
236 public boolean isOnlyIfCached() {
237 return onlyIfCached;
238 }
239
240 public Builder setOnlyIfCached(final boolean onlyIfCached) {
241 this.onlyIfCached = onlyIfCached;
242 return this;
243 }
244
245 public long getStaleIfError() {
246 return staleIfError;
247 }
248
249 public Builder setStaleIfError(final long staleIfError) {
250 this.staleIfError = staleIfError;
251 return this;
252 }
253
254 public boolean isNoTransform() {
255 return noTransform;
256 }
257
258 public Builder setNoTransform(final boolean noTransform) {
259 this.noTransform = noTransform;
260 return this;
261 }
262
263
264 public RequestCacheControl build() {
265 return new RequestCacheControl(maxAge, maxStale, minFresh, noCache, noStore, onlyIfCached, staleIfError, noTransform);
266 }
267
268 }
269 }