/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
namespace Avro
{
///
/// Class for schemas of primitive types
///
public sealed class PrimitiveSchema : UnnamedSchema
{
///
/// Constructor for primitive schema
///
///
private PrimitiveSchema(Type type, PropertyMap props) : base(type, props)
{
}
///
/// Static function to return new instance of primitive schema
///
/// primitive type
///
public static PrimitiveSchema NewInstance(string type, PropertyMap props = null)
{
const string q = "\"";
if (type.StartsWith(q) && type.EndsWith(q)) type = type.Substring(1, type.Length - 2);
switch (type)
{
case "null":
return new PrimitiveSchema(Schema.Type.Null, props);
case "boolean":
return new PrimitiveSchema(Schema.Type.Boolean, props);
case "int":
return new PrimitiveSchema(Schema.Type.Int, props);
case "long":
return new PrimitiveSchema(Schema.Type.Long, props);
case "float":
return new PrimitiveSchema(Schema.Type.Float, props);
case "double":
return new PrimitiveSchema(Schema.Type.Double, props);
case "bytes":
return new PrimitiveSchema(Schema.Type.Bytes, props);
case "string":
return new PrimitiveSchema(Schema.Type.String, props);
default:
return null;
}
}
///
/// Writes primitive schema in JSON format
///
///
///
///
protected internal override void WriteJson(JsonTextWriter w, SchemaNames names, string encspace)
{
w.WriteValue(Name);
}
///
/// Checks if this schema can read data written by the given schema. Used for decoding data.
///
/// writer schema
/// true if this and writer schema are compatible based on the AVRO specification, false otherwise
public override bool CanRead(Schema writerSchema)
{
if (writerSchema is UnionSchema || Tag == writerSchema.Tag) return true;
Type t = writerSchema.Tag;
switch (Tag)
{
case Type.Double:
return t == Type.Int || t == Type.Long || t == Type.Float;
case Type.Float:
return t == Type.Int || t == Type.Long;
case Type.Long:
return t == Type.Int;
default:
return false;
}
}
///
/// Function to compare equality of two primitive schemas
///
/// other primitive schema
/// true two schemas are equal, false otherwise
public override bool Equals(object obj)
{
if (this == obj) return true;
if (obj != null && obj is PrimitiveSchema)
{
var that = obj as PrimitiveSchema;
if (this.Tag == that.Tag)
return areEqual(that.Props, this.Props);
}
return false;
}
///
/// Hashcode function
///
///
public override int GetHashCode()
{
return 13 * Tag.GetHashCode() + getHashCode(Props);
}
}
}