Kinetica   C#   API  Version 7.2.3.0
PrimitiveSchema.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;
22 
23 namespace Avro
24 {
28  public sealed class PrimitiveSchema : UnnamedSchema
29  {
34  private PrimitiveSchema(Type type, PropertyMap props) : base(type, props)
35  {
36  }
37 
43  public static PrimitiveSchema NewInstance(string type, PropertyMap props = null)
44  {
45  const string q = "\"";
46  if (type.StartsWith(q) && type.EndsWith(q)) type = type.Substring(1, type.Length - 2);
47  switch (type)
48  {
49  case "null":
50  return new PrimitiveSchema(Schema.Type.Null, props);
51  case "boolean":
52  return new PrimitiveSchema(Schema.Type.Boolean, props);
53  case "int":
54  return new PrimitiveSchema(Schema.Type.Int, props);
55  case "long":
56  return new PrimitiveSchema(Schema.Type.Long, props);
57  case "float":
58  return new PrimitiveSchema(Schema.Type.Float, props);
59  case "double":
60  return new PrimitiveSchema(Schema.Type.Double, props);
61  case "bytes":
62  return new PrimitiveSchema(Schema.Type.Bytes, props);
63  case "string":
64  return new PrimitiveSchema(Schema.Type.String, props);
65  default:
66  return null;
67  }
68  }
69 
76  protected internal override void WriteJson(JsonTextWriter w, SchemaNames names, string encspace)
77  {
78  w.WriteValue(Name);
79  }
80 
86  public override bool CanRead(Schema writerSchema)
87  {
88  if (writerSchema is UnionSchema || Tag == writerSchema.Tag) return true;
89  Type t = writerSchema.Tag;
90  switch (Tag)
91  {
92  case Type.Double:
93  return t == Type.Int || t == Type.Long || t == Type.Float;
94  case Type.Float:
95  return t == Type.Int || t == Type.Long;
96  case Type.Long:
97  return t == Type.Int;
98  default:
99  return false;
100  }
101  }
102 
108  public override bool Equals(object obj)
109  {
110  if (this == obj) return true;
111 
112  if (obj != null && obj is PrimitiveSchema)
113  {
114  var that = obj as PrimitiveSchema;
115  if (this.Tag == that.Tag)
116  return areEqual(that.Props, this.Props);
117  }
118  return false;
119  }
120 
125  public override int GetHashCode()
126  {
127  return 13 * Tag.GetHashCode() + getHashCode(Props);
128  }
129  }
130 }
static int getHashCode(object obj)
Hash code helper function
Definition: Schema.cs:301
Base class for all unnamed schemas
Base class for all schema types
Definition: Schema.cs:29
Class for union schemas
Definition: UnionSchema.cs:29
override int GetHashCode()
Hashcode function
Type
Enum for schema types
Definition: Schema.cs:34
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Class for schemas of primitive types
static PrimitiveSchema NewInstance(string type, PropertyMap props=null)
Static function to return new instance of primitive schema
override string Name
internal override void WriteJson(JsonTextWriter w, SchemaNames names, string encspace)
Writes primitive schema in JSON format
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
override bool CanRead(Schema writerSchema)
Checks if this schema can read data written by the given schema.
static bool areEqual(object o1, object o2)
Compares two objects, null is equal to null
Definition: Schema.cs:291
override bool Equals(object obj)
Function to compare equality of two primitive schemas
Type Tag
Schema type property
Definition: Schema.cs:56