Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StringLiteral.cs
1using System;
2using System.Text;
5
7{
12 {
13 private readonly string language;
14
19 : base()
20 {
21 this.language = null;
22 }
23
28 public StringLiteral(string Value)
29 : base(Value, Value)
30 {
31 this.language = null;
32 }
33
39 public StringLiteral(string Value, string Language)
40 : base(Value, Value)
41 {
42 this.language = Language;
43 }
44
48 public const string TypeUri = XmlSchema.Namespace + "string";
49
53 public override string StringType => string.Empty;
54
60 public override Grade Supports(Type ValueType)
61 {
62 return ValueType == typeof(string) ? Grade.Ok : Grade.NotAtAll;
63 }
64
68 public string Language => this.language;
69
75 public override Grade Supports(string DataType)
76 {
77 return string.IsNullOrEmpty(DataType) || DataType == TypeUri ? Grade.Ok : Grade.NotAtAll;
78 }
79
87 public override ISemanticLiteral Parse(string Value, string DataType, string Language)
88 {
89 return new StringLiteral(Value, Language);
90 }
91
97 public override ISemanticLiteral Encapsulate(object Value)
98 {
99 return new StringLiteral(Value?.ToString() ?? string.Empty);
100 }
101
103 public override string ToString()
104 {
105 StringBuilder sb = new StringBuilder();
106
107 sb.Append('"');
108 sb.Append(JSON.Encode(this.StringValue));
109 sb.Append('"');
110
111 if (!string.IsNullOrEmpty(this.language))
112 {
113 sb.Append('@');
114 sb.Append(this.language);
115 }
116
117 return sb.ToString();
118 }
119
121 public override bool Equals(object obj)
122 {
123 return obj is StringLiteral Typed &&
124 Typed.StringValue == this.StringValue &&
125 string.Compare(Typed.language, this.language, true) == 0;
126 }
127
129 public override int GetHashCode()
130 {
131 int Result = this.StringValue.GetHashCode();
132 Result ^= Result << 5 ^ (this.language?.GetHashCode() ?? 0);
133 return Result;
134 }
135
147 public override int CompareTo(object obj)
148 {
149 if (obj is StringLiteral Typed)
150 {
151 int i = this.StringValue.CompareTo(Typed.StringValue);
152 if (i != 0)
153 return i;
154
155 return this.language?.CompareTo(Typed.language) ?? (Typed.language is null ? 0 : -1);
156 }
157
158 return base.CompareTo(obj);
159 }
160
161 }
162}
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
override ISemanticLiteral Parse(string Value, string DataType, string Language)
Tries to parse a string value of the type supported by the class..
StringLiteral(string Value, string Language)
Represents a string literal.
override ISemanticLiteral Encapsulate(object Value)
Encapsulates an object value as a semantic literal value.
override Grade Supports(string DataType)
How well the type supports a given data type.
override int CompareTo(object obj)
Compares the current instance with another object of the same type and returns an integer that indica...
StringLiteral(string Value)
Represents a string literal.
override Grade Supports(Type ValueType)
How well the type supports a given value type.
const string TypeUri
http://www.w3.org/2001/XMLSchema#string
Abstract base class for semantic literal values.
string StringValue
String representation of value.
Interface for semantic literals.
Grade
Grade enumeration
Definition: Grade.cs:7