Kinetica   C#   API  Version 7.2.3.0
EnumSchema.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 using Newtonsoft.Json.Linq;
22 
23 namespace Avro
24 {
28  public class EnumSchema : NamedSchema
29  {
33  public IList<string> Symbols { get; private set; }
34 
38  private readonly IDictionary<string, int> symbolMap;
39 
43  public int Count { get { return Symbols.Count; } }
44 
52  internal static EnumSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace)
53  {
54  SchemaName name = NamedSchema.GetName(jtok, encspace);
55  var aliases = NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);
56 
57  JArray jsymbols = jtok["symbols"] as JArray;
58  if (null == jsymbols)
59  throw new SchemaParseException("Enum has no symbols: " + name);
60 
61  List<string> symbols = new List<string>();
62  IDictionary<string, int> symbolMap = new Dictionary<string, int>();
63  int i = 0;
64  foreach (JValue jsymbol in jsymbols)
65  {
66  string s = (string)jsymbol.Value;
67  if (symbolMap.ContainsKey(s))
68  throw new SchemaParseException("Duplicate symbol: " + s);
69 
70  symbolMap[s] = i++;
71  symbols.Add(s);
72  }
73  return new EnumSchema(name, aliases, symbols, symbolMap, props, names);
74  }
75 
84  private EnumSchema(SchemaName name, IList<SchemaName> aliases, List<string> symbols,
85  IDictionary<String, int> symbolMap, PropertyMap props, SchemaNames names)
86  : base(Type.Enumeration, name, aliases, props, names)
87  {
88  if (null == name.Name) throw new SchemaParseException("name cannot be null for enum schema.");
89  this.Symbols = symbols;
90  this.symbolMap = symbolMap;
91  }
92 
99  protected internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer,
100  SchemaNames names, string encspace)
101  {
102  base.WriteJsonFields(writer, names, encspace);
103  writer.WritePropertyName("symbols");
104  writer.WriteStartArray();
105  foreach (string s in this.Symbols)
106  writer.WriteValue(s);
107  writer.WriteEndArray();
108  }
109 
116  public int Ordinal(string symbol)
117  {
118  int result;
119  if (symbolMap.TryGetValue(symbol, out result)) return result;
120  throw new AvroException("No such symbol: " + symbol);
121  }
122 
128  public string this[int index]
129  {
130  get
131  {
132  if (index < Symbols.Count) return Symbols[index];
133  throw new AvroException("Enumeration out of range. Must be less than " + Symbols.Count + ", but is " + index);
134  }
135  }
136 
142  public bool Contains(string symbol)
143  {
144  return symbolMap.ContainsKey(symbol);
145  }
146 
151  public IEnumerator<string> GetEnumerator()
152  {
153  return Symbols.GetEnumerator();
154  }
155 
161  public override bool Equals(object obj)
162  {
163  if (obj == this) return true;
164  if (obj != null && obj is EnumSchema)
165  {
166  EnumSchema that = obj as EnumSchema;
167  if (SchemaName.Equals(that.SchemaName) && Count == that.Count)
168  {
169  for (int i = 0; i < Count; i++) if (!Symbols[i].Equals(that.Symbols[i])) return false;
170  return areEqual(that.Props, this.Props);
171  }
172  }
173  return false;
174  }
175 
180  public override int GetHashCode()
181  {
182  int result = SchemaName.GetHashCode() + getHashCode(Props);
183  foreach (string s in Symbols) result += 23 * s.GetHashCode();
184  return result;
185  }
186 
192  public override bool CanRead(Schema writerSchema)
193  {
194  if (writerSchema.Tag != Tag) return false;
195 
196  EnumSchema that = writerSchema as EnumSchema;
197  if (!that.SchemaName.Equals(SchemaName))
198  if (!InAliases(that.SchemaName)) return false;
199 
200  // we defer checking of symbols. Writer may have a symbol missing from the reader,
201  // but if writer never used the missing symbol, then reader should still be able to read the data
202 
203  return true;
204  }
205  }
206 }
static SchemaName GetName(JToken jtok, string encspace)
Parses the name and namespace from the given JSON schema object then creates SchemaName object includ...
Definition: NamedSchema.cs:116
IList< string > Symbols
List of strings representing the enum symbols
Definition: EnumSchema.cs:33
static int getHashCode(object obj)
Hash code helper function
Definition: Schema.cs:301
static IList< SchemaName > GetAliases(JToken jtok, string space, string encspace)
Parses the 'aliases' property from the given JSON token
Definition: NamedSchema.cs:130
override int GetHashCode()
Hashcode function
Definition: EnumSchema.cs:180
override bool CanRead(Schema writerSchema)
Checks if this schema can read data written by the given schema.
Definition: EnumSchema.cs:192
Class for enum type schemas
Definition: EnumSchema.cs:28
int Count
Count of enum symbols
Definition: EnumSchema.cs:43
bool InAliases(SchemaName name)
Definition: NamedSchema.cs:150
Base class for all schema types
Definition: Schema.cs:29
bool Contains(string symbol)
Checks if given symbol is in the list of enum symbols
Definition: EnumSchema.cs:142
Type
Enum for schema types
Definition: Schema.cs:34
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
int Ordinal(string symbol)
Returns the position of the given symbol within this enum.
Definition: EnumSchema.cs:116
Class to store schema name, namespace and enclosing namespace
Definition: SchemaName.cs:27
internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes enum schema in JSON format
Definition: EnumSchema.cs:99
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
override int GetHashCode()
Definition: SchemaName.cs:136
override bool Equals(object obj)
Checks equality of two enum schema
Definition: EnumSchema.cs:161
override bool Equals(Object obj)
Compares two schema names
Definition: SchemaName.cs:114
IEnumerator< string > GetEnumerator()
Returns an enumerator that enumerates the symbols in this enum schema in the order of their definitio...
Definition: EnumSchema.cs:151
String EncSpace
Namespace from the most tightly enclosing schema
Definition: SchemaName.cs:42
static bool areEqual(object o1, object o2)
Compares two objects, null is equal to null
Definition: Schema.cs:291
String Space
Namespace specified within the schema
Definition: SchemaName.cs:37
Type Tag
Schema type property
Definition: Schema.cs:56
Base class for all named schemas: fixed, enum, record
Definition: NamedSchema.cs:29
SchemaName SchemaName
Name of the schema, contains name, namespace and enclosing namespace
Definition: NamedSchema.cs:34