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