001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.graph.cache;
023    
024    import java.util.concurrent.TimeUnit;
025    import org.jboss.dna.common.util.CheckArg;
026    
027    /**
028     * @author Randall Hauch
029     */
030    public class BasicCachePolicy implements CachePolicy {
031    
032        private static final long serialVersionUID = 1L;
033        private long timeToLiveInMillis = 0L;
034    
035        public BasicCachePolicy() {
036        }
037    
038        public BasicCachePolicy( long timeToCache,
039                                 TimeUnit unit ) {
040            CheckArg.isNotNull(unit, "unit");
041            this.timeToLiveInMillis = TimeUnit.MILLISECONDS.convert(timeToCache, unit);
042        }
043    
044        /**
045         * {@inheritDoc}
046         * 
047         * @see org.jboss.dna.graph.cache.CachePolicy#getTimeToLive()
048         */
049        public long getTimeToLive() {
050            return this.timeToLiveInMillis;
051        }
052    
053        /**
054         * Set the time for values and information to live in the cache.
055         * 
056         * @param timeToLive Sets timeToLive to the specified value.
057         * @param unit the unit in which the time to live value is defined
058         */
059        public void setTimeToLive( long timeToLive,
060                                   TimeUnit unit ) {
061            this.timeToLiveInMillis = TimeUnit.NANOSECONDS.convert(timeToLive, unit);
062        }
063    
064        public boolean isEmpty() {
065            return this.timeToLiveInMillis == 0;
066        }
067    
068        public CachePolicy getUnmodifiable() {
069            return new ImmutableCachePolicy(this.getTimeToLive());
070        }
071    
072        /**
073         * {@inheritDoc}
074         * 
075         * @see java.lang.Object#equals(java.lang.Object)
076         */
077        @Override
078        public boolean equals( Object obj ) {
079            if (obj == this) return true;
080            if (obj instanceof CachePolicy) {
081                CachePolicy that = (CachePolicy)obj;
082                if (this.getTimeToLive() != that.getTimeToLive()) return false;
083                if (obj instanceof BasicCachePolicy) return true;
084            }
085            return false;
086        }
087    
088        /**
089         * {@inheritDoc}
090         * 
091         * @see java.lang.Object#toString()
092         */
093        @Override
094        public String toString() {
095            return "{ TTL=" + this.timeToLiveInMillis + " ms }";
096        }
097    
098    }