Skip to content

Commit 70cd2fd

Browse files
committed
In WebMarkupMin.AspNet.Common was made refactoring
1 parent 1fa39e7 commit 70cd2fd

File tree

3 files changed

+116
-144
lines changed

3 files changed

+116
-144
lines changed

src/WebMarkupMin.AspNet.Common/Compressors/BuiltInBrotliCompressionSettings.cs

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace WebMarkupMin.AspNet.Common.Compressors
1414
/// Brotli compression settings
1515
/// </summary>
1616
public sealed class BuiltInBrotliCompressionSettings
17+
#if NET9_0_OR_GREATER
18+
: CommonCompressionSettingsBase<BrotliCompressionOptions>
19+
#else
20+
: CommonCompressionSettingsBase
21+
#endif
1722
{
1823
#if NET9_0_OR_GREATER
1924
/// <summary>
@@ -26,16 +31,9 @@ public sealed class BuiltInBrotliCompressionSettings
2631
/// </summary>
2732
private const int MAX_ALT_COMPRESSION_LEVEL = 11;
2833

29-
/// <summary>
30-
/// Compression options
31-
/// </summary>
32-
private BrotliCompressionOptions _options = new();
33-
3434
#endif
35-
/// <summary>
36-
/// Gets or sets a compression level
37-
/// </summary>
38-
public CompressionLevel Level
35+
/// <inheritdoc/>
36+
public override CompressionLevel Level
3937
{
4038
#if NET9_0_OR_GREATER
4139
get
@@ -53,14 +51,12 @@ public CompressionLevel Level
5351
}
5452
#if NET9_0_OR_GREATER
5553

56-
/// <summary>
57-
/// Gets or sets a alternative compression level
58-
/// </summary>
54+
/// <inheritdoc/>
5955
/// <remarks>
6056
/// The higher the level, the slower the compression. Range is from 0 to 11. The default value is 4.
6157
/// </remarks>
6258
/// <exception cref="ArgumentOutOfRangeException">The value is less than 0 or greater than 11.</exception>
63-
public int AlternativeLevel
59+
public override int AlternativeLevel
6460
{
6561
get
6662
{
@@ -80,17 +76,6 @@ public int AlternativeLevel
8076
_options.Quality = value;
8177
}
8278
}
83-
#endif
84-
85-
86-
/// <summary>
87-
/// Constructs an instance of the brotli compression settings
88-
/// </summary>
89-
public BuiltInBrotliCompressionSettings()
90-
{
91-
Level = CompressionLevel.Optimal;
92-
}
93-
#if NET9_0_OR_GREATER
9479

9580

9681
/// <summary>
@@ -101,25 +86,14 @@ public BuiltInBrotliCompressionSettings()
10186
/// <exception cref="NotSupportedException"/>
10287
private static int ConvertCompressionLevelEnumToNumber(CompressionLevel level)
10388
{
104-
int levelNumber;
105-
106-
switch (level)
89+
int levelNumber = level switch
10790
{
108-
case CompressionLevel.NoCompression:
109-
levelNumber = MIN_ALT_COMPRESSION_LEVEL;
110-
break;
111-
case CompressionLevel.Fastest:
112-
levelNumber = 1;
113-
break;
114-
case CompressionLevel.Optimal:
115-
levelNumber = 4;
116-
break;
117-
case CompressionLevel.SmallestSize:
118-
levelNumber = MAX_ALT_COMPRESSION_LEVEL;
119-
break;
120-
default:
121-
throw new NotSupportedException();
122-
}
91+
CompressionLevel.NoCompression => MIN_ALT_COMPRESSION_LEVEL,
92+
CompressionLevel.Fastest => 1,
93+
CompressionLevel.Optimal => 4,
94+
CompressionLevel.SmallestSize => MAX_ALT_COMPRESSION_LEVEL,
95+
_ => throw new NotSupportedException()
96+
};
12397

12498
return levelNumber;
12599
}
@@ -132,40 +106,17 @@ private static int ConvertCompressionLevelEnumToNumber(CompressionLevel level)
132106
/// <exception cref="NotSupportedException"/>
133107
private static CompressionLevel ConvertCompressionLevelNumberToEnum(int level)
134108
{
135-
CompressionLevel levelEnum;
136-
137-
switch (level)
109+
CompressionLevel levelEnum = level switch
138110
{
139-
case MIN_ALT_COMPRESSION_LEVEL:
140-
levelEnum = CompressionLevel.NoCompression;
141-
break;
142-
case 1:
143-
case 2:
144-
levelEnum = CompressionLevel.Fastest;
145-
break;
146-
case int n when n >= 3 && n <= 9:
147-
levelEnum = CompressionLevel.Optimal;
148-
break;
149-
case 10:
150-
case MAX_ALT_COMPRESSION_LEVEL:
151-
levelEnum = CompressionLevel.SmallestSize;
152-
break;
153-
default:
154-
throw new NotSupportedException();
155-
}
111+
MIN_ALT_COMPRESSION_LEVEL => CompressionLevel.NoCompression,
112+
int n when n == 1 || n == 2 => CompressionLevel.Fastest,
113+
int n when n >= 3 && n <= 9 => CompressionLevel.Optimal,
114+
int n when n == 10 || n == MAX_ALT_COMPRESSION_LEVEL => CompressionLevel.SmallestSize,
115+
_ => throw new NotSupportedException()
116+
};
156117

157118
return levelEnum;
158119
}
159-
160-
161-
/// <summary>
162-
/// Gets a compression options
163-
/// </summary>
164-
/// <returns>Compression options</returns>
165-
internal BrotliCompressionOptions GetOptions()
166-
{
167-
return _options;
168-
}
169120
#endif
170121
}
171122
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#if NET45 || NETSTANDARD || NET9_0_OR_GREATER
2+
using System.IO.Compression;
3+
4+
namespace WebMarkupMin.AspNet.Common.Compressors
5+
{
6+
#if NET9_0_OR_GREATER
7+
/// <summary>
8+
/// Base class of common compression settings
9+
/// </summary>
10+
/// <typeparam name="TOptions">The type of generic compression options</typeparam>
11+
#else
12+
/// <summary>
13+
/// Base class of common compression settings
14+
/// </summary>
15+
#endif
16+
public abstract class CommonCompressionSettingsBase
17+
#if NET9_0_OR_GREATER
18+
<TOptions> where TOptions : class, new()
19+
#endif
20+
{
21+
#if NET9_0_OR_GREATER
22+
/// <summary>
23+
/// Compression options
24+
/// </summary>
25+
protected TOptions _options = new();
26+
27+
#endif
28+
/// <summary>
29+
/// Gets or sets a compression level
30+
/// </summary>
31+
public abstract CompressionLevel Level
32+
{
33+
get;
34+
set;
35+
}
36+
#if NET9_0_OR_GREATER
37+
38+
/// <summary>
39+
/// Gets or sets a alternative compression level
40+
/// </summary>
41+
public abstract int AlternativeLevel
42+
{
43+
get;
44+
set;
45+
}
46+
#endif
47+
48+
49+
/// <summary>
50+
/// Constructs an instance of common compression settings
51+
/// </summary>
52+
protected CommonCompressionSettingsBase()
53+
{
54+
Level = CompressionLevel.Optimal;
55+
}
56+
#if NET9_0_OR_GREATER
57+
58+
59+
/// <summary>
60+
/// Gets a compression options
61+
/// </summary>
62+
/// <returns>Compression options</returns>
63+
internal TOptions GetOptions()
64+
{
65+
return _options;
66+
}
67+
#endif
68+
}
69+
}
70+
#endif

src/WebMarkupMin.AspNet.Common/Compressors/ZLibCompressionSettingsBase.cs

Lines changed: 23 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace WebMarkupMin.AspNet.Common.Compressors
1414
/// Base class of the ZLib compression settings
1515
/// </summary>
1616
public abstract class ZLibCompressionSettingsBase
17+
#if NET9_0_OR_GREATER
18+
: CommonCompressionSettingsBase<ZLibCompressionOptions>
19+
#else
20+
: CommonCompressionSettingsBase
21+
#endif
1722
{
1823
#if NET9_0_OR_GREATER
1924
/// <summary>
@@ -26,16 +31,9 @@ public abstract class ZLibCompressionSettingsBase
2631
/// </summary>
2732
private const int MAX_ALT_COMPRESSION_LEVEL = 9;
2833

29-
/// <summary>
30-
/// Compression options
31-
/// </summary>
32-
private ZLibCompressionOptions _options = new();
33-
3434
#endif
35-
/// <summary>
36-
/// Gets or sets a compression level
37-
/// </summary>
38-
public CompressionLevel Level
35+
/// <inheritdoc/>
36+
public override CompressionLevel Level
3937
{
4038
#if NET9_0_OR_GREATER
4139
get
@@ -53,14 +51,12 @@ public CompressionLevel Level
5351
}
5452
#if NET9_0_OR_GREATER
5553

56-
/// <summary>
57-
/// Gets or sets a alternative compression level
58-
/// </summary>
54+
/// <inheritdoc/>
5955
/// <remarks>
6056
/// The higher the level, the slower the compression. Range is from 0 to 9. The default value is 6.
6157
/// </remarks>
6258
/// <exception cref="ArgumentOutOfRangeException">The value is less than 0 or greater than 9.</exception>
63-
public int AlternativeLevel
59+
public override int AlternativeLevel
6460
{
6561
get
6662
{
@@ -80,17 +76,6 @@ public int AlternativeLevel
8076
_options.CompressionLevel = value;
8177
}
8278
}
83-
#endif
84-
85-
86-
/// <summary>
87-
/// Constructs an instance of the ZLib compression settings
88-
/// </summary>
89-
protected ZLibCompressionSettingsBase()
90-
{
91-
Level = CompressionLevel.Optimal;
92-
}
93-
#if NET9_0_OR_GREATER
9479

9580

9681
/// <summary>
@@ -101,25 +86,14 @@ protected ZLibCompressionSettingsBase()
10186
/// <exception cref="NotSupportedException"/>
10287
private static int ConvertCompressionLevelEnumToNumber(CompressionLevel level)
10388
{
104-
int levelNumber;
105-
106-
switch (level)
89+
int levelNumber = level switch
10790
{
108-
case CompressionLevel.NoCompression:
109-
levelNumber = MIN_ALT_COMPRESSION_LEVEL;
110-
break;
111-
case CompressionLevel.Fastest:
112-
levelNumber = 1;
113-
break;
114-
case CompressionLevel.Optimal:
115-
levelNumber = 6;
116-
break;
117-
case CompressionLevel.SmallestSize:
118-
levelNumber = MAX_ALT_COMPRESSION_LEVEL;
119-
break;
120-
default:
121-
throw new NotSupportedException();
122-
}
91+
CompressionLevel.NoCompression => MIN_ALT_COMPRESSION_LEVEL,
92+
CompressionLevel.Fastest => 1,
93+
CompressionLevel.Optimal => 6,
94+
CompressionLevel.SmallestSize => MAX_ALT_COMPRESSION_LEVEL,
95+
_ => throw new NotSupportedException()
96+
};
12397

12498
return levelNumber;
12599
}
@@ -132,40 +106,17 @@ private static int ConvertCompressionLevelEnumToNumber(CompressionLevel level)
132106
/// <exception cref="NotSupportedException"/>
133107
private static CompressionLevel ConvertCompressionLevelNumberToEnum(int level)
134108
{
135-
CompressionLevel levelEnum;
136-
137-
switch (level)
109+
CompressionLevel levelEnum = level switch
138110
{
139-
case MIN_ALT_COMPRESSION_LEVEL:
140-
levelEnum = CompressionLevel.NoCompression;
141-
break;
142-
case 1:
143-
case 2:
144-
levelEnum = CompressionLevel.Fastest;
145-
break;
146-
case int n when n >= 3 && n <= 7:
147-
levelEnum = CompressionLevel.Optimal;
148-
break;
149-
case 8:
150-
case MAX_ALT_COMPRESSION_LEVEL:
151-
levelEnum = CompressionLevel.SmallestSize;
152-
break;
153-
default:
154-
throw new NotSupportedException();
155-
}
111+
MIN_ALT_COMPRESSION_LEVEL => CompressionLevel.NoCompression,
112+
int n when n == 1 || n == 2 => CompressionLevel.Fastest,
113+
int n when n >= 3 && n <= 7 => CompressionLevel.Optimal,
114+
int n when n == 8 || n == MAX_ALT_COMPRESSION_LEVEL => CompressionLevel.SmallestSize,
115+
_ => throw new NotSupportedException()
116+
};
156117

157118
return levelEnum;
158119
}
159-
160-
161-
/// <summary>
162-
/// Gets a compression options
163-
/// </summary>
164-
/// <returns>Compression options</returns>
165-
internal ZLibCompressionOptions GetOptions()
166-
{
167-
return _options;
168-
}
169120
#endif
170121
}
171122
}

0 commit comments

Comments
 (0)