Skip to content

Commit ccfddef

Browse files
committed
use Dictionary instead of interface for internal only methods
1 parent 4a9c516 commit ccfddef

8 files changed

+51
-54
lines changed

projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ internal sealed class AutorecoveringConnection : IAutorecoveringConnection
6767

6868
private readonly object _recordedEntitiesLock = new object();
6969

70-
private readonly IDictionary<string, RecordedExchange> _recordedExchanges = new Dictionary<string, RecordedExchange>();
70+
private readonly Dictionary<string, RecordedExchange> _recordedExchanges = new Dictionary<string, RecordedExchange>();
7171

72-
private readonly IDictionary<string, RecordedQueue> _recordedQueues = new Dictionary<string, RecordedQueue>();
72+
private readonly Dictionary<string, RecordedQueue> _recordedQueues = new Dictionary<string, RecordedQueue>();
7373

74-
private readonly IDictionary<RecordedBinding, byte> _recordedBindings = new Dictionary<RecordedBinding, byte>();
74+
private readonly Dictionary<RecordedBinding, byte> _recordedBindings = new Dictionary<RecordedBinding, byte>();
7575

76-
private readonly IDictionary<string, RecordedConsumer> _recordedConsumers = new Dictionary<string, RecordedConsumer>();
76+
private readonly Dictionary<string, RecordedConsumer> _recordedConsumers = new Dictionary<string, RecordedConsumer>();
7777

78-
private readonly ICollection<AutorecoveringModel> _models = new List<AutorecoveringModel>();
78+
private readonly List<AutorecoveringModel> _models = new List<AutorecoveringModel>();
7979

8080
private EventHandler<ConnectionBlockedEventArgs> _recordedBlockedEventHandlers;
8181
private EventHandler<ShutdownEventArgs> _recordedShutdownEventHandlers;
@@ -485,12 +485,11 @@ public void DeleteRecordedBinding(RecordedBinding rb)
485485

486486
public RecordedConsumer DeleteRecordedConsumer(string consumerTag)
487487
{
488-
RecordedConsumer rc = null;
488+
RecordedConsumer rc;
489489
lock (_recordedEntitiesLock)
490490
{
491-
if (_recordedConsumers.ContainsKey(consumerTag))
491+
if (_recordedConsumers.TryGetValue(consumerTag, out rc))
492492
{
493-
rc = _recordedConsumers[consumerTag];
494493
_recordedConsumers.Remove(consumerTag);
495494
}
496495
}
@@ -912,10 +911,12 @@ private void PropagateQueueNameChangeToBindings(string oldName, string newName)
912911
{
913912
lock (_recordedBindings)
914913
{
915-
IEnumerable<RecordedBinding> bs = _recordedBindings.Keys.Where(b => b.Destination.Equals(oldName));
916-
foreach (RecordedBinding b in bs)
914+
foreach (RecordedBinding b in _recordedBindings.Keys)
917915
{
918-
b.Destination = newName;
916+
if (b.Destination.Equals(oldName))
917+
{
918+
b.Destination = newName;
919+
}
919920
}
920921
}
921922
}
@@ -924,21 +925,22 @@ private void PropagateQueueNameChangeToConsumers(string oldName, string newName)
924925
{
925926
lock (_recordedConsumers)
926927
{
927-
IEnumerable<KeyValuePair<string, RecordedConsumer>> cs = _recordedConsumers.
928-
Where(pair => pair.Value.Queue.Equals(oldName));
929-
foreach (KeyValuePair<string, RecordedConsumer> c in cs)
928+
foreach (KeyValuePair<string, RecordedConsumer> c in _recordedConsumers)
930929
{
931-
c.Value.Queue = newName;
930+
if (c.Value.Queue.Equals(oldName))
931+
{
932+
c.Value.Queue = newName;
933+
}
932934
}
933935
}
934936
}
935937

936938
private void RecoverBindings()
937939
{
938-
IDictionary<RecordedBinding, byte> recordedBindingsCopy = null;
940+
Dictionary<RecordedBinding, byte> recordedBindingsCopy;
939941
lock (_recordedBindings)
940942
{
941-
recordedBindingsCopy = _recordedBindings.ToDictionary(e => e.Key, e => e.Value);
943+
recordedBindingsCopy = new Dictionary<RecordedBinding, byte>(_recordedBindings);
942944
}
943945

944946
foreach (RecordedBinding b in recordedBindingsCopy.Keys)
@@ -1031,10 +1033,10 @@ private void RecoverConsumers()
10311033
throw new ObjectDisposedException(GetType().FullName);
10321034
}
10331035

1034-
IDictionary<string, RecordedConsumer> recordedConsumersCopy = null;
1036+
Dictionary<string, RecordedConsumer> recordedConsumersCopy;
10351037
lock (_recordedConsumers)
10361038
{
1037-
recordedConsumersCopy = _recordedConsumers.ToDictionary(e => e.Key, e => e.Value);
1039+
recordedConsumersCopy = new Dictionary<string, RecordedConsumer>(_recordedConsumers);
10381040
}
10391041

10401042
foreach (KeyValuePair<string, RecordedConsumer> pair in recordedConsumersCopy)
@@ -1091,10 +1093,10 @@ private void RecoverEntities()
10911093

10921094
private void RecoverExchanges()
10931095
{
1094-
IDictionary<string, RecordedExchange> recordedExchangesCopy = null;
1096+
Dictionary<string, RecordedExchange> recordedExchangesCopy;
10951097
lock (_recordedEntitiesLock)
10961098
{
1097-
recordedExchangesCopy = _recordedExchanges.ToDictionary(e => e.Key, e => e.Value);
1099+
recordedExchangesCopy = new Dictionary<string, RecordedExchange>(_recordedExchanges);
10981100
}
10991101

11001102
foreach (RecordedExchange rx in recordedExchangesCopy.Values)
@@ -1125,10 +1127,10 @@ private void RecoverModels()
11251127

11261128
private void RecoverQueues()
11271129
{
1128-
IDictionary<string, RecordedQueue> recordedQueuesCopy = null;
1130+
Dictionary<string, RecordedQueue> recordedQueuesCopy;
11291131
lock (_recordedEntitiesLock)
11301132
{
1131-
recordedQueuesCopy = _recordedQueues.ToDictionary(entry => entry.Key, entry => entry.Value);
1133+
recordedQueuesCopy = new Dictionary<string, RecordedQueue>(_recordedQueues);
11321134
}
11331135

11341136
foreach (KeyValuePair<string, RecordedQueue> pair in recordedQueuesCopy)

projects/RabbitMQ.Client/client/impl/BasicProperties.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ public override object Clone()
284284
var clone = MemberwiseClone() as BasicProperties;
285285
if (IsHeadersPresent())
286286
{
287-
clone.Headers = new Dictionary<string, object>();
288-
foreach (KeyValuePair<string, object> entry in Headers)
289-
{
290-
clone.Headers[entry.Key] = entry.Value;
291-
}
287+
clone.Headers = new Dictionary<string, object>(Headers);
292288
}
293289

294290
return clone;

projects/RabbitMQ.Client/client/impl/ContentHeaderPropertyReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
namespace RabbitMQ.Client.Impl
4747
{
48-
struct ContentHeaderPropertyReader
48+
internal struct ContentHeaderPropertyReader
4949
{
5050
private ushort m_bitCount;
5151
private ushort m_flagWord;
@@ -143,9 +143,9 @@ public string ReadShortstr()
143143
}
144144

145145
/// <returns>A type of <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
146-
public IDictionary<string, object> ReadTable()
146+
public Dictionary<string, object> ReadTable()
147147
{
148-
IDictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
148+
Dictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
149149
_memoryOffset += bytesRead;
150150
return result;
151151
}

projects/RabbitMQ.Client/client/impl/MethodArgumentReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
namespace RabbitMQ.Client.Impl
4747
{
48-
struct MethodArgumentReader
48+
internal struct MethodArgumentReader
4949
{
5050
private int? _bit;
5151
private int _bits;
@@ -125,10 +125,10 @@ public string ReadShortstr()
125125
return result;
126126
}
127127

128-
public IDictionary<string, object> ReadTable()
128+
public Dictionary<string, object> ReadTable()
129129
{
130130
ClearBits();
131-
IDictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
131+
Dictionary<string, object> result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead);
132132
_memoryOffset += bytesRead;
133133
return result;
134134
}

projects/RabbitMQ.Client/client/impl/ProtocolBase.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ namespace RabbitMQ.Client.Framing.Impl
4747
{
4848
abstract class ProtocolBase : IProtocol
4949
{
50-
public IDictionary<string, bool> Capabilities = new Dictionary<string, bool>();
50+
public IDictionary<string, bool> Capabilities;
5151

5252
public ProtocolBase()
5353
{
54-
Capabilities["publisher_confirms"] = true;
55-
Capabilities["exchange_exchange_bindings"] = true;
56-
Capabilities["basic.nack"] = true;
57-
Capabilities["consumer_cancel_notify"] = true;
58-
Capabilities["connection.blocked"] = true;
59-
Capabilities["authentication_failure_close"] = true;
54+
Capabilities = new Dictionary<string, bool>
55+
{
56+
["publisher_confirms"] = true,
57+
["exchange_exchange_bindings"] = true,
58+
["basic.nack"] = true,
59+
["consumer_cancel_notify"] = true,
60+
["connection.blocked"] = true,
61+
["authentication_failure_close"] = true
62+
};
6063
}
6164

6265
public abstract string ApiName { get; }

projects/RabbitMQ.Client/client/impl/SessionManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SessionManager
5555
public readonly ushort ChannelMax;
5656
private readonly IntAllocator _ints;
5757
private readonly Connection _connection;
58-
private readonly IDictionary<int, ISession> _sessionMap = new Dictionary<int, ISession>();
58+
private readonly Dictionary<int, ISession> _sessionMap = new Dictionary<int, ISession>();
5959
private bool _autoClose = false;
6060

6161
public SessionManager(Connection connection, ushort channelMax)

projects/RabbitMQ.Client/client/impl/StreamProperties.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ public override object Clone()
5555
var clone = MemberwiseClone() as StreamProperties;
5656
if (IsHeadersPresent())
5757
{
58-
clone.Headers = new Dictionary<string, object>();
59-
foreach (KeyValuePair<string, object> entry in Headers)
60-
{
61-
clone.Headers[entry.Key] = entry.Value;
62-
}
58+
clone.Headers = new Dictionary<string, object>(Headers);
6359
}
6460

6561
return clone;

projects/RabbitMQ.Client/client/impl/WireFormatting.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
namespace RabbitMQ.Client.Impl
5151
{
52-
class WireFormatting
52+
internal class WireFormatting
5353
{
5454
public static decimal AmqpToDecimal(byte scale, uint unsignedMantissa)
5555
{
@@ -138,7 +138,7 @@ public static object ReadFieldValue(ReadOnlyMemory<byte> memory, out int bytesRe
138138
bytesRead += 8;
139139
return ReadTimestamp(slice);
140140
case 'F':
141-
IDictionary<string, object> tableResult = ReadTable(slice, out int tableBytesRead);
141+
Dictionary<string, object> tableResult = ReadTable(slice, out int tableBytesRead);
142142
bytesRead += tableBytesRead;
143143
return tableResult;
144144
case 'A':
@@ -207,10 +207,10 @@ public static string ReadShortstr(ReadOnlyMemory<byte> memory, out int bytesRead
207207
/// and F, as well as the QPid-0-8 specific b, d, f, l, s, t,
208208
/// x and V types and the AMQP 0-9-1 A type.
209209
///</remarks>
210-
/// <returns>A <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
211-
public static IDictionary<string, object> ReadTable(ReadOnlyMemory<byte> memory, out int bytesRead)
210+
/// <returns>A <seealso cref="System.Collections.Generic.Dictionary{TKey,TValue}"/>.</returns>
211+
public static Dictionary<string, object> ReadTable(ReadOnlyMemory<byte> memory, out int bytesRead)
212212
{
213-
IDictionary<string, object> table = new Dictionary<string, object>();
213+
Dictionary<string, object> table = new Dictionary<string, object>();
214214
long tableLength = NetworkOrderDeserializer.ReadUInt32(memory);
215215
bytesRead = 4;
216216
while ((bytesRead - 4) < tableLength)
@@ -476,7 +476,7 @@ public static int WriteTable(Memory<byte> memory, IDictionary<string, object> va
476476
int bytesWritten = 0;
477477
foreach (KeyValuePair<string, object> entry in val)
478478
{
479-
bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key.ToString());
479+
bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key);
480480
bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
481481
}
482482

@@ -512,7 +512,7 @@ public static int GetTableByteCount(IDictionary<string, object> val)
512512

513513
foreach (KeyValuePair<string, object> entry in val)
514514
{
515-
byteCount += Encoding.UTF8.GetByteCount(entry.Key.ToString()) + 1;
515+
byteCount += Encoding.UTF8.GetByteCount(entry.Key) + 1;
516516
byteCount += GetFieldValueByteCount(entry.Value);
517517
}
518518

0 commit comments

Comments
 (0)