From 83ff0adc2ece396b21561c3a76c47d2413a818c8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 19 Mar 2015 17:25:44 -0400 Subject: [PATCH] Add accessors for expirationTime in FlashMap FlashMap now has a single field reflecting the expiration time and also provides accessors that can be used for serialization purposes. Issue: SPR-12757 --- .../springframework/web/servlet/FlashMap.java | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/FlashMap.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/FlashMap.java index 03a2ecfdf0..fa58cd5e78 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/FlashMap.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/FlashMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import org.springframework.util.MultiValueMap; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; + /** * A FlashMap provides a way for one request to store attributes intended for * use in another. This is most commonly needed when redirecting from one URL @@ -50,11 +51,9 @@ public final class FlashMap extends HashMap implements Comparabl private String targetRequestPath; - private final MultiValueMap targetRequestParams = new LinkedMultiValueMap(); - - private long expirationStartTime; + private final MultiValueMap targetRequestParams = new LinkedMultiValueMap(4); - private int timeToLive; + private long expirationTime = -1; /** @@ -112,8 +111,25 @@ public final class FlashMap extends HashMap implements Comparabl * @param timeToLive the number of seconds before expiration */ public void startExpirationPeriod(int timeToLive) { - this.expirationStartTime = System.currentTimeMillis(); - this.timeToLive = timeToLive; + this.expirationTime = System.currentTimeMillis() + timeToLive * 1000; + } + + /** + * Set the expiration time for the FlashMap. This is provided for serialization + * purposes but can also be used instead {@link #startExpirationPeriod(int)}. + * @since 4.2 + */ + public void setExpirationTime(long expirationTime) { + this.expirationTime = expirationTime; + } + + /** + * Return the expiration time for the FlashMap or -1 if the expiration + * period has not started. + * @since 4.2 + */ + public long getExpirationTime() { + return this.expirationTime; } /** @@ -121,8 +137,7 @@ public final class FlashMap extends HashMap implements Comparabl * elapsed time since the call to {@link #startExpirationPeriod}. */ public boolean isExpired() { - return (this.expirationStartTime != 0 && - (System.currentTimeMillis() - this.expirationStartTime > this.timeToLive * 1000)); + return (this.expirationTime != -1 && System.currentTimeMillis() > this.expirationTime); } @@ -167,11 +182,8 @@ public final class FlashMap extends HashMap implements Comparabl @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("FlashMap [attributes=").append(super.toString()); - sb.append(", targetRequestPath=").append(this.targetRequestPath); - sb.append(", targetRequestParams=").append(this.targetRequestParams).append("]"); - return sb.toString(); + return "FlashMap [attributes=" + super.toString() + ", targetRequestPath=" + + this.targetRequestPath + ", targetRequestParams=" + this.targetRequestParams + "]"; } }