Skip to content

Commit

Permalink
Improve code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcean committed Nov 13, 2024
1 parent 3962c6f commit ae106f6
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 157 deletions.
5 changes: 3 additions & 2 deletions Core/Collection/Dictionary/KeyedDataMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Trivial.Reflection;

namespace Trivial.Collection;

Expand All @@ -30,7 +31,7 @@ public T this[string key]
{
get
{
if (key == null) throw new ArgumentNullException(nameof(key), "key should not be null.");
if (key == null) throw ObjectConvert.ArgumentNull(nameof(key));
key = key.Trim().ToLowerInvariant();
if (string.IsNullOrEmpty(key)) throw new ArgumentException("key should not be empty.", nameof(key));
if (mapping.TryGetValue(key, out var r)) return r;
Expand All @@ -56,7 +57,7 @@ public T this[string key]

set
{
if (key == null) throw new ArgumentNullException(nameof(key), "key should not be null.");
if (key == null) throw ObjectConvert.ArgumentNull(nameof(key));
key = key.Trim().ToLowerInvariant();
if (string.IsNullOrEmpty(key) || value is null) return;
try
Expand Down
100 changes: 65 additions & 35 deletions Core/Collection/List/ListExtensions.cs

Large diffs are not rendered by default.

67 changes: 63 additions & 4 deletions Core/Collection/List/StringsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,31 @@ public static string ToResponseString(this IEnumerable<ServerSentEventInfo> col,
/// Gets the server-sent event format string.
/// </summary>
/// <param name="col">The input collection.</param>
/// <param name="stream">The stream.</param>
/// <param name="newLineN">true if use \n instead of new line.</param>
/// <returns>A server-sent event format string.</returns>
public static void ToResponseString(this IEnumerable<ServerSentEventInfo> col, Stream stream)
public static async Task<string> ToResponseStringAsync(this IAsyncEnumerable<ServerSentEventInfo> col, bool newLineN)
{
if (col == null) return null;
var sb = new StringBuilder();
await foreach (var item in col)
{
if (item == null) continue;
item.ToResponseString(sb, newLineN);
}

return sb.ToString();
}

/// <summary>
/// Gets the server-sent event format string.
/// </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>
public static void WriteTo(this IEnumerable<ServerSentEventInfo> col, Stream stream, Encoding encoding = null)
{
if (col == null) return;
var writer = new StreamWriter(stream, Encoding.UTF8);
if (col == null || stream == null) return;
var writer = new StreamWriter(stream, encoding ?? Encoding.UTF8);
foreach (var item in col)
{
if (item == null) continue;
Expand All @@ -49,6 +68,46 @@ public static void ToResponseString(this IEnumerable<ServerSentEventInfo> col, S
}
}

/// <summary>
/// Gets the server-sent event format string.
/// </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>
/// <returns>A task that represents the asynchronous write operation.</returns>
public static async Task WriteToAsync(this IEnumerable<ServerSentEventInfo> col, Stream stream, Encoding encoding = null)
{
if (col == null || stream == null) return;
var writer = new StreamWriter(stream, encoding ?? Encoding.UTF8);
foreach (var item in col)
{
if (item == null) continue;
await writer.WriteAsync(item.ToResponseString(true));
writer.Write('\n');
await writer.FlushAsync();
}
}

/// <summary>
/// Gets the server-sent event format string.
/// </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>
/// <returns>A task that represents the asynchronous write operation.</returns>
public static async Task WriteToAsync(this IAsyncEnumerable<ServerSentEventInfo> col, Stream stream, Encoding encoding = null)
{
if (col == null || stream == null) return;
var writer = new StreamWriter(stream, encoding ?? Encoding.UTF8);
await foreach (var item in col)
{
if (item == null) continue;
await writer.WriteAsync(item.ToResponseString(true));
writer.Write('\n');
await writer.FlushAsync();
}
}

/// <summary>
/// Generates a string collection.
/// </summary>
Expand Down
45 changes: 23 additions & 22 deletions Core/Data/Query/SimpleCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using Trivial.Maths;
using Trivial.Reflection;

namespace Trivial.Data;

Expand Down Expand Up @@ -139,7 +140,7 @@ public abstract class SimpleCondition : ISimpleCondition
/// <returns>A comparing operator.</returns>
public static DbCompareOperator GetLeftOperator<T>(ISimpleInterval<T> value)
=> value == null
? throw new ArgumentNullException(nameof(value))
? throw ObjectConvert.ArgumentNull(nameof(value))
: (value.LeftOpen ? DbCompareOperator.Greater : DbCompareOperator.GreaterOrEqual);

/// <summary>
Expand All @@ -150,7 +151,7 @@ public static DbCompareOperator GetLeftOperator<T>(ISimpleInterval<T> value)
/// <returns>A comparing operator.</returns>
public static DbCompareOperator GetRightOperator<T>(ISimpleInterval<T> value)
=> value == null
? throw new ArgumentNullException(nameof(value))
? throw ObjectConvert.ArgumentNull(nameof(value))
: (value.RightOpen ? DbCompareOperator.Less : DbCompareOperator.LessOrEqual);

/// <summary>
Expand Down Expand Up @@ -572,7 +573,7 @@ public Int32Condition(DbCompareOperator op, int value) : base(op, value)
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static Int32Condition CreateFromLeft(StructValueSimpleInterval<int> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new Int32Condition
{
Value = value.MinValue,
Expand All @@ -587,7 +588,7 @@ public static Int32Condition CreateFromLeft(StructValueSimpleInterval<int> value
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static Int32Condition CreateFromRight(StructValueSimpleInterval<int> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new Int32Condition
{
Value = value.MaxValue,
Expand All @@ -602,7 +603,7 @@ public static Int32Condition CreateFromRight(StructValueSimpleInterval<int> valu
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static Int32Condition CreateFromLeft(NullableValueSimpleInterval<int> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.LeftBounded) return null;
return new Int32Condition
{
Expand All @@ -618,7 +619,7 @@ public static Int32Condition CreateFromLeft(NullableValueSimpleInterval<int> val
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static Int32Condition CreateFromRight(NullableValueSimpleInterval<int> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.RightBounded) return null;
return new Int32Condition
{
Expand Down Expand Up @@ -733,7 +734,7 @@ public Int64Condition(DbCompareOperator op, long value) : base(op, value)
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static Int64Condition CreateFromLeft(StructValueSimpleInterval<long> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new Int64Condition
{
Value = value.MinValue,
Expand All @@ -748,7 +749,7 @@ public static Int64Condition CreateFromLeft(StructValueSimpleInterval<long> valu
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static Int64Condition CreateFromRight(StructValueSimpleInterval<long> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new Int64Condition
{
Value = value.MaxValue,
Expand All @@ -763,7 +764,7 @@ public static Int64Condition CreateFromRight(StructValueSimpleInterval<long> val
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static Int64Condition CreateFromLeft(NullableValueSimpleInterval<long> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.LeftBounded) return null;
return new Int64Condition
{
Expand All @@ -779,7 +780,7 @@ public static Int64Condition CreateFromLeft(NullableValueSimpleInterval<long> va
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static Int64Condition CreateFromRight(NullableValueSimpleInterval<long> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.RightBounded) return null;
return new Int64Condition
{
Expand Down Expand Up @@ -894,7 +895,7 @@ public SingleCondition(DbCompareOperator op, float value) : base(op, value)
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static SingleCondition CreateFromLeft(StructValueSimpleInterval<float> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new SingleCondition
{
Value = value.MinValue,
Expand All @@ -909,7 +910,7 @@ public static SingleCondition CreateFromLeft(StructValueSimpleInterval<float> va
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static SingleCondition CreateFromRight(StructValueSimpleInterval<float> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new SingleCondition
{
Value = value.MaxValue,
Expand All @@ -924,7 +925,7 @@ public static SingleCondition CreateFromRight(StructValueSimpleInterval<float> v
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static SingleCondition CreateFromLeft(NullableValueSimpleInterval<float> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.LeftBounded) return null;
return new SingleCondition
{
Expand All @@ -940,7 +941,7 @@ public static SingleCondition CreateFromLeft(NullableValueSimpleInterval<float>
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static SingleCondition CreateFromRight(NullableValueSimpleInterval<float> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.RightBounded) return null;
return new SingleCondition
{
Expand Down Expand Up @@ -1055,7 +1056,7 @@ public DoubleCondition(DbCompareOperator op, double value) : base(op, value)
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static DoubleCondition CreateFromLeft(StructValueSimpleInterval<double> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new DoubleCondition
{
Value = value.MinValue,
Expand All @@ -1070,7 +1071,7 @@ public static DoubleCondition CreateFromLeft(StructValueSimpleInterval<double> v
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static DoubleCondition CreateFromRight(StructValueSimpleInterval<double> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new DoubleCondition
{
Value = value.MaxValue,
Expand All @@ -1085,7 +1086,7 @@ public static DoubleCondition CreateFromRight(StructValueSimpleInterval<double>
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static DoubleCondition CreateFromLeft(NullableValueSimpleInterval<double> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.LeftBounded) return null;
return new DoubleCondition
{
Expand All @@ -1101,7 +1102,7 @@ public static DoubleCondition CreateFromLeft(NullableValueSimpleInterval<double>
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static DoubleCondition CreateFromRight(NullableValueSimpleInterval<double> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.RightBounded) return null;
return new DoubleCondition
{
Expand Down Expand Up @@ -1206,7 +1207,7 @@ public DateTimeCondition(IStructSimpleCondition<DateTime> copier)
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static DateTimeCondition CreateFromLeft(StructValueSimpleInterval<DateTime> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new DateTimeCondition
{
Value = value.MinValue,
Expand All @@ -1221,7 +1222,7 @@ public static DateTimeCondition CreateFromLeft(StructValueSimpleInterval<DateTim
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static DateTimeCondition CreateFromRight(StructValueSimpleInterval<DateTime> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
return new DateTimeCondition
{
Value = value.MaxValue,
Expand All @@ -1236,7 +1237,7 @@ public static DateTimeCondition CreateFromRight(StructValueSimpleInterval<DateTi
/// <returns>A condition from left bound of a specific simple interval.</returns>
public static DateTimeCondition CreateFromLeft(NullableValueSimpleInterval<DateTime> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.LeftBounded) return null;
return new DateTimeCondition
{
Expand All @@ -1252,7 +1253,7 @@ public static DateTimeCondition CreateFromLeft(NullableValueSimpleInterval<DateT
/// <returns>A condition from right bound of a specific simple interval.</returns>
public static DateTimeCondition CreateFromRight(NullableValueSimpleInterval<DateTime> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw ObjectConvert.ArgumentNull(nameof(value));
if (!value.RightBounded) return null;
return new DateTimeCondition
{
Expand Down
7 changes: 4 additions & 3 deletions Core/IO/File/FileSystemInfoUtility.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.Reflection;
using Trivial.Text;

namespace Trivial.IO;
Expand Down Expand Up @@ -548,7 +549,7 @@ public static DirectoryInfo TryGetDirectoryInfo(string folder, string folderName
public static FileInfo GetFileInfo(string fileName)
{
var path = GetLocalPath(fileName);
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentNullException(nameof(fileName));
if (string.IsNullOrWhiteSpace(path)) throw ObjectConvert.ArgumentNull(nameof(fileName));
path = Path.Combine(path, fileName);
return new FileInfo(path);
}
Expand All @@ -566,8 +567,8 @@ public static FileInfo GetFileInfo(string fileName)
public static FileInfo GetFileInfo(string folder, string fileName)
{
var path = GetLocalPath(folder);
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentNullException(nameof(folder));
if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException(nameof(fileName));
if (string.IsNullOrWhiteSpace(path)) throw ObjectConvert.ArgumentNull(nameof(folder));
if (string.IsNullOrWhiteSpace(fileName)) throw ObjectConvert.ArgumentNull(nameof(fileName));
path = Path.Combine(path, fileName);
return new FileInfo(path);
}
Expand Down
9 changes: 5 additions & 4 deletions Core/IO/Stream/CharsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Trivial.Reflection;

namespace Trivial.IO;

Expand Down Expand Up @@ -118,7 +119,7 @@ public override int Read()
/// <exception cref="ObjectDisposedException">The current reader is closed.</exception>
public override int Read(char[] buffer, int index, int count)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer), "buffer should not be null");
if (buffer == null) throw ObjectConvert.ArgumentNull(nameof(buffer));
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), "index should not be negative.");
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "count should not be negative.");
if (count == 0) return 0;
Expand Down Expand Up @@ -402,7 +403,7 @@ public static IEnumerable<string> ReadLines(TextReader reader, Action<bool> onRe
/// <exception cref="ObjectDisposedException">The stream has disposed.</exception>
public static IEnumerable<string> ReadLines(Stream stream, Encoding encoding, bool removeEmptyLine = false)
{
if (stream == null) throw new ArgumentNullException(nameof(stream), "stream should not be null.");
if (stream == null) throw ObjectConvert.ArgumentNull(nameof(stream));
var reader = new StreamReader(stream, encoding ?? Encoding.UTF8);
var hasDisposed = false;
return ReadLines(reader, isSucc =>
Expand Down Expand Up @@ -501,7 +502,7 @@ public static IEnumerable<string> ReadLines(IEnumerable<char> chars, bool remove
/// <exception cref="IOException">An I/O error occurs.</exception>
public static IEnumerable<string> ReadLines(FileInfo file, Encoding encoding, bool removeEmptyLine = false)
{
if (file == null) throw new ArgumentNullException(nameof(file), "file should not be null.");
if (file == null) throw ObjectConvert.ArgumentNull(nameof(file));
var reader = new StreamReader(file.FullName, encoding);
var hasDisposed = false;
return ReadLines(reader, isSucc =>
Expand All @@ -525,7 +526,7 @@ public static IEnumerable<string> ReadLines(FileInfo file, Encoding encoding, bo
/// <exception cref="NotSupportedException">Cannot read the file.</exception>
public static IEnumerable<string> ReadLines(FileInfo file, bool detectEncodingFromByteOrderMarks, bool removeEmptyLine = false)
{
if (file == null) throw new ArgumentNullException(nameof(file), "file should not be null.");
if (file == null) throw ObjectConvert.ArgumentNull(nameof(file));
var reader = new StreamReader(file.FullName, detectEncodingFromByteOrderMarks);
var hasDisposed = false;
return ReadLines(reader, isSucc =>
Expand Down
Loading

0 comments on commit ae106f6

Please sign in to comment.