diff --git a/spring-web/src/main/java/org/springframework/http/CacheControl.java b/spring-web/src/main/java/org/springframework/http/CacheControl.java index 4cb693cf8f91..6cd1f6fd63f9 100644 --- a/spring-web/src/main/java/org/springframework/http/CacheControl.java +++ b/spring-web/src/main/java/org/springframework/http/CacheControl.java @@ -51,43 +51,56 @@ */ public class CacheControl { + private static final CacheControl EMPTY = new CacheControl(null, false, false, false, + false, false, false, false, null, null, + null, false); + @Nullable - private Duration maxAge; + private final Duration maxAge; - private boolean noCache = false; + private final boolean noCache; - private boolean noStore = false; + private final boolean noStore; - private boolean mustRevalidate = false; + private final boolean mustRevalidate; - private boolean noTransform = false; + private final boolean noTransform; - private boolean cachePublic = false; + private final boolean cachePublic; - private boolean cachePrivate = false; + private final boolean cachePrivate; - private boolean proxyRevalidate = false; + private final boolean proxyRevalidate; @Nullable - private Duration staleWhileRevalidate; + private final Duration staleWhileRevalidate; @Nullable - private Duration staleIfError; + private final Duration staleIfError; @Nullable - private Duration sMaxAge; - - private boolean immutable = false; - - - /** - * Create an empty CacheControl instance. - * @see #empty() - */ - protected CacheControl() { + private final Duration sMaxAge; + + private final boolean immutable; + + private CacheControl(@Nullable Duration maxAge, boolean noCache, boolean noStore, + boolean mustRevalidate, boolean noTransform, boolean cachePublic, + boolean cachePrivate, boolean proxyRevalidate, @Nullable Duration staleWhileRevalidate, + @Nullable Duration staleIfError, @Nullable Duration sMaxAge, boolean immutable) { + 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. *
This is well suited for using other optional directives without "max-age", @@ -95,7 +108,7 @@ protected CacheControl() { * @return {@code this}, to facilitate method chaining */ public static CacheControl empty() { - return new CacheControl(); + return EMPTY; } /** @@ -132,9 +145,8 @@ public static CacheControl maxAge(long maxAge, TimeUnit unit) { * @see rfc7234 section 5.2.2.8 */ public static CacheControl maxAge(Duration maxAge) { - CacheControl cc = new CacheControl(); - cc.maxAge = maxAge; - return cc; + return new CacheControl(maxAge, false, false, false, false, false, false, false, + null, null, null, false); } /** @@ -150,9 +162,8 @@ public static CacheControl maxAge(Duration maxAge) { * @see rfc7234 section 5.2.2.2 */ public static CacheControl noCache() { - CacheControl cc = new CacheControl(); - cc.noCache = true; - return cc; + return new CacheControl(null, true, false, false, false, false, false, false, + null, null, null, false); } /** @@ -163,9 +174,8 @@ public static CacheControl noCache() { * @see rfc7234 section 5.2.2.3 */ public static CacheControl noStore() { - CacheControl cc = new CacheControl(); - cc.noStore = true; - return cc; + return new CacheControl(null, false, true, false, false, false, false, false, + null, null, null, false); } @@ -178,8 +188,9 @@ public static CacheControl noStore() { * @see rfc7234 section 5.2.2.1 */ public CacheControl mustRevalidate() { - this.mustRevalidate = true; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, true, this.noTransform, + this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate, + this.staleIfError, this.sMaxAge, this.immutable); } /** @@ -191,8 +202,9 @@ public CacheControl mustRevalidate() { * @see rfc7234 section 5.2.2.4 */ public CacheControl noTransform() { - this.noTransform = true; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, true, + this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate, + this.staleIfError, this.sMaxAge, this.immutable); } /** @@ -204,8 +216,9 @@ public CacheControl noTransform() { * @see rfc7234 section 5.2.2.5 */ public CacheControl cachePublic() { - this.cachePublic = true; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + true, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate, + this.staleIfError, this.sMaxAge, this.immutable); } /** @@ -216,8 +229,9 @@ public CacheControl cachePublic() { * @see rfc7234 section 5.2.2.6 */ public CacheControl cachePrivate() { - this.cachePrivate = true; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + this.cachePublic, true, this.proxyRevalidate, this.staleWhileRevalidate, + this.staleIfError, this.sMaxAge, this.immutable); } /** @@ -228,8 +242,9 @@ public CacheControl cachePrivate() { * @see rfc7234 section 5.2.2.7 */ public CacheControl proxyRevalidate() { - this.proxyRevalidate = true; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + this.cachePublic, this.cachePrivate, true, this.staleWhileRevalidate, + this.staleIfError, this.sMaxAge, this.immutable); } /** @@ -256,8 +271,9 @@ public CacheControl sMaxAge(long sMaxAge, TimeUnit unit) { * @see rfc7234 section 5.2.2.9 */ public CacheControl sMaxAge(Duration sMaxAge) { - this.sMaxAge = sMaxAge; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate, + this.staleIfError, sMaxAge, this.immutable); } /** @@ -290,8 +306,9 @@ public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit uni * @see rfc5861 section 3 */ public CacheControl staleWhileRevalidate(Duration staleWhileRevalidate) { - this.staleWhileRevalidate = staleWhileRevalidate; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + this.cachePublic, this.cachePrivate, this.proxyRevalidate, staleWhileRevalidate, + this.staleIfError, this.sMaxAge, this.immutable); } /** @@ -318,8 +335,9 @@ public CacheControl staleIfError(long staleIfError, TimeUnit unit) { * @see rfc5861 section 4 */ public CacheControl staleIfError(Duration staleIfError) { - this.staleIfError = staleIfError; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate, + staleIfError, this.sMaxAge, this.immutable); } /** @@ -333,8 +351,9 @@ public CacheControl staleIfError(Duration staleIfError) { * @see rfc8246 */ public CacheControl immutable() { - this.immutable = true; - return this; + return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform, + this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate, + this.staleIfError, this.sMaxAge, true); } /**