-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace generic dictionary with specialized collection type.
- Loading branch information
1 parent
7c2b496
commit c84f4ba
Showing
22 changed files
with
221 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace SixLabors.ImageSharp.Web.Commands | ||
{ | ||
/// <summary> | ||
/// Represents an ordered collection of processing commands. | ||
/// </summary> | ||
public sealed class CommandCollection : KeyedCollection<string, KeyValuePair<string, string>> | ||
{ | ||
private readonly List<string> keys = new(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CommandCollection"/> class. | ||
/// </summary> | ||
public CommandCollection() | ||
: this(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
} | ||
|
||
private CommandCollection(IEqualityComparer<string> comparer) | ||
: base(comparer) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets an <see cref="ICollection{T}"/> representing the keys of the collection. | ||
/// </summary> | ||
public ICollection<string> Keys => this.keys; | ||
|
||
/// <summary> | ||
/// Gets the command value with the specified key. | ||
/// </summary> | ||
/// <param name="key">The key of the element to get.</param> | ||
/// <returns> | ||
/// The command value with the specified key. If a value with the specified key is not | ||
/// found, an exception is thrown. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception> | ||
/// <exception cref="KeyNotFoundException">An element with the specified key does not exist in the collection.</exception> | ||
public new string this[string key] | ||
{ | ||
get | ||
{ | ||
if (this.TryGetValue(key, out KeyValuePair<string, string> item)) | ||
{ | ||
return item.Key; | ||
} | ||
|
||
throw new KeyNotFoundException(); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void InsertItem(int index, KeyValuePair<string, string> item) | ||
{ | ||
base.InsertItem(index, item); | ||
this.keys.Insert(index, item.Key); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void RemoveItem(int index) | ||
{ | ||
base.RemoveItem(index); | ||
this.keys.RemoveAt(index); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void SetItem(int index, KeyValuePair<string, string> item) | ||
{ | ||
base.SetItem(index, item); | ||
this.keys[index] = item.Key; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void ClearItems() | ||
{ | ||
base.ClearItems(); | ||
this.keys.Clear(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override string GetKeyForItem(KeyValuePair<string, string> item) => item.Key; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/ImageSharp.Web/Commands/CommandCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace SixLabors.ImageSharp.Web.Commands | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="CommandCollectionExtensions"/>. | ||
/// </summary> | ||
public static class CommandCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the value associated with the specified key or the default value. | ||
/// </summary> | ||
/// <param name="collection">The collection instance.</param> | ||
/// <param name="key">The key of the value to get.</param> | ||
/// <returns>The value associated with the specified key or the default value.</returns> | ||
public static string GetValueOrDefault(this CommandCollection collection, string key) | ||
{ | ||
collection.TryGetValue(key, out KeyValuePair<string, string> result); | ||
return result.Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.