blob: 3f3761a039cc9aaa454ceb99d2d0d0bca0b9a37f [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.skyframe;
15
Janak Ramakrishnane1412862016-01-27 21:58:30 +000016import com.google.common.collect.Interner;
Nathan Harmataa16d9f12016-11-23 20:58:07 +000017import com.google.devtools.build.lib.concurrent.BlazeInterners;
Janak Ramakrishnane1412862016-01-27 21:58:30 +000018
Janak Ramakrishnan3a19bda2016-02-12 00:08:34 +000019/** Versioning scheme based on integers. */
20final class IntVersion implements Version {
Nathan Harmataa16d9f12016-11-23 20:58:07 +000021 private static final Interner<IntVersion> interner = BlazeInterners.newWeakInterner();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010022
23 private final long val;
24
Janak Ramakrishnane1412862016-01-27 21:58:30 +000025 private IntVersion(long val) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026 this.val = val;
27 }
28
Janak Ramakrishnan3a19bda2016-02-12 00:08:34 +000029 long getVal() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030 return val;
31 }
32
Janak Ramakrishnan3a19bda2016-02-12 00:08:34 +000033 IntVersion next() {
Janak Ramakrishnane1412862016-01-27 21:58:30 +000034 return of(val + 1);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035 }
36
Janak Ramakrishnan3a19bda2016-02-12 00:08:34 +000037 static IntVersion of(long val) {
Janak Ramakrishnane1412862016-01-27 21:58:30 +000038 return interner.intern(new IntVersion(val));
Janak Ramakrishnan1c9c98e2015-11-20 23:44:15 +000039 }
40
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041 @Override
42 public boolean atMost(Version other) {
43 if (!(other instanceof IntVersion)) {
44 return false;
45 }
46 return val <= ((IntVersion) other).val;
47 }
48
49 @Override
50 public int hashCode() {
51 return Long.valueOf(val).hashCode();
52 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (obj instanceof IntVersion) {
57 IntVersion other = (IntVersion) obj;
58 return other.val == val;
59 }
60 return false;
61 }
62
63 @Override
64 public String toString() {
65 return "IntVersion: " + val;
66 }
67}