001/* 002 * nimbus-jose-jwt 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.jose.util.cache; 019 020 021import net.jcip.annotations.Immutable; 022 023 024/** 025 * Cached object. 026 * 027 * @param <V> The object type. 028 * 029 * @version 2022-04-08 030 */ 031@Immutable 032public final class CachedObject<V> { 033 034 035 private final V object; 036 private final long timestamp; 037 private final long expirationTime; 038 039 040 /** 041 * Computes expiration time. 042 * 043 * @param currentTime The current time, in milliseconds since the Unix 044 * epoch. 045 * @param timeToLive The time to live, in milliseconds. 046 * 047 * @return The expiration time, in milliseconds since the Unix epoch. 048 */ 049 public static long computeExpirationTime(final long currentTime, final long timeToLive) { 050 return currentTime + timeToLive; 051 } 052 053 054 /** 055 * Creates a new cached object. 056 * 057 * @param object The cached object. Must not be {@code null}. 058 * @param timestamp The caching timestamp, in milliseconds since 059 * the Unix epoch. 060 * @param expirationTime The expiration time, in milliseconds since the 061 * Unix epoch. 062 */ 063 public CachedObject(final V object, final long timestamp, final long expirationTime) { 064 if (object == null) { 065 throw new IllegalArgumentException("The object must not be null"); 066 } 067 this.object = object; 068 this.timestamp = timestamp; 069 this.expirationTime = expirationTime; 070 } 071 072 073 /** 074 * Returns the cached object. 075 * 076 * @return The cached object. 077 */ 078 public V get() { 079 return object; 080 } 081 082 083 /** 084 * Returns the caching timestamp. 085 * 086 * @return The caching timestamp, in milliseconds since the Unix epoch. 087 */ 088 public long getTimestamp() { 089 return timestamp; 090 } 091 092 093 /** 094 * Returns the expiration time. 095 * 096 * @return The expiration time, in milliseconds since the Unix epoch. 097 */ 098 public long getExpirationTime() { 099 return expirationTime; 100 } 101 102 103 /** 104 * Returns {@code true} if the cached object is valid. 105 * 106 * @param currentTime The current time, in milliseconds since the Unix 107 * epoch. 108 * 109 * @return {@code true} if the cached object is valid, else 110 * {@code false}. 111 */ 112 public boolean isValid(final long currentTime) { 113 return currentTime < expirationTime; 114 } 115 116 117 /** 118 * Returns {@code true} if the cached object expired. 119 * 120 * @param currentTime The current time, in milliseconds since the Unix 121 * epoch. 122 * 123 * @return {@code true} if the cached object expired, else 124 * {@code false}. 125 */ 126 public boolean isExpired(final long currentTime) { 127 return ! isValid(currentTime); 128 } 129}