51
51
*/
52
52
public class CacheControl {
53
53
54
+ private static final CacheControl EMPTY = new CacheControl (null , false , false , false ,
55
+ false , false , false , false , null , null ,
56
+ null , false );
57
+
54
58
@ Nullable
55
- private Duration maxAge ;
59
+ private final Duration maxAge ;
56
60
57
- private boolean noCache = false ;
61
+ private final boolean noCache ;
58
62
59
- private boolean noStore = false ;
63
+ private final boolean noStore ;
60
64
61
- private boolean mustRevalidate = false ;
65
+ private final boolean mustRevalidate ;
62
66
63
- private boolean noTransform = false ;
67
+ private final boolean noTransform ;
64
68
65
- private boolean cachePublic = false ;
69
+ private final boolean cachePublic ;
66
70
67
- private boolean cachePrivate = false ;
71
+ private final boolean cachePrivate ;
68
72
69
- private boolean proxyRevalidate = false ;
73
+ private final boolean proxyRevalidate ;
70
74
71
75
@ Nullable
72
- private Duration staleWhileRevalidate ;
76
+ private final Duration staleWhileRevalidate ;
73
77
74
78
@ Nullable
75
- private Duration staleIfError ;
79
+ private final Duration staleIfError ;
76
80
77
81
@ Nullable
78
- private Duration sMaxAge ;
79
-
80
- private boolean immutable = false ;
81
-
82
-
83
- /**
84
- * Create an empty CacheControl instance.
85
- * @see #empty()
86
- */
87
- protected CacheControl () {
82
+ private final Duration sMaxAge ;
83
+
84
+ private final boolean immutable ;
85
+
86
+ private CacheControl (@ Nullable Duration maxAge , boolean noCache , boolean noStore ,
87
+ boolean mustRevalidate , boolean noTransform , boolean cachePublic ,
88
+ boolean cachePrivate , boolean proxyRevalidate , @ Nullable Duration staleWhileRevalidate ,
89
+ @ Nullable Duration staleIfError , @ Nullable Duration sMaxAge , boolean immutable ) {
90
+ this .maxAge = maxAge ;
91
+ this .noCache = noCache ;
92
+ this .noStore = noStore ;
93
+ this .mustRevalidate = mustRevalidate ;
94
+ this .noTransform = noTransform ;
95
+ this .cachePublic = cachePublic ;
96
+ this .cachePrivate = cachePrivate ;
97
+ this .proxyRevalidate = proxyRevalidate ;
98
+ this .staleWhileRevalidate = staleWhileRevalidate ;
99
+ this .staleIfError = staleIfError ;
100
+ this .sMaxAge = sMaxAge ;
101
+ this .immutable = immutable ;
88
102
}
89
103
90
-
91
104
/**
92
105
* Return an empty directive.
93
106
* <p>This is well suited for using other optional directives without "max-age",
94
107
* "no-cache" or "no-store".
95
108
* @return {@code this}, to facilitate method chaining
96
109
*/
97
110
public static CacheControl empty () {
98
- return new CacheControl () ;
111
+ return EMPTY ;
99
112
}
100
113
101
114
/**
@@ -132,9 +145,8 @@ public static CacheControl maxAge(long maxAge, TimeUnit unit) {
132
145
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
133
146
*/
134
147
public static CacheControl maxAge (Duration maxAge ) {
135
- CacheControl cc = new CacheControl ();
136
- cc .maxAge = maxAge ;
137
- return cc ;
148
+ return new CacheControl (maxAge , false , false , false , false , false , false , false ,
149
+ null , null , null , false );
138
150
}
139
151
140
152
/**
@@ -150,9 +162,8 @@ public static CacheControl maxAge(Duration maxAge) {
150
162
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.2">rfc7234 section 5.2.2.2</a>
151
163
*/
152
164
public static CacheControl noCache () {
153
- CacheControl cc = new CacheControl ();
154
- cc .noCache = true ;
155
- return cc ;
165
+ return new CacheControl (null , true , false , false , false , false , false , false ,
166
+ null , null , null , false );
156
167
}
157
168
158
169
/**
@@ -163,9 +174,8 @@ public static CacheControl noCache() {
163
174
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.3">rfc7234 section 5.2.2.3</a>
164
175
*/
165
176
public static CacheControl noStore () {
166
- CacheControl cc = new CacheControl ();
167
- cc .noStore = true ;
168
- return cc ;
177
+ return new CacheControl (null , false , true , false , false , false , false , false ,
178
+ null , null , null , false );
169
179
}
170
180
171
181
@@ -178,8 +188,9 @@ public static CacheControl noStore() {
178
188
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.1">rfc7234 section 5.2.2.1</a>
179
189
*/
180
190
public CacheControl mustRevalidate () {
181
- this .mustRevalidate = true ;
182
- return this ;
191
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , true , this .noTransform ,
192
+ this .cachePublic , this .cachePrivate , this .proxyRevalidate , this .staleWhileRevalidate ,
193
+ this .staleIfError , this .sMaxAge , this .immutable );
183
194
}
184
195
185
196
/**
@@ -191,8 +202,9 @@ public CacheControl mustRevalidate() {
191
202
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.4">rfc7234 section 5.2.2.4</a>
192
203
*/
193
204
public CacheControl noTransform () {
194
- this .noTransform = true ;
195
- return this ;
205
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , true ,
206
+ this .cachePublic , this .cachePrivate , this .proxyRevalidate , this .staleWhileRevalidate ,
207
+ this .staleIfError , this .sMaxAge , this .immutable );
196
208
}
197
209
198
210
/**
@@ -204,8 +216,9 @@ public CacheControl noTransform() {
204
216
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.5">rfc7234 section 5.2.2.5</a>
205
217
*/
206
218
public CacheControl cachePublic () {
207
- this .cachePublic = true ;
208
- return this ;
219
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
220
+ true , this .cachePrivate , this .proxyRevalidate , this .staleWhileRevalidate ,
221
+ this .staleIfError , this .sMaxAge , this .immutable );
209
222
}
210
223
211
224
/**
@@ -216,8 +229,9 @@ public CacheControl cachePublic() {
216
229
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.6">rfc7234 section 5.2.2.6</a>
217
230
*/
218
231
public CacheControl cachePrivate () {
219
- this .cachePrivate = true ;
220
- return this ;
232
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
233
+ this .cachePublic , true , this .proxyRevalidate , this .staleWhileRevalidate ,
234
+ this .staleIfError , this .sMaxAge , this .immutable );
221
235
}
222
236
223
237
/**
@@ -228,8 +242,9 @@ public CacheControl cachePrivate() {
228
242
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.7">rfc7234 section 5.2.2.7</a>
229
243
*/
230
244
public CacheControl proxyRevalidate () {
231
- this .proxyRevalidate = true ;
232
- return this ;
245
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
246
+ this .cachePublic , this .cachePrivate , true , this .staleWhileRevalidate ,
247
+ this .staleIfError , this .sMaxAge , this .immutable );
233
248
}
234
249
235
250
/**
@@ -256,8 +271,9 @@ public CacheControl sMaxAge(long sMaxAge, TimeUnit unit) {
256
271
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
257
272
*/
258
273
public CacheControl sMaxAge (Duration sMaxAge ) {
259
- this .sMaxAge = sMaxAge ;
260
- return this ;
274
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
275
+ this .cachePublic , this .cachePrivate , this .proxyRevalidate , this .staleWhileRevalidate ,
276
+ this .staleIfError , sMaxAge , this .immutable );
261
277
}
262
278
263
279
/**
@@ -290,8 +306,9 @@ public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit uni
290
306
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
291
307
*/
292
308
public CacheControl staleWhileRevalidate (Duration staleWhileRevalidate ) {
293
- this .staleWhileRevalidate = staleWhileRevalidate ;
294
- return this ;
309
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
310
+ this .cachePublic , this .cachePrivate , this .proxyRevalidate , staleWhileRevalidate ,
311
+ this .staleIfError , this .sMaxAge , this .immutable );
295
312
}
296
313
297
314
/**
@@ -318,8 +335,9 @@ public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
318
335
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
319
336
*/
320
337
public CacheControl staleIfError (Duration staleIfError ) {
321
- this .staleIfError = staleIfError ;
322
- return this ;
338
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
339
+ this .cachePublic , this .cachePrivate , this .proxyRevalidate , this .staleWhileRevalidate ,
340
+ staleIfError , this .sMaxAge , this .immutable );
323
341
}
324
342
325
343
/**
@@ -333,8 +351,9 @@ public CacheControl staleIfError(Duration staleIfError) {
333
351
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
334
352
*/
335
353
public CacheControl immutable () {
336
- this .immutable = true ;
337
- return this ;
354
+ return new CacheControl (this .maxAge , this .noCache , this .noStore , this .mustRevalidate , this .noTransform ,
355
+ this .cachePublic , this .cachePrivate , this .proxyRevalidate , this .staleWhileRevalidate ,
356
+ this .staleIfError , this .sMaxAge , true );
338
357
}
339
358
340
359
/**
0 commit comments