Skip to content

Commit

Permalink
Update argument null exception throw to use the helper method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Nov 14, 2024
1 parent ae106f6 commit 903c501
Show file tree
Hide file tree
Showing 28 changed files with 777 additions and 425 deletions.
25 changes: 5 additions & 20 deletions Core/Collection/List/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,37 +1384,22 @@ internal static IEnumerable<TResult> Select<TItem, TResult>(IEnumerable<TItem> l
{
i++;
var vA = padding;
var skip = true;
if (hasA)
{
hasA = a.MoveNext();
if (hasA)
{
skip = false;
vA = a.Current;
}
else
{
a.Dispose();
}
if (hasA) vA = a.Current;
else a.Dispose();
}

var vB = padding;
if (hasB)
{
hasB = b.MoveNext();
if (hasB)
{
skip = false;
vB = b.Current;
}
else
{
b.Dispose();
}
if (hasB) vB = b.Current;
else b.Dispose();
}

if (skip) yield break;
if (!hasA && !hasB) yield break;
yield return callback(vA, hasA, vB, hasB, i);
}
}
Expand Down
13 changes: 10 additions & 3 deletions Core/Collection/List/StringsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Trivial.Net;
using Trivial.Text;
Expand Down Expand Up @@ -54,7 +55,9 @@ public static async Task<string> ToResponseStringAsync(this IAsyncEnumerable<Ser
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="stream">The stream.</param>
/// <param name="encoding">The encoding; or null, by default, if uses UTF-8.</param>
/// <param name="encoding">The encoding; or null, by default, uses UTF-8.</param>
/// <exception cref="InvalidOperationException">The stream is disposed.</exception>
/// <exception cref="ArgumentException">stream is not writable.</exception>
public static void WriteTo(this IEnumerable<ServerSentEventInfo> col, Stream stream, Encoding encoding = null)
{
if (col == null || stream == null) return;
Expand All @@ -73,8 +76,10 @@ public static void WriteTo(this IEnumerable<ServerSentEventInfo> col, Stream str
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="stream">The stream.</param>
/// <param name="encoding">The encoding; or null, by default, if uses UTF-8.</param>
/// <param name="encoding">The encoding; or null, by default, uses UTF-8.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
/// <exception cref="InvalidOperationException">The stream is disposed.</exception>
/// <exception cref="ArgumentException">stream is not writable.</exception>
public static async Task WriteToAsync(this IEnumerable<ServerSentEventInfo> col, Stream stream, Encoding encoding = null)
{
if (col == null || stream == null) return;
Expand All @@ -93,8 +98,10 @@ public static async Task WriteToAsync(this IEnumerable<ServerSentEventInfo> col,
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="stream">The stream.</param>
/// <param name="encoding">The encoding; or null, by default, if uses UTF-8.</param>
/// <param name="encoding">The encoding; or null, by default, uses UTF-8.</param>
/// <returns>A task that represents the asynchronous write operation.</returns>
/// <exception cref="InvalidOperationException">The stream is disposed.</exception>
/// <exception cref="ArgumentException">stream is not writable.</exception>
public static async Task WriteToAsync(this IAsyncEnumerable<ServerSentEventInfo> col, Stream stream, Encoding encoding = null)
{
if (col == null || stream == null) return;
Expand Down
8 changes: 5 additions & 3 deletions Core/Data/Cache/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Trivial.Text;

namespace Trivial.Data;

Expand Down Expand Up @@ -79,21 +80,22 @@ public class DataCacheCollection<T> : ICollection<DataCacheItemInfo<T>>, IReadOn
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns>The value.</returns>
/// <exception cref="ArgumentNullException">id was null, empty or consists only of white-space characters.</exception>
/// <exception cref="ArgumentNullException">id was null.</exception>
/// <exception cref="ArgumentException">id was empty or consists only of white-space characters.</exception>
/// <exception cref="KeyNotFoundException">The identifier does not exist.</exception>
public T this[string id]
{
get
{
if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id), "id should not be null, empty or consists only of white-space characters.");
StringExtensions.AssertNotWhiteSpace(nameof(id), id);
var info = GetInfo(id);
if (info == null) throw new KeyNotFoundException("The identifier does not exist.");
return info.Value;
}

set
{
if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id), "id should not be null, empty or consists only of white-space characters.");
StringExtensions.AssertNotWhiteSpace(nameof(id), id);
items[id] = new DataCacheItemInfo<T>(id, value);
RemoveExpiredAuto();
}
Expand Down
10 changes: 6 additions & 4 deletions Core/Data/Cache/Namespaced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Trivial.Text;

namespace Trivial.Data;

Expand Down Expand Up @@ -80,22 +81,23 @@ public class NamespacedDataCacheCollection<T> : ICollection<DataCacheItemInfo<T>
/// <param name="ns">The namespace of resource group; or null for no namespace ones.</param>
/// <param name="id">The identifier in the resource group.</param>
/// <returns>The value.</returns>
/// <exception cref="ArgumentNullException">id was null, empty or consists only of white-space characters.</exception>
/// <exception cref="ArgumentNullException">id was null.</exception>
/// <exception cref="ArgumentException">id was empty or consists only of white-space characters.</exception>
/// <exception cref="KeyNotFoundException">The identifier does not exist.</exception>
public T this[string ns, string id]
{
get
{
if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id), "id should not be null, empty or consists only of white-space characters.");
StringExtensions.AssertNotWhiteSpace(nameof(id), id);
var info = GetInfo(ns, id);
if (info == null) throw new KeyNotFoundException("The identifier does not exist.");
return info.Value;
}

set
{
if (string.IsNullOrWhiteSpace(id)) throw new ArgumentNullException(nameof(id), "id should not be null, empty or consists only of white-space characters.");
else items[GetIdWithPrefix(ns, id)] = new DataCacheItemInfo<T>(ns, id, value);
StringExtensions.AssertNotWhiteSpace(nameof(id), id);
items[GetIdWithPrefix(ns, id)] = new DataCacheItemInfo<T>(ns, id, value);
RemoveExpiredAuto();
}
}
Expand Down
36 changes: 10 additions & 26 deletions Core/Data/Query/Criteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ public CollectionCriteria(CriteriaBooleanOperator op)
/// <param name="y">Criteria y.</param>
/// <returns>A collection of criteria.</returns>
public static CollectionCriteria operator &(CollectionCriteria x, ICriteria y)
{
return And(x, y);
}
=> And(x, y);

/// <summary>
/// AND operator for criteria.
Expand All @@ -101,9 +99,7 @@ public CollectionCriteria(CriteriaBooleanOperator op)
/// <param name="y">Criteria y.</param>
/// <returns>A collection of criteria.</returns>
public static CollectionCriteria operator &(ICriteria x, CollectionCriteria y)
{
return And(x, y);
}
=> And(x, y);

/// <summary>
/// AND operator for criteria.
Expand All @@ -112,9 +108,7 @@ public CollectionCriteria(CriteriaBooleanOperator op)
/// <param name="y">Criteria y.</param>
/// <returns>A collection of criteria.</returns>
public static CollectionCriteria operator &(CollectionCriteria x, CollectionCriteria y)
{
return And(x, y);
}
=> And(x, y);

/// <summary>
/// OR operator for criteria.
Expand All @@ -123,9 +117,7 @@ public CollectionCriteria(CriteriaBooleanOperator op)
/// <param name="y">Criteria y.</param>
/// <returns>A collection of criteria.</returns>
public static CollectionCriteria operator |(CollectionCriteria x, ICriteria y)
{
return Or(x, y);
}
=> Or(x, y);

/// <summary>
/// OR operator for criteria.
Expand All @@ -134,9 +126,7 @@ public CollectionCriteria(CriteriaBooleanOperator op)
/// <param name="y">Criteria y.</param>
/// <returns>A collection of criteria.</returns>
public static CollectionCriteria operator |(ICriteria x, CollectionCriteria y)
{
return Or(x, y);
}
=> Or(x, y);

/// <summary>
/// OR operator for criteria.
Expand All @@ -145,9 +135,7 @@ public CollectionCriteria(CriteriaBooleanOperator op)
/// <param name="y">Criteria y.</param>
/// <returns>A collection of criteria.</returns>
public static CollectionCriteria operator |(CollectionCriteria x, CollectionCriteria y)
{
return Or(x, y);
}
=> Or(x, y);

/// <summary>
/// Creates a collection criteria with AND operation.
Expand Down Expand Up @@ -191,13 +179,11 @@ public static bool IsForAll(ICriteria criteria)
if (criteria == null || criteria.CriteriaType == CriteriaType.All) return true;
if (criteria.CriteriaType == CriteriaType.Collection)
{
var col = criteria as IEnumerable<ICriteria>;
if (col != null && col.Count() == 0) return true;
if (criteria is IEnumerable<ICriteria> col && !col.Any()) return true;
}
else if (criteria.CriteriaType == CriteriaType.Property)
{
var prop = criteria as PropertyCriteria;
if (prop != null
if (criteria is PropertyCriteria prop
&& prop.Condition.ValueIsNull
&& (prop.Condition.Operator == DbCompareOperator.Contains || prop.Condition.Operator == DbCompareOperator.StartsWith || prop.Condition.Operator == DbCompareOperator.EndsWith))
return true;
Expand Down Expand Up @@ -326,13 +312,11 @@ public PropertyCriteria(string name, BasicCompareOperator op, DateTime value)
/// </summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
return string.Format(
=> string.Format(
CultureInfo.InvariantCulture,
"[{0}] {1}",
Name != null ? Name : "[empty]",
Name ?? "[empty]",
Condition != null ? Condition.ToString() : "null");
}

/// <summary>
/// Converts given basic compare operator to database compare operator.
Expand Down
Loading

0 comments on commit 903c501

Please sign in to comment.