Skip to content

Commit 251f15e

Browse files
committed
Improve docs.
1 parent 141e9f6 commit 251f15e

File tree

4 files changed

+97
-74
lines changed

4 files changed

+97
-74
lines changed

Linguini.Bundle/ConcurrentBundle.cs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,33 @@
1313

1414
namespace Linguini.Bundle
1515
{
16+
/// <summary>
17+
/// Represents a thread-safe implementation of the FluentBundle class.
18+
///
19+
/// `ConcurrentBundle` implements the <see cref="IReadBundle"/> interface.
20+
/// </summary>
1621
public sealed class ConcurrentBundle : FluentBundle, IEquatable<ConcurrentBundle>
1722
{
1823
internal ConcurrentDictionary<string, FluentFunction> Functions = new();
19-
private ConcurrentDictionary<string, AstTerm> _terms = new();
20-
private ConcurrentDictionary<string, AstMessage> _messages = new();
21-
22-
public static ConcurrentBundle Thaw(FrozenBundle frozenBundle)
23-
{
24-
return new ConcurrentBundle
25-
{
26-
_messages = new ConcurrentDictionary<string, AstMessage>(frozenBundle.Messages),
27-
Functions = new ConcurrentDictionary<string, FluentFunction>(frozenBundle.Functions),
28-
_terms = new ConcurrentDictionary<string, AstTerm>(frozenBundle.Terms),
29-
FormatterFunc = frozenBundle.FormatterFunc,
30-
Locales = frozenBundle.Locales,
31-
UseIsolating = frozenBundle.UseIsolating,
32-
MaxPlaceable = frozenBundle.MaxPlaceable,
33-
EnableExtensions = frozenBundle.EnableExtensions,
34-
TransformFunc = frozenBundle.TransformFunc,
35-
Culture = frozenBundle.Culture
36-
};
37-
}
24+
internal ConcurrentDictionary<string, AstTerm> Terms = new();
25+
internal ConcurrentDictionary<string, AstMessage> Messages = new();
3826

3927
/// <inheritdoc />
4028
protected override void AddMessageOverriding(AstMessage message)
4129
{
42-
_messages[message.GetId()] = message;
30+
Messages[message.GetId()] = message;
4331
}
4432

4533
/// <inheritdoc />
4634
protected override void AddTermOverriding(AstTerm term)
4735
{
48-
_terms[term.GetId()] = term;
36+
Terms[term.GetId()] = term;
4937
}
5038

5139
/// <inheritdoc />
5240
protected override bool TryAddTerm(AstTerm term, List<FluentError>? errors)
5341
{
54-
if (_terms.TryAdd(term.GetId(), term)) return true;
42+
if (Terms.TryAdd(term.GetId(), term)) return true;
5543
errors ??= new List<FluentError>();
5644
errors.Add(new OverrideFluentError(term.GetId(), EntryKind.Term));
5745
return false;
@@ -60,7 +48,7 @@ protected override bool TryAddTerm(AstTerm term, List<FluentError>? errors)
6048
/// <inheritdoc />
6149
protected override bool TryAddMessage(AstMessage message, List<FluentError>? errors)
6250
{
63-
if (_messages.TryAdd(message.GetId(), message)) return true;
51+
if (Messages.TryAdd(message.GetId(), message)) return true;
6452
errors ??= new List<FluentError>();
6553
errors.Add(new OverrideFluentError(message.GetId(), EntryKind.Message));
6654
return false;
@@ -89,19 +77,19 @@ public override void AddFunctionUnchecked(string funcName, ExternalFunction flue
8977
/// <inheritdoc />
9078
public override bool HasMessage(string identifier)
9179
{
92-
return _messages.ContainsKey(identifier);
80+
return Messages.ContainsKey(identifier);
9381
}
9482

9583
/// <inheritdoc />
9684
public override bool TryGetAstMessage(string ident, [NotNullWhen(true)] out AstMessage? message)
9785
{
98-
return _messages.TryGetValue(ident, out message);
86+
return Messages.TryGetValue(ident, out message);
9987
}
10088

10189
/// <inheritdoc />
10290
public override bool TryGetAstTerm(string ident, [NotNullWhen(true)] out AstTerm? term)
10391
{
104-
return _terms.TryGetValue(ident, out term);
92+
return Terms.TryGetValue(ident, out term);
10593
}
10694

10795

@@ -114,7 +102,7 @@ public override bool TryGetFunction(string funcName, [NotNullWhen(true)] out Flu
114102
/// <inheritdoc />
115103
public override IEnumerable<string> GetMessageEnumerable()
116104
{
117-
return _messages.Keys;
105+
return Messages.Keys;
118106
}
119107

120108
/// <inheritdoc />
@@ -126,19 +114,22 @@ public override IEnumerable<string> GetFuncEnumerable()
126114
/// <inheritdoc />
127115
public override IEnumerable<string> GetTermEnumerable()
128116
{
129-
return _terms.Keys;
117+
return Terms.Keys;
130118
}
131119

120+
/// <inheritdoc/>
132121
internal override IDictionary<string, AstMessage> GetMessagesDictionary()
133122
{
134-
return _messages;
123+
return Messages;
135124
}
136125

126+
/// <inheritdoc/>
137127
internal override IDictionary<string, AstTerm> GetTermsDictionary()
138128
{
139-
return _terms;
129+
return Terms;
140130
}
141131

132+
/// <inheritdoc/>
142133
internal override IDictionary<string, FluentFunction> GetFunctionDictionary()
143134
{
144135
return Functions;
@@ -150,8 +141,8 @@ public override FluentBundle DeepClone()
150141
return new ConcurrentBundle
151142
{
152143
Functions = new ConcurrentDictionary<string, FluentFunction>(Functions),
153-
_terms = new ConcurrentDictionary<string, AstTerm>(_terms),
154-
_messages = new ConcurrentDictionary<string, AstMessage>(_messages),
144+
Terms = new ConcurrentDictionary<string, AstTerm>(Terms),
145+
Messages = new ConcurrentDictionary<string, AstMessage>(Messages),
155146
Culture = (CultureInfo)Culture.Clone(),
156147
Locales = new List<string>(Locales),
157148
UseIsolating = UseIsolating,
@@ -167,8 +158,8 @@ public bool Equals(ConcurrentBundle? other)
167158
{
168159
if (ReferenceEquals(null, other)) return false;
169160
if (ReferenceEquals(this, other)) return true;
170-
return base.Equals(other) && Functions.SequenceEqual(other.Functions) && _terms.SequenceEqual(other._terms) &&
171-
_messages.SequenceEqual(other._messages);
161+
return base.Equals(other) && Functions.SequenceEqual(other.Functions) && Terms.SequenceEqual(other.Terms) &&
162+
Messages.SequenceEqual(other.Messages);
172163
}
173164

174165
/// <inheritdoc/>
@@ -180,7 +171,7 @@ public override bool Equals(object? obj)
180171
/// <inheritdoc/>
181172
public override int GetHashCode()
182173
{
183-
return HashCode.Combine(base.GetHashCode(), Functions, _terms, _messages);
174+
return HashCode.Combine(base.GetHashCode(), Functions, Terms, Messages);
184175
}
185176
}
186177
}

Linguini.Bundle/FrozenBundle.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ internal FrozenBundle(FluentBundle bundle)
115115
Functions = new Dictionary<string, FluentFunction>(bundle.GetFunctionDictionary());
116116
}
117117
#endif
118+
/// <inheritdoc/>
118119
public bool HasMessage(string identifier)
119120
{
120121
return Messages.ContainsKey(identifier);
121122
}
122123

124+
/// <inheritdoc/>
123125
public string FormatPattern(Pattern pattern, IDictionary<string, IFluentType>? args,
124126
[NotNullWhen(false)] out IList<FluentError>? errors)
125127
{
@@ -129,36 +131,43 @@ public string FormatPattern(Pattern pattern, IDictionary<string, IFluentType>? a
129131
return value.AsString();
130132
}
131133

134+
/// <inheritdoc/>
132135
public bool TryGetAstMessage(string ident, [NotNullWhen(true)] out AstMessage? message)
133136
{
134137
return Messages.TryGetValue(ident, out message);
135138
}
136139

140+
/// <inheritdoc/>
137141
public bool TryGetAstTerm(string ident, [NotNullWhen(true)] out AstTerm? term)
138142
{
139143
return Terms.TryGetValue(ident, out term);
140144
}
141145

146+
/// <inheritdoc/>
142147
public bool TryGetFunction(Identifier id, [NotNullWhen(true)] out FluentFunction? function)
143148
{
144149
return Functions.TryGetValue(id.ToString(), out function);
145150
}
146151

152+
/// <inheritdoc/>
147153
public bool TryGetFunction(string funcName, [NotNullWhen(true)] out FluentFunction? function)
148154
{
149155
return Functions.TryGetValue(funcName, out function);
150156
}
151157

158+
/// <inheritdoc/>
152159
public IEnumerable<string> GetMessageEnumerable()
153160
{
154161
return Messages.Keys;
155162
}
156163

164+
/// <inheritdoc/>
157165
public IEnumerable<string> GetFuncEnumerable()
158166
{
159167
return Functions.Keys;
160168
}
161169

170+
/// <inheritdoc/>
162171
public IEnumerable<string> GetTermEnumerable()
163172
{
164173
return Terms.Keys;

Linguini.Bundle/IReadBundle.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Diagnostics.CodeAnalysis;
45
using Linguini.Bundle.Errors;
@@ -8,6 +9,9 @@
89

910
namespace Linguini.Bundle
1011
{
12+
/// <summary>
13+
/// Represents an interface for reading language bundles.
14+
/// </summary>
1115
public interface IReadBundle
1216
{
1317
/// <summary>
@@ -197,8 +201,33 @@ public bool TryGetMessage(string id, string? attribute, IDictionary<string, IFlu
197201
}
198202
}
199203

204+
/// <summary>
205+
/// Provides extension methods for working with bundles.
206+
/// </summary>
200207
public static class ReadBundleExtensions
201208
{
209+
/// <summary>
210+
/// Thaws a frozen bundle and returns a new instance of a concurrent bundle.
211+
/// </summary>
212+
/// <param name="frozenBundle">The frozen bundle to thaw.</param>
213+
/// <returns>A new instance of a concurrent bundle.</returns>
214+
public static ConcurrentBundle Thaw(this FrozenBundle frozenBundle)
215+
{
216+
return new ConcurrentBundle
217+
{
218+
Messages = new ConcurrentDictionary<string, AstMessage>(frozenBundle.Messages),
219+
Functions = new ConcurrentDictionary<string, FluentFunction>(frozenBundle.Functions),
220+
Terms = new ConcurrentDictionary<string, AstTerm>(frozenBundle.Terms),
221+
FormatterFunc = frozenBundle.FormatterFunc,
222+
Locales = frozenBundle.Locales,
223+
UseIsolating = frozenBundle.UseIsolating,
224+
MaxPlaceable = frozenBundle.MaxPlaceable,
225+
EnableExtensions = frozenBundle.EnableExtensions,
226+
TransformFunc = frozenBundle.TransformFunc,
227+
Culture = frozenBundle.Culture
228+
};
229+
}
230+
202231
/// <summary>
203232
/// Convenience method for <see cref="IReadBundle.HasAttrMessage"/>
204233
/// </summary>
@@ -251,8 +280,8 @@ public static bool HasAttrMessage(this IReadBundle bundle, string idWithAttr)
251280
/// <param name="bundle">The bundle to retrieve the message from.</param>
252281
/// <param name="msgWithAttr">The message with attribute</param>
253282
/// <param name="args">Optional arguments to be passed to the attribute message.</param>
254-
/// <param name="errors">When this method returns, contains any errors that occured during retrieval, if any.</param>
255-
/// <param name="message">When this method returns, contains the retrieved attribute message, if it exists.</param>
283+
/// <param name="errors">When this method returns false, contains a list of errors that occurred while trying to retrieve the message; otherwise, null.</param>
284+
/// <param name="message">When this method returns true, contains the retrieved message; otherwise, null.</param>
256285
/// <returns><c>true</c> if the attribute message was successfully retrieved; otherwise, <c>false</c>.</returns>
257286
public static bool TryGetAttrMessage(this IReadBundle bundle, string msgWithAttr,
258287
IDictionary<string, IFluentType>? args,
@@ -266,8 +295,8 @@ public static bool TryGetAttrMessage(this IReadBundle bundle, string msgWithAttr
266295
/// </summary>
267296
/// <param name="bundle">The bundle to retrieve the message from.</param>
268297
/// <param name="id">The identifier of the message.</param>
269-
/// <param name="attribute">The attribute of the message (optional).</param>
270-
/// <param name="args">The arguments for the message (optional).</param>
298+
/// <param name="attribute">Optional attribute of the message.</param>
299+
/// <param name="args">Optional arguments to be passed to the attribute message.</param>
271300
/// <param name="errors">When this method returns false, contains a list of errors that occurred while trying to retrieve the message; otherwise, null.</param>
272301
/// <param name="message">When this method returns true, contains the retrieved message; otherwise, null.</param>
273302
/// <returns>True if the message was successfully retrieved, otherwise false.</returns>

0 commit comments

Comments
 (0)