Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
Jonathan Hart | 63939a3 | 2015-05-08 11:57:03 -0700 | [diff] [blame] | 16 | package org.onosproject.store.service; |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.store.Timestamp; |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | import com.google.common.collect.ComparisonChain; |
| 26 | |
| 27 | /** |
| 28 | * A Timestamp that derives its value from the prevailing |
| 29 | * wallclock time on the controller where it is generated. |
| 30 | */ |
| 31 | public class WallClockTimestamp implements Timestamp { |
| 32 | |
| 33 | private final long unixTimestamp; |
| 34 | |
| 35 | public WallClockTimestamp() { |
| 36 | unixTimestamp = System.currentTimeMillis(); |
| 37 | } |
| 38 | |
Brian O'Connor | a6c9b5c | 2015-04-29 22:38:29 -0700 | [diff] [blame] | 39 | public WallClockTimestamp(long timestamp) { |
| 40 | unixTimestamp = timestamp; |
| 41 | } |
| 42 | |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 43 | @Override |
| 44 | public int compareTo(Timestamp o) { |
| 45 | checkArgument(o instanceof WallClockTimestamp, |
| 46 | "Must be WallClockTimestamp", o); |
| 47 | WallClockTimestamp that = (WallClockTimestamp) o; |
| 48 | |
| 49 | return ComparisonChain.start() |
| 50 | .compare(this.unixTimestamp, that.unixTimestamp) |
| 51 | .result(); |
| 52 | } |
| 53 | @Override |
| 54 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 55 | return Long.hashCode(unixTimestamp); |
Madan Jampani | fd26ffb | 2014-10-13 14:08:55 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean equals(Object obj) { |
| 60 | if (this == obj) { |
| 61 | return true; |
| 62 | } |
| 63 | if (!(obj instanceof WallClockTimestamp)) { |
| 64 | return false; |
| 65 | } |
| 66 | WallClockTimestamp that = (WallClockTimestamp) obj; |
| 67 | return Objects.equals(this.unixTimestamp, that.unixTimestamp); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String toString() { |
| 72 | return MoreObjects.toStringHelper(getClass()) |
| 73 | .add("unixTimestamp", unixTimestamp) |
| 74 | .toString(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the unixTimestamp. |
| 79 | * |
| 80 | * @return unix timestamp |
| 81 | */ |
| 82 | public long unixTimestamp() { |
| 83 | return unixTimestamp; |
| 84 | } |
| 85 | } |