Kinetica   C#   API  Version 7.2.3.0
Property.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using Newtonsoft.Json.Linq;
23 using Newtonsoft.Json;
24 
25 namespace Avro
26 {
27  public class PropertyMap : Dictionary<string, string>
28  {
32  private static readonly HashSet<string> ReservedProps = new HashSet<string>() { "type", "name", "namespace", "fields", "items", "size", "symbols", "values", "aliases", "order", "doc", "default" };
33 
39  public void Parse(JToken jtok)
40  {
41  JObject jo = jtok as JObject;
42  foreach (JProperty prop in jo.Properties())
43  {
44  if (ReservedProps.Contains(prop.Name))
45  continue;
46  if (!ContainsKey(prop.Name))
47  Add(prop.Name, prop.Value.ToString());
48  }
49  }
50 
56  public void Set(string key, string value)
57  {
58  if (ReservedProps.Contains(key))
59  throw new AvroException("Can't set reserved property: " + key);
60 
61  string oldValue;
62  if (TryGetValue(key, out oldValue))
63  {
64  if (!oldValue.Equals(value)) throw new AvroException("Property cannot be overwritten: " + key);
65  }
66  else
67  Add(key, value);
68  }
69 
74  public void WriteJson(JsonTextWriter writer)
75  {
76  foreach (KeyValuePair<string, string> kp in this)
77  {
78  if (ReservedProps.Contains(kp.Key)) continue;
79 
80  writer.WritePropertyName(kp.Key);
81  writer.WriteRawValue(kp.Value);
82  }
83  }
84 
90  public override bool Equals(object obj)
91  {
92  if (this == obj) return true;
93 
94  if (obj != null && obj is PropertyMap)
95  {
96  var that = obj as PropertyMap;
97  if (this.Count != that.Count)
98  return false;
99  foreach (KeyValuePair<string, string> pair in this)
100  {
101  if (!that.ContainsKey(pair.Key))
102  return false;
103  if (!pair.Value.Equals(that[pair.Key]))
104  return false;
105  }
106  return true;
107  }
108  return false;
109  }
110 
115  public override int GetHashCode()
116  {
117  int hash = this.Count;
118  int index = 1;
119  foreach (KeyValuePair<string, string> pair in this)
120  hash += (pair.Key.GetHashCode() + pair.Value.GetHashCode()) * index++;
121  return hash;
122  }
123  }
124 }
void WriteJson(JsonTextWriter writer)
Writes the schema's custom properties in JSON format
Definition: Property.cs:74
void Set(string key, string value)
Adds a custom property to the schema
Definition: Property.cs:56
override bool Equals(object obj)
Function to compare equality of two PropertyMaps
Definition: Property.cs:90
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
override int GetHashCode()
Hashcode function
Definition: Property.cs:115
void Parse(JToken jtok)
Parses the custom properties from the given JSON object and stores them into the schema's list of cus...
Definition: Property.cs:39