NTP Analyzer  0.8.2
Analyze the operation of time servers
PersistentObject.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 namespace Ntp.Analyzer.Objects
23 {
24  public abstract class PersistentObject
25  {
33  protected PersistentObject(int id)
34  {
35  Id = id;
36  NewObject = false;
37  }
38 
42  protected PersistentObject()
43  {
44  Id = -1;
45  NewObject = true;
46  }
47 
55  public int Id { get; private set; }
56 
64  public bool NewObject { get; protected set; }
65 
66  public override bool Equals(object obj)
67  {
68  if (obj == null)
69  return false;
70 
71  if (obj.GetType() != GetType())
72  return false;
73 
74  return NewObject
75  // ReSharper disable once BaseObjectEqualsIsObjectEquals
76  ? base.Equals(obj)
77  : Id == ((PersistentObject) obj).Id;
78  }
79 
80  public override int GetHashCode()
81  {
82  // ReSharper disable once NonReadonlyMemberInGetHashCode
83  return NewObject
84  // ReSharper disable once BaseObjectGetHashCodeCallInGetHashCode
85  ? base.GetHashCode()
86  // ReSharper disable once NonReadonlyMemberInGetHashCode
87  : (int) (Id*2654435761%2 ^ 32);
88  }
89 
94  public void SetId(int id)
95  {
96  Id = id;
97  NewObject = false;
98  }
99 
106  public override string ToString()
107  {
108  return $"[PersistentObject: Id={Id}, NewObject={NewObject}]";
109  }
110  }
111 }
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
PersistentObject(int id)
Initializes a new instance of the PersistentObject class.
override bool Equals(object obj)
override string ToString()
Returns a string that represents the current PersistentObject.
PersistentObject()
Initializes a new instance of the PersistentObject class.