View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.model;
17  
18  import java.util.Date;
19  
20  /**
21   * @author Christian Bauer
22   */
23  public class ExpirationDetails {
24  
25      public static final int UNLIMITED_AGE = 0;
26  
27      private int maxAgeSeconds = UNLIMITED_AGE;
28      private long lastRefreshTimestampSeconds = getCurrentTimestampSeconds();
29  
30      public ExpirationDetails() {
31      }
32  
33      public ExpirationDetails(int maxAgeSeconds) {
34          this.maxAgeSeconds = maxAgeSeconds;
35      }
36  
37      public int getMaxAgeSeconds() {
38          return maxAgeSeconds;
39      }
40  
41      public long getLastRefreshTimestampSeconds() {
42          return lastRefreshTimestampSeconds;
43      }
44  
45      public void setLastRefreshTimestampSeconds(long lastRefreshTimestampSeconds) {
46          this.lastRefreshTimestampSeconds = lastRefreshTimestampSeconds;
47      }
48  
49      public void stampLastRefresh() {
50          setLastRefreshTimestampSeconds(getCurrentTimestampSeconds());
51      }
52  
53      public boolean hasExpired() {
54          return hasExpired(false);
55      }
56  
57      /**
58       * @param halfTime If <code>true</code> then half maximum age is used to determine expiration.
59       * @return <code>true</code> if the maximum age has been reached.
60       */
61      public boolean hasExpired(boolean halfTime) {
62          // Note: Uses direct field access for performance reasons on Android
63          return maxAgeSeconds != UNLIMITED_AGE &&
64                  (lastRefreshTimestampSeconds + (maxAgeSeconds/(halfTime ? 2 : 1))) < getCurrentTimestampSeconds();
65      }
66  
67      public long getSecondsUntilExpiration() {
68          // Note: Uses direct field access for performance reasons on Android
69          return maxAgeSeconds == UNLIMITED_AGE
70                  ? Integer.MAX_VALUE
71                  : (lastRefreshTimestampSeconds + maxAgeSeconds) - getCurrentTimestampSeconds();
72      }
73  
74      protected long getCurrentTimestampSeconds() {
75          return new Date().getTime()/1000;
76      }
77  
78      // Performance optimization on Android
79      private static  String simpleName = ExpirationDetails.class.getSimpleName();
80  	@Override
81      public String toString() {
82          return "(" + simpleName + ")" + " MAX AGE: " + maxAgeSeconds;
83      }
84  }