Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CustomLiteral.cs
1using System;
2using System.Text;
4
6{
11 {
12 private readonly string language;
13
18 : this(null, null)
19 {
20 }
21
27 public CustomLiteral(string Value, string Type)
28 : base(Value, Value)
29 {
30 this.Type = Type;
31 this.language = null;
32 }
33
40 public CustomLiteral(string Value, string Type, string Language)
41 : base(Value, Value)
42 {
43 this.Type = Type;
44 this.language = Language;
45 }
46
50 public string Type { get; set; }
51
55 public override string StringType => this.Type;
56
60 public string Language => this.language;
61
67 public override Grade Supports(Type ValueType)
68 {
69 return Grade.NotAtAll;
70 }
71
79 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
80 {
81 return new CustomLiteral(Value, DataType, Language);
82 }
83
89 public override ISemanticLiteral Encapsulate(object Value)
90 {
91 return new StringLiteral(Value?.ToString() ?? string.Empty);
92 }
93
95 public override string ToString()
96 {
97 StringBuilder sb = new StringBuilder();
98
99 sb.Append('"');
100 sb.Append(JSON.Encode(this.StringValue));
101 sb.Append('"');
102
103 if (!string.IsNullOrEmpty(this.language))
104 {
105 sb.Append('@');
106 sb.Append(this.language);
107 }
108
109 sb.Append("^^<");
110 sb.Append(this.StringType);
111 sb.Append('>');
112
113 return sb.ToString();
114 }
115
117 public override bool Equals(object obj)
118 {
119 return obj is CustomLiteral Typed &&
120 Typed.StringValue == this.StringValue &&
121 Typed.StringType == this.StringType &&
122 Typed.language == this.language;
123 }
124
126 public override int GetHashCode()
127 {
128 int Result = this.StringValue.GetHashCode();
129 Result ^= Result << 5 ^ this.StringType.GetHashCode();
130 Result ^= Result << 5 ^ (this.language?.GetHashCode() ?? 0);
131 return Result;
132 }
133
145 public override int CompareTo(object obj)
146 {
147 if (obj is CustomLiteral Typed)
148 {
149 int i = this.StringValue.CompareTo(Typed.StringValue);
150 if (i != 0)
151 return i;
152
153 i = this.StringType.CompareTo(Typed.StringType);
154 if (i != 0)
155 return i;
156
157 return this.language?.CompareTo(Typed.language) ?? (Typed.language is null ? 0 : -1);
158 }
159
160 return base.CompareTo(obj);
161 }
162 }
163}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
CustomLiteral(string Value, string Type, string Language)
Represents a custom literal.
CustomLiteral(string Value, string Type)
Represents a custom literal.
override ISemanticLiteral Parse(string Value, string DataType, string Language)
Tries to parse a string value of the type supported by the class..
override ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
override Grade Supports(Type ValueType)
How well the type supports a given value type.
override int CompareTo(object obj)
Compares the current instance with another object of the same type and returns an integer that indica...
Abstract base class for semantic literal values.
string StringValue
String representation of value.
Interface for semantic literals.
Grade
Grade enumeration
Definition: Grade.cs:7