Make CacheControl immutable

See gh-33366
This commit is contained in:
Riley Park 2024-08-12 00:04:12 -07:00 committed by Stéphane Nicoll
parent e27192e8ad
commit c3ab9bba11
1 changed files with 73 additions and 54 deletions

View File

@ -51,43 +51,56 @@ import org.springframework.util.StringUtils;
*/ */
public class CacheControl { public class CacheControl {
@Nullable private static final CacheControl EMPTY = new CacheControl(null, false, false, false,
private Duration maxAge; false, false, false, false, null, null,
null, false);
private boolean noCache = false;
private boolean noStore = false;
private boolean mustRevalidate = false;
private boolean noTransform = false;
private boolean cachePublic = false;
private boolean cachePrivate = false;
private boolean proxyRevalidate = false;
@Nullable @Nullable
private Duration staleWhileRevalidate; private final Duration maxAge;
private final boolean noCache;
private final boolean noStore;
private final boolean mustRevalidate;
private final boolean noTransform;
private final boolean cachePublic;
private final boolean cachePrivate;
private final boolean proxyRevalidate;
@Nullable @Nullable
private Duration staleIfError; private final Duration staleWhileRevalidate;
@Nullable @Nullable
private Duration sMaxAge; private final Duration staleIfError;
private boolean immutable = false; @Nullable
private final Duration sMaxAge;
private final boolean immutable;
/** private CacheControl(@Nullable Duration maxAge, boolean noCache, boolean noStore,
* Create an empty CacheControl instance. boolean mustRevalidate, boolean noTransform, boolean cachePublic,
* @see #empty() boolean cachePrivate, boolean proxyRevalidate, @Nullable Duration staleWhileRevalidate,
*/ @Nullable Duration staleIfError, @Nullable Duration sMaxAge, boolean immutable) {
protected CacheControl() { this.maxAge = maxAge;
this.noCache = noCache;
this.noStore = noStore;
this.mustRevalidate = mustRevalidate;
this.noTransform = noTransform;
this.cachePublic = cachePublic;
this.cachePrivate = cachePrivate;
this.proxyRevalidate = proxyRevalidate;
this.staleWhileRevalidate = staleWhileRevalidate;
this.staleIfError = staleIfError;
this.sMaxAge = sMaxAge;
this.immutable = immutable;
} }
/** /**
* Return an empty directive. * Return an empty directive.
* <p>This is well suited for using other optional directives without "max-age", * <p>This is well suited for using other optional directives without "max-age",
@ -95,7 +108,7 @@ public class CacheControl {
* @return {@code this}, to facilitate method chaining * @return {@code this}, to facilitate method chaining
*/ */
public static CacheControl empty() { public static CacheControl empty() {
return new CacheControl(); return EMPTY;
} }
/** /**
@ -132,9 +145,8 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
*/ */
public static CacheControl maxAge(Duration maxAge) { public static CacheControl maxAge(Duration maxAge) {
CacheControl cc = new CacheControl(); return new CacheControl(maxAge, false, false, false, false, false, false, false,
cc.maxAge = maxAge; null, null, null, false);
return cc;
} }
/** /**
@ -150,9 +162,8 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.2">rfc7234 section 5.2.2.2</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.2">rfc7234 section 5.2.2.2</a>
*/ */
public static CacheControl noCache() { public static CacheControl noCache() {
CacheControl cc = new CacheControl(); return new CacheControl(null, true, false, false, false, false, false, false,
cc.noCache = true; null, null, null, false);
return cc;
} }
/** /**
@ -163,9 +174,8 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.3">rfc7234 section 5.2.2.3</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.3">rfc7234 section 5.2.2.3</a>
*/ */
public static CacheControl noStore() { public static CacheControl noStore() {
CacheControl cc = new CacheControl(); return new CacheControl(null, false, true, false, false, false, false, false,
cc.noStore = true; null, null, null, false);
return cc;
} }
@ -178,8 +188,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.1">rfc7234 section 5.2.2.1</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.1">rfc7234 section 5.2.2.1</a>
*/ */
public CacheControl mustRevalidate() { public CacheControl mustRevalidate() {
this.mustRevalidate = true; return new CacheControl(this.maxAge, this.noCache, this.noStore, true, this.noTransform,
return this; this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -191,8 +202,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.4">rfc7234 section 5.2.2.4</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.4">rfc7234 section 5.2.2.4</a>
*/ */
public CacheControl noTransform() { public CacheControl noTransform() {
this.noTransform = true; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, true,
return this; this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -204,8 +216,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.5">rfc7234 section 5.2.2.5</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.5">rfc7234 section 5.2.2.5</a>
*/ */
public CacheControl cachePublic() { public CacheControl cachePublic() {
this.cachePublic = true; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; true, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -216,8 +229,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.6">rfc7234 section 5.2.2.6</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.6">rfc7234 section 5.2.2.6</a>
*/ */
public CacheControl cachePrivate() { public CacheControl cachePrivate() {
this.cachePrivate = true; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; this.cachePublic, true, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -228,8 +242,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.7">rfc7234 section 5.2.2.7</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.7">rfc7234 section 5.2.2.7</a>
*/ */
public CacheControl proxyRevalidate() { public CacheControl proxyRevalidate() {
this.proxyRevalidate = true; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; this.cachePublic, this.cachePrivate, true, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -256,8 +271,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a> * @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
*/ */
public CacheControl sMaxAge(Duration sMaxAge) { public CacheControl sMaxAge(Duration sMaxAge) {
this.sMaxAge = sMaxAge; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, sMaxAge, this.immutable);
} }
/** /**
@ -290,8 +306,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a> * @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
*/ */
public CacheControl staleWhileRevalidate(Duration staleWhileRevalidate) { public CacheControl staleWhileRevalidate(Duration staleWhileRevalidate) {
this.staleWhileRevalidate = staleWhileRevalidate; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; this.cachePublic, this.cachePrivate, this.proxyRevalidate, staleWhileRevalidate,
this.staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -318,8 +335,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a> * @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
*/ */
public CacheControl staleIfError(Duration staleIfError) { public CacheControl staleIfError(Duration staleIfError) {
this.staleIfError = staleIfError; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
staleIfError, this.sMaxAge, this.immutable);
} }
/** /**
@ -333,8 +351,9 @@ public class CacheControl {
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a> * @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
*/ */
public CacheControl immutable() { public CacheControl immutable() {
this.immutable = true; return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
return this; this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
this.staleIfError, this.sMaxAge, true);
} }
/** /**