Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SourceState.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4
6{
10 public class SourceState
11 {
12 private readonly List<DocumentInformation> documents = new List<DocumentInformation>();
13 private readonly string source;
14 private DocumentType type = DocumentType.Empty;
15 private string firstText = null;
16 private int nrDocuments = 0;
17 private readonly bool isDefault;
18
24 public SourceState(string Source, bool IsDefault)
25 {
26 this.source = Source;
27 this.isDefault = IsDefault;
28 }
29
33 public string Source => this.source;
34
38 public bool IsDefault => this.isDefault;
39
43 public async Task<string> GetFirstText()
44 {
45 if (!(this.firstText is null))
46 return this.firstText;
47
49 if (Doc is null)
50 return string.Empty;
51
52 this.firstText = await Doc.GenerateMarkdown(false);
53
54 return this.firstText;
55 }
56
61 {
62 get
63 {
64 lock (this.documents)
65 {
66 if (this.documents.Count == 0)
67 return null;
68 else
69 return this.documents[0];
70 }
71 }
72 }
73
78 {
79 get
80 {
81 lock (this.documents)
82 {
83 return this.documents.ToArray();
84 }
85 }
86 }
87
94 public async Task<DocumentType> Add(MarkdownDocument Markdown, string Id)
95 {
96 DocumentInformation Info = await DocumentInformation.CreateAsync(Markdown, Id);
97
98 lock (this.documents)
99 {
100 this.documents.Add(Info);
101 this.nrDocuments++;
102
103 if (this.nrDocuments == 1)
104 this.type = Info.Type;
105 else
106 this.type = DocumentType.Complex;
107
108 return this.type;
109 }
110 }
111
118 public async Task<DocumentType> Update(MarkdownDocument Markdown, string Id)
119 {
120 if (string.IsNullOrEmpty(Id))
121 return await this.Add(Markdown, Id);
122
123 DocumentInformation Info = await DocumentInformation.CreateAsync(Markdown, Id);
125 int i, c;
126
127 lock (this.documents)
128 {
129 c = this.documents.Count;
130 for (i = 0; i < c; i++)
131 {
132 Info2 = this.documents[i];
133 if (Info2.Id == Id)
134 {
135 this.documents[i] = Info;
136
137 if (this.nrDocuments == 1)
138 this.type = Info.Type;
139
140 return this.type;
141 }
142 }
143
144 this.documents.Add(Info);
145 this.nrDocuments++;
146
147 if (this.nrDocuments == 1)
148 this.type = Info.Type;
149 else
150 this.type = DocumentType.Complex;
151
152 return this.type;
153 }
154 }
155 }
156}
static async Task< DocumentInformation > CreateAsync(MarkdownDocument Markdown, string Id)
Information about a document.
Maintains the state of one source.
Definition: SourceState.cs:11
DocumentInformation[] Documents
Available documents from source.
Definition: SourceState.cs:78
SourceState(string Source, bool IsDefault)
Maintains the state of one source.
Definition: SourceState.cs:24
async Task< string > GetFirstText()
First document text.
Definition: SourceState.cs:43
DocumentInformation FirstDocument
First document.
Definition: SourceState.cs:61
async Task< DocumentType > Add(MarkdownDocument Markdown, string Id)
Adds a markdown document from the source.
Definition: SourceState.cs:94
async Task< DocumentType > Update(MarkdownDocument Markdown, string Id)
Updates a markdown document from the source. If not found, it is added.
Definition: SourceState.cs:118
bool IsDefault
If the content is default content (true), or reported content (false).
Definition: SourceState.cs:38
Contains a markdown document. This markdown document class supports original markdown,...
Task< string > GenerateMarkdown()
Generates Markdown from the markdown text.
DocumentType
Type of markdown document.