blob: 682d0ae79937724a32bd06660d93bef9ed41e671 [file] [log] [blame]
Madan Jampani05833872016-07-12 23:01:39 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani05833872016-07-12 23:01:39 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.core;
17
18import com.google.common.base.MoreObjects;
19
20/**
21 * Time provided by a Hybrid Logical Clock described in
22 * this <a href="http://www.cse.buffalo.edu/tech-reports/2014-04.pdf">paper</a>.
23 */
24public class HybridLogicalTime {
25 private final long logicalTime;
26 private final long logicalCounter;
27
28 public HybridLogicalTime(long logicalTime, long logicalCounter) {
29 this.logicalTime = logicalTime;
30 this.logicalCounter = logicalCounter;
31 }
32
33 /**
34 * Returns the logical time component of a HLT.
35 * @return logical time
36 */
37 public long logicalTime() {
38 return logicalTime;
39 }
40
41 /**
42 * Returns the logical counter component of a HLT.
43 * @return logical counter
44 */
45 public long logicalCounter() {
46 return logicalCounter;
47 }
48
49 /**
50 * Returns the real system time represented by this HLT.
51 * @return real system time
52 */
53 public long time() {
54 return (logicalTime >> 16 << 16) | (logicalCounter << 48 >> 48);
55 }
56
57 @Override
58 public String toString() {
59 return MoreObjects.toStringHelper(getClass())
60 .add("logicalTime", logicalTime)
61 .add("logicalCounter", logicalCounter)
62 .toString();
63 }
64}