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 : base(null, null)
19 {
20 this.Type = null;
21 this.language = null;
22 }
23
29 public CustomLiteral(string Value, string Type)
30 : base(Value, Value)
31 {
32 this.Type = Type;
33 this.language = null;
34 }
35
41 public CustomLiteral(string Value, Uri Type)
42 : base(Value, Value)
43 {
44 this.Type = Type.AbsoluteUri;
45 this.language = null;
46 }
47
54 public CustomLiteral(string Value, string Type, string Language)
55 : base(Value, Value)
56 {
57 this.Type = Type;
58 this.language = Language;
59 }
60
67 public CustomLiteral(string Value, Uri Type, string Language)
68 : base(Value, Value)
69 {
70 this.Type = Type.AbsoluteUri;
71 this.language = Language;
72 }
73
77 public string Type { get; set; }
78
82 public override string StringType => this.Type;
83
87 public string Language => this.language;
88
94 public override Grade Supports(Type ValueType)
95 {
96 return Grade.NotAtAll;
97 }
98
106 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
107 {
108 return new CustomLiteral(Value, DataType, Language);
109 }
110
116 public override ISemanticLiteral Encapsulate(object Value)
117 {
118 return new StringLiteral(Value?.ToString() ?? string.Empty);
119 }
120
122 public override string ToString()
123 {
124 return ToString(this.StringValue, this.language, this.StringType);
125 }
126
134 public static string ToString(string StringValue, string Language, string StringType)
135 {
136 StringBuilder sb = new StringBuilder();
137
138 sb.Append('"');
139 sb.Append(JSON.Encode(StringValue));
140 sb.Append('"');
141
142 if (!string.IsNullOrEmpty(Language))
143 {
144 sb.Append('@');
145 sb.Append(Language);
146 }
147
148 sb.Append("^^<");
149 sb.Append(StringType);
150 sb.Append('>');
151
152 return sb.ToString();
153 }
154
156 public override bool Equals(object obj)
157 {
158 return obj is CustomLiteral Typed &&
159 Typed.StringValue == this.StringValue &&
160 Typed.StringType == this.StringType &&
161 Typed.language == this.language;
162 }
163
165 public override int GetHashCode()
166 {
167 int Result = this.StringValue.GetHashCode();
168 Result ^= Result << 5 ^ this.StringType.GetHashCode();
169 Result ^= Result << 5 ^ (this.language?.GetHashCode() ?? 0);
170 return Result;
171 }
172
184 public override int CompareTo(object obj)
185 {
186 if (obj is CustomLiteral Typed)
187 {
188 int i = this.StringValue.CompareTo(Typed.StringValue);
189 if (i != 0)
190 return i;
191
192 i = this.StringType.CompareTo(Typed.StringType);
193 if (i != 0)
194 return i;
195
196 return this.language?.CompareTo(Typed.language) ?? (Typed.language is null ? 0 : -1);
197 }
198
199 return base.CompareTo(obj);
200 }
201 }
202}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
CustomLiteral(string Value, string Type, string Language)
Represents a custom literal.
CustomLiteral(string Value, Uri 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..
static string ToString(string StringValue, string Language, string StringType)
Outputs a string-representation of a custom literal.
override ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
CustomLiteral(string Value, Uri Type)
Represents a custom literal.
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