Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StringValue.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
6
8{
12 public sealed class StringValue : SemiGroupElement
13 {
14 private static readonly StringValues associatedSemiGroup = new StringValues();
15 private static readonly CaseInsensitiveStringValues associatedSemiGroupCis = new CaseInsensitiveStringValues();
16
17 private string value;
18 private string valueLower = null;
19 private bool caseInsensitive;
20
25 public StringValue(string Value)
26 {
27 this.value = Value;
28 this.caseInsensitive = false;
29 }
30
36 public StringValue(string Value, bool CaseInsensitive)
37 {
38 this.value = Value;
39 this.caseInsensitive = CaseInsensitive;
40 }
41
45 public string Value
46 {
47 get => this.value;
48 set => this.value = value;
49 }
50
54 public bool CaseInsensitive
55 {
56 get => this.caseInsensitive;
57 set => this.caseInsensitive = value;
58 }
59
61 public override string ToString()
62 {
63 return Expression.ToString(this.value);
64 }
65
70 {
71 get
72 {
73 if (this.caseInsensitive)
74 return associatedSemiGroupCis;
75 else
76 return associatedSemiGroup;
77 }
78 }
79
83 public override object AssociatedObjectValue => this.value;
84
91 {
92 if (Element.IsScalar)
93 return new StringValue(Element.AssociatedObjectValue.ToString() + this.value, this.caseInsensitive);
94 else
95 {
96 LinkedList<IElement> Elements = new LinkedList<IElement>();
98
99 foreach (IElement E in Element.ChildElements)
100 {
101 SE = E as ISemiGroupElement;
102 if (SE is null)
103 Elements.AddLast(new StringValue(E.AssociatedObjectValue.ToString() + this.value, this.caseInsensitive));
104 else
105 Elements.AddLast(this.AddLeft(SE));
106 }
107
108 return (ISemiGroupElement)Element.Encapsulate(Elements, null);
109 }
110 }
111
118 {
119 if (Element.IsScalar)
120 return new StringValue(this.value + Element.AssociatedObjectValue.ToString(), this.caseInsensitive);
121 else
122 {
123 LinkedList<IElement> Elements = new LinkedList<IElement>();
125
126 foreach (IElement E in Element.ChildElements)
127 {
128 SE = E as ISemiGroupElement;
129 if (SE is null)
130 Elements.AddLast(new StringValue(this.value + E.AssociatedObjectValue.ToString(), this.caseInsensitive));
131 else
132 Elements.AddLast(this.AddRight(SE));
133 }
134
135 return (ISemiGroupElement)Element.Encapsulate(Elements, null);
136 }
137 }
138
140 public override bool Equals(object obj)
141 {
142 if (obj is StringValue S)
143 return string.Compare(this.value, S.value, this.caseInsensitive || S.caseInsensitive) == 0;
144
145 if (!(obj is IElement E))
146 return false;
147
148 if (!(E.AssociatedObjectValue is string s))
149 return false;
150
151 return string.Compare(this.value, s, this.caseInsensitive) == 0;
152 }
153
155 public override int GetHashCode()
156 {
157 if (this.caseInsensitive)
158 {
159 if (this.valueLower is null)
160 this.valueLower = this.value.ToLower();
161
162 return this.valueLower.GetHashCode();
163 }
164 else
165 return this.value.GetHashCode();
166 }
167
174 public override bool TryConvertTo(Type DesiredType, out object Value)
175 {
176 if (DesiredType == typeof(string))
177 {
178 Value = this.value;
179 return true;
180 }
181 else if (DesiredType == typeof(char))
182 {
183 if (this.value.Length == 1)
184 {
185 Value = this.value[0];
186 return true;
187 }
188 else
189 {
190 Value = null;
191 return false;
192 }
193 }
194 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(string).GetTypeInfo()))
195 {
196 Value = this.value;
197 return true;
198 }
199 else if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(StringValue).GetTypeInfo()))
200 {
201 Value = this;
202 return true;
203 }
204 else
205 return Expression.TryConvert(this.value, DesiredType, out Value);
206 }
207
211 public static readonly StringValue Empty = new StringValue(string.Empty);
212 }
213}
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
virtual bool IsScalar
If the element represents a scalar value.
Definition: Element.cs:54
virtual ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: Element.cs:62
virtual IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
Definition: Element.cs:72
Base class for all types of semigroup elements.
Class managing a script expression.
Definition: Expression.cs:39
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5268
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
Semi-group of case-insensitive string values.
override ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add an element to the current element, from the left.
Definition: StringValue.cs:90
override object AssociatedObjectValue
Associated object value.
Definition: StringValue.cs:83
static readonly StringValue Empty
The empty string.
Definition: StringValue.cs:211
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
Definition: StringValue.cs:174
StringValue(string Value)
String value.
Definition: StringValue.cs:25
string Value
String value.
Definition: StringValue.cs:46
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: StringValue.cs:140
bool CaseInsensitive
If the string value is case insensitive or not.
Definition: StringValue.cs:55
override ISemiGroup AssociatedSemiGroup
Associated Semi-Group.
Definition: StringValue.cs:70
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: StringValue.cs:155
override ISemiGroupElement AddRight(ISemiGroupElement Element)
Tries to add an element to the current element, from the right.
Definition: StringValue.cs:117
StringValue(string Value, bool CaseInsensitive)
String value.
Definition: StringValue.cs:36
Semi-group of string values.
Definition: StringValues.cs:11
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for all types of semigroup elements.
Basic interface for all types of semigroups.
Definition: ISemiGroup.cs:10