KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > DefaultDataVersion


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.optimistic;
8
9 /**
10  * The default implementation of a DataVersion, uses a <code>long</code> to
11  * compare versions.
12  * This class is immutable.
13  * <p/>
14  * Also note that this is meant to control implicit, internal versioning. Do not attempt to instantiate or use instances
15  * of this class explicitly, via the {@link org.jboss.cache.config.Option#setDataVersion(DataVersion)} API, as it WILL
16  * break things.
17  *
18  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
19  */

20 public class DefaultDataVersion implements DataVersion
21 {
22    private static final long serialVersionUID = -6896315742831861046L;
23    /**
24     * Version zero.
25     * Assign this as the first version to your data.
26     */

27    public static final DataVersion ZERO = new DefaultDataVersion(0L);
28
29    /**
30     * Version one.
31     */

32    private static final DataVersion ONE = new DefaultDataVersion(1L);
33
34    /**
35     * Version two.
36     */

37    private static final DataVersion TWO = new DefaultDataVersion(2L);
38
39    private long version;
40
41    /**
42     * Constructs with version 0.
43     */

44    public DefaultDataVersion()
45    {
46    }
47
48    /**
49     * Constructs with a version number.
50     */

51    public DefaultDataVersion(long version)
52    {
53       this.version = version;
54    }
55
56    /**
57     * Returns a new DataVersion with a newer version number.
58     */

59    public DataVersion increment()
60    {
61       if (this == ZERO)
62       {
63          return ONE;
64       }
65       if (this == ONE)
66       {
67          return TWO;
68       }
69       return new DefaultDataVersion(version + 1);
70    }
71
72    public boolean newerThan(DataVersion other)
73    {
74       if (other instanceof DefaultDataVersion)
75       {
76          DefaultDataVersion dvOther = (DefaultDataVersion) other;
77          return version > dvOther.version;
78       }
79       return false;
80    }
81
82    public String JavaDoc toString()
83    {
84       return "Ver=" + version;
85    }
86
87    public boolean equals(Object JavaDoc other)
88    {
89       if (other instanceof DefaultDataVersion)
90       {
91          return version == ((DefaultDataVersion) other).version;
92       }
93       return false;
94    }
95
96    public int hashCode()
97    {
98       return (int) version;
99    }
100
101    public long getRawVersion()
102    {
103       return version;
104    }
105 }
106
Popular Tags