agones.dev/agones@v1.53.0/sdks/unity/model/GameServerObjectMeta.cs (about)

     1  // Copyright 2019 Google LLC
     2  // All Rights Reserved.
     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  
    16  using System;
    17  using System.Collections.Generic;
    18  using System.Linq;
    19  using System.Text;
    20  
    21  namespace Agones.Model
    22  {
    23      /// <summary>
    24      /// GameServerObjectMeta
    25      /// </summary>
    26      public class GameServerObjectMeta : IEquatable<GameServerObjectMeta>
    27      {
    28          /// <summary>
    29          /// Initializes a new instance of the <see cref="GameServerObjectMeta" /> class.
    30          /// </summary>
    31          public GameServerObjectMeta(IReadOnlyDictionary<string, object> data)
    32          {
    33              this.Name = (string) data["name"];
    34              this.Namespace = (string) data["namespace"];
    35              this.Uid = (string) data["uid"];
    36              this.ResourceVersion = (string) data["resource_version"];
    37              this.Generation = Int32.Parse((string) data["generation"]);
    38              this.CreationTimestamp =
    39                  DateTimeOffset.FromUnixTimeSeconds(long.Parse((string) data["creation_timestamp"])).DateTime;
    40  
    41              if (data.TryGetValue("deletion_timestamp", out var timestamp))
    42              {
    43                  this.DeletionTimestamp =
    44                      DateTimeOffset.FromUnixTimeSeconds(long.Parse((string) timestamp)).DateTime;
    45              }
    46  
    47              if (data.TryGetValue("annotations", out var annotations))
    48              {
    49                  this.Annotations = new Dictionary<string, string>();
    50                  var values = (Dictionary<string, object>) annotations;
    51                  foreach (var item in values)
    52                  {
    53                      this.Annotations.Add(item.Key, item.Value.ToString());
    54                  }
    55              }
    56  
    57              if (data.TryGetValue("labels", out var labels))
    58              {
    59                  this.Labels = new Dictionary<string, string>();
    60                  var values = (Dictionary<string, object>) labels;
    61                  foreach (var item in values)
    62                  {
    63                      this.Labels.Add(item.Key, item.Value.ToString());
    64                  }
    65              }
    66          }
    67  
    68          public string Name { get; }
    69          public string Namespace { get; }
    70          public string Uid { get; }
    71          public string ResourceVersion { get; }
    72          public int Generation { get; }
    73          public DateTime CreationTimestamp { get; }
    74          public DateTime? DeletionTimestamp { get; }
    75          public Dictionary<string, string> Annotations { get; }
    76          public Dictionary<string, string> Labels { get; }
    77  
    78          /// <summary>
    79          /// Returns the string presentation of the object
    80          /// </summary>
    81          /// <returns>String presentation of the object</returns>
    82          public override string ToString()
    83          {
    84              var sb = new StringBuilder();
    85              sb.Append("class GameServerObjectMeta {\n");
    86              sb.Append("  Name: ").Append(Name).Append("\n");
    87              sb.Append("  Namespace: ").Append(Namespace).Append("\n");
    88              sb.Append("  Uid: ").Append(Uid).Append("\n");
    89              sb.Append("  ResourceVersion: ").Append(ResourceVersion).Append("\n");
    90              sb.Append("  Generation: ").Append(Generation).Append("\n");
    91              sb.Append("  CreationTimestamp: ").Append(CreationTimestamp).Append("\n");
    92              sb.Append("  DeletionTimestamp: ").Append(DeletionTimestamp).Append("\n");
    93              sb.Append("  Annotations: ").Append(string.Join(";", Annotations)).Append("\n");
    94              sb.Append("  Labels: ").Append(string.Join(";", Labels)).Append("\n");
    95              sb.Append("}\n");
    96              return sb.ToString();
    97          }
    98  
    99          /// <summary>
   100          /// Returns true if objects are equal
   101          /// </summary>
   102          /// <param name="input">Object to be compared</param>
   103          /// <returns>Boolean</returns>
   104          public override bool Equals(object input)
   105          {
   106              return this.Equals(input as GameServerObjectMeta);
   107          }
   108  
   109          /// <summary>
   110          /// Returns true if GameServerObjectMeta instances are equal
   111          /// </summary>
   112          /// <param name="input">Instance of GameServerObjectMeta to be compared</param>
   113          /// <returns>Boolean</returns>
   114          public bool Equals(GameServerObjectMeta input)
   115          {
   116              if (input == null)
   117                  return false;
   118  
   119              return
   120                  (
   121                      this.Name == input.Name ||
   122                      (this.Name != null &&
   123                       this.Name.Equals(input.Name))
   124                  ) &&
   125                  (
   126                      this.Namespace == input.Namespace ||
   127                      (this.Namespace != null &&
   128                       this.Namespace.Equals(input.Namespace))
   129                  ) &&
   130                  (
   131                      this.Uid == input.Uid ||
   132                      (this.Uid != null &&
   133                       this.Uid.Equals(input.Uid))
   134                  ) &&
   135                  (
   136                      this.ResourceVersion == input.ResourceVersion ||
   137                      (this.ResourceVersion != null &&
   138                       this.ResourceVersion.Equals(input.ResourceVersion))
   139                  ) &&
   140                  (
   141                      this.Generation == input.Generation ||
   142                      (this.Generation.Equals(input.Generation))
   143                  ) &&
   144                  (
   145                      this.CreationTimestamp == input.CreationTimestamp ||
   146                      (this.CreationTimestamp.Equals(input.CreationTimestamp))
   147                  ) &&
   148                  (
   149                      this.DeletionTimestamp == input.DeletionTimestamp ||
   150                      (this.DeletionTimestamp != null &&
   151                       this.DeletionTimestamp.Equals(input.DeletionTimestamp))
   152                  ) &&
   153                  (
   154                      this.Annotations == input.Annotations ||
   155                      this.Annotations != null &&
   156                      this.Annotations.SequenceEqual(input.Annotations)
   157                  ) &&
   158                  (
   159                      this.Labels == input.Labels ||
   160                      this.Labels != null &&
   161                      this.Labels.SequenceEqual(input.Labels)
   162                  );
   163          }
   164  
   165          /// <summary>
   166          /// Gets the hash code
   167          /// </summary>
   168          /// <returns>Hash code</returns>
   169          public override int GetHashCode()
   170          {
   171              unchecked // Overflow is fine, just wrap
   172              {
   173                  int hashCode = 41;
   174                  if (this.Name != null)
   175                      hashCode = hashCode * 59 + this.Name.GetHashCode();
   176                  if (this.Namespace != null)
   177                      hashCode = hashCode * 59 + this.Namespace.GetHashCode();
   178                  if (this.Uid != null)
   179                      hashCode = hashCode * 59 + this.Uid.GetHashCode();
   180                  if (this.ResourceVersion != null)
   181                      hashCode = hashCode * 59 + this.ResourceVersion.GetHashCode();
   182                  hashCode = hashCode * 59 + this.Generation.GetHashCode();
   183                  hashCode = hashCode * 59 + this.CreationTimestamp.GetHashCode();
   184                  if (this.DeletionTimestamp != null)
   185                      hashCode = hashCode * 59 + this.DeletionTimestamp.GetHashCode();
   186                  if (this.Annotations != null)
   187                      hashCode = hashCode * 59 + this.Annotations.GetHashCode();
   188                  if (this.Labels != null)
   189                      hashCode = hashCode * 59 + this.Labels.GetHashCode();
   190                  return hashCode;
   191              }
   192          }
   193      }
   194  }