1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.cache;
25
26 import java.util.concurrent.TimeUnit;
27 import net.jcip.annotations.NotThreadSafe;
28 import org.modeshape.common.util.CheckArg;
29
30 /**
31 * A basic mutable {@link CachePolicy} implementation.
32 */
33 @NotThreadSafe
34 public class BasicCachePolicy implements CachePolicy {
35
36 private static final long serialVersionUID = 1L;
37 private long timeToLiveInMillis = 0L;
38
39 public BasicCachePolicy() {
40 }
41
42 public BasicCachePolicy( long timeToCache,
43 TimeUnit unit ) {
44 CheckArg.isNotNull(unit, "unit");
45 this.timeToLiveInMillis = TimeUnit.MILLISECONDS.convert(timeToCache, unit);
46 }
47
48 /**
49 * {@inheritDoc}
50 *
51 * @see org.modeshape.graph.cache.CachePolicy#getTimeToLive()
52 */
53 public long getTimeToLive() {
54 return this.timeToLiveInMillis;
55 }
56
57 /**
58 * Set the time for values and information to live in the cache.
59 *
60 * @param timeToLive Sets timeToLive to the specified value.
61 * @param unit the unit in which the time to live value is defined
62 */
63 public void setTimeToLive( long timeToLive,
64 TimeUnit unit ) {
65 this.timeToLiveInMillis = TimeUnit.NANOSECONDS.convert(timeToLive, unit);
66 }
67
68 public boolean isEmpty() {
69 return this.timeToLiveInMillis == 0;
70 }
71
72 public CachePolicy getUnmodifiable() {
73 return new ImmutableCachePolicy(this.getTimeToLive());
74 }
75
76 /**
77 * {@inheritDoc}
78 *
79 * @see java.lang.Object#equals(java.lang.Object)
80 */
81 @Override
82 public boolean equals( Object obj ) {
83 if (obj == this) return true;
84 if (obj instanceof CachePolicy) {
85 CachePolicy that = (CachePolicy)obj;
86 if (this.getTimeToLive() != that.getTimeToLive()) return false;
87 if (obj instanceof BasicCachePolicy) return true;
88 }
89 return false;
90 }
91
92 /**
93 * {@inheritDoc}
94 *
95 * @see java.lang.Object#toString()
96 */
97 @Override
98 public String toString() {
99 return "{ TTL=" + this.timeToLiveInMillis + " ms }";
100 }
101
102 }