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
023import java.util.Objects;
024
025
026/**
027 * Cached object.
028 *
029 * @param <V> The object type.
030 *
031 * @version 2024-04-20
032 */
033@Immutable
034public final class CachedObject<V> {
035        
036        
037        private final V object;
038        private final long timestamp;
039        private final long expirationTime;
040        
041        
042        /**
043         * Computes expiration time.
044         *
045         * @param currentTime The current time, in milliseconds since the Unix
046         *                    epoch.
047         * @param timeToLive  The time to live, in milliseconds.
048         *
049         * @return The expiration time, in milliseconds since the Unix epoch.
050         */
051        public static long computeExpirationTime(final long currentTime, final long timeToLive) {
052                
053                long expirationTime = currentTime + timeToLive;
054                
055                if (expirationTime < 0L) {
056                        // We have wrap around, return MAX value
057                        return Long.MAX_VALUE;
058                }
059                
060                return expirationTime;
061        }
062        
063        
064        /**
065         * Creates a new cached object.
066         *
067         * @param object         The cached object. Must not be {@code null}.
068         * @param timestamp      The caching timestamp, in milliseconds since
069         *                       the Unix epoch.
070         * @param expirationTime The expiration time, in milliseconds since the
071         *                       Unix epoch.
072         */
073        public CachedObject(final V object, final long timestamp, final long expirationTime) {
074                this.object = Objects.requireNonNull(object);
075                this.timestamp = timestamp;
076                this.expirationTime = expirationTime;
077        }
078        
079        
080        /**
081         * Returns the cached object.
082         *
083         * @return The cached object.
084         */
085        public V get() {
086                return object;
087        }
088        
089        
090        /**
091         * Returns the caching timestamp.
092         *
093         * @return The caching timestamp, in milliseconds since the Unix epoch.
094         */
095        public long getTimestamp() {
096                return timestamp;
097        }
098        
099        
100        /**
101         * Returns the expiration time.
102         *
103         * @return The expiration time, in milliseconds since the Unix epoch.
104         */
105        public long getExpirationTime() {
106                return expirationTime;
107        }
108        
109        
110        /**
111         * Returns {@code true} if the cached object is valid.
112         *
113         * @param currentTime The current time, in milliseconds since the Unix
114         *                    epoch.
115         *
116         * @return {@code true} if the cached object is valid, else
117         *         {@code false}.
118         */
119        public boolean isValid(final long currentTime) {
120                return currentTime < expirationTime;
121        }
122        
123        
124        /**
125         * Returns {@code true} if the cached object expired.
126         *
127         * @param currentTime The current time, in milliseconds since the Unix
128         *                    epoch.
129         *
130         * @return {@code true} if the cached object expired, else
131         *         {@code false}.
132         */
133        public boolean isExpired(final long currentTime) {
134                return ! isValid(currentTime);
135        }
136}