Skip to content

Commit c3ab9bb

Browse files
kashikesnicoll
authored andcommitted
Make CacheControl immutable
See gh-33366
1 parent e27192e commit c3ab9bb

File tree

1 file changed

+68
-49
lines changed

1 file changed

+68
-49
lines changed

spring-web/src/main/java/org/springframework/http/CacheControl.java

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,51 +51,64 @@
5151
*/
5252
public class CacheControl {
5353

54+
private static final CacheControl EMPTY = new CacheControl(null, false, false, false,
55+
false, false, false, false, null, null,
56+
null, false);
57+
5458
@Nullable
55-
private Duration maxAge;
59+
private final Duration maxAge;
5660

57-
private boolean noCache = false;
61+
private final boolean noCache;
5862

59-
private boolean noStore = false;
63+
private final boolean noStore;
6064

61-
private boolean mustRevalidate = false;
65+
private final boolean mustRevalidate;
6266

63-
private boolean noTransform = false;
67+
private final boolean noTransform;
6468

65-
private boolean cachePublic = false;
69+
private final boolean cachePublic;
6670

67-
private boolean cachePrivate = false;
71+
private final boolean cachePrivate;
6872

69-
private boolean proxyRevalidate = false;
73+
private final boolean proxyRevalidate;
7074

7175
@Nullable
72-
private Duration staleWhileRevalidate;
76+
private final Duration staleWhileRevalidate;
7377

7478
@Nullable
75-
private Duration staleIfError;
79+
private final Duration staleIfError;
7680

7781
@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;
88102
}
89103

90-
91104
/**
92105
* Return an empty directive.
93106
* <p>This is well suited for using other optional directives without "max-age",
94107
* "no-cache" or "no-store".
95108
* @return {@code this}, to facilitate method chaining
96109
*/
97110
public static CacheControl empty() {
98-
return new CacheControl();
111+
return EMPTY;
99112
}
100113

101114
/**
@@ -132,9 +145,8 @@ public static CacheControl maxAge(long maxAge, TimeUnit unit) {
132145
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
133146
*/
134147
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);
138150
}
139151

140152
/**
@@ -150,9 +162,8 @@ public static CacheControl maxAge(Duration maxAge) {
150162
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.2">rfc7234 section 5.2.2.2</a>
151163
*/
152164
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);
156167
}
157168

158169
/**
@@ -163,9 +174,8 @@ public static CacheControl noCache() {
163174
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.3">rfc7234 section 5.2.2.3</a>
164175
*/
165176
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);
169179
}
170180

171181

@@ -178,8 +188,9 @@ public static CacheControl noStore() {
178188
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.1">rfc7234 section 5.2.2.1</a>
179189
*/
180190
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);
183194
}
184195

185196
/**
@@ -191,8 +202,9 @@ public CacheControl mustRevalidate() {
191202
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.4">rfc7234 section 5.2.2.4</a>
192203
*/
193204
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);
196208
}
197209

198210
/**
@@ -204,8 +216,9 @@ public CacheControl noTransform() {
204216
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.5">rfc7234 section 5.2.2.5</a>
205217
*/
206218
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);
209222
}
210223

211224
/**
@@ -216,8 +229,9 @@ public CacheControl cachePublic() {
216229
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.6">rfc7234 section 5.2.2.6</a>
217230
*/
218231
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);
221235
}
222236

223237
/**
@@ -228,8 +242,9 @@ public CacheControl cachePrivate() {
228242
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.7">rfc7234 section 5.2.2.7</a>
229243
*/
230244
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);
233248
}
234249

235250
/**
@@ -256,8 +271,9 @@ public CacheControl sMaxAge(long sMaxAge, TimeUnit unit) {
256271
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
257272
*/
258273
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);
261277
}
262278

263279
/**
@@ -290,8 +306,9 @@ public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit uni
290306
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
291307
*/
292308
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);
295312
}
296313

297314
/**
@@ -318,8 +335,9 @@ public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
318335
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
319336
*/
320337
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);
323341
}
324342

325343
/**
@@ -333,8 +351,9 @@ public CacheControl staleIfError(Duration staleIfError) {
333351
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
334352
*/
335353
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);
338357
}
339358

340359
/**

0 commit comments

Comments
 (0)