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 2023-02-15
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                
051                long expirationTime = currentTime + timeToLive;
052                
053                if (expirationTime < 0L) {
054                        // We have wrap around, return MAX value
055                        return Long.MAX_VALUE;
056                }
057                
058                return expirationTime;
059        }
060        
061        
062        /**
063         * Creates a new cached object.
064         *
065         * @param object         The cached object. Must not be {@code null}.
066         * @param timestamp      The caching timestamp, in milliseconds since
067         *                       the Unix epoch.
068         * @param expirationTime The expiration time, in milliseconds since the
069         *                       Unix epoch.
070         */
071        public CachedObject(final V object, final long timestamp, final long expirationTime) {
072                if (object == null) {
073                        throw new IllegalArgumentException("The object must not be null");
074                }
075                this.object = object;
076                this.timestamp = timestamp;
077                this.expirationTime = expirationTime;
078        }
079        
080        
081        /**
082         * Returns the cached object.
083         *
084         * @return The cached object.
085         */
086        public V get() {
087                return object;
088        }
089        
090        
091        /**
092         * Returns the caching timestamp.
093         *
094         * @return The caching timestamp, in milliseconds since the Unix epoch.
095         */
096        public long getTimestamp() {
097                return timestamp;
098        }
099        
100        
101        /**
102         * Returns the expiration time.
103         *
104         * @return The expiration time, in milliseconds since the Unix epoch.
105         */
106        public long getExpirationTime() {
107                return expirationTime;
108        }
109        
110        
111        /**
112         * Returns {@code true} if the cached object is valid.
113         *
114         * @param currentTime The current time, in milliseconds since the Unix
115         *                    epoch.
116         *
117         * @return {@code true} if the cached object is valid, else
118         *         {@code false}.
119         */
120        public boolean isValid(final long currentTime) {
121                return currentTime < expirationTime;
122        }
123        
124        
125        /**
126         * Returns {@code true} if the cached object expired.
127         *
128         * @param currentTime The current time, in milliseconds since the Unix
129         *                    epoch.
130         *
131         * @return {@code true} if the cached object expired, else
132         *         {@code false}.
133         */
134        public boolean isExpired(final long currentTime) {
135                return ! isValid(currentTime);
136        }
137}