diff --git a/crypto/src/asn1/cms/AttributeTable.cs b/crypto/src/asn1/cms/AttributeTable.cs
index e20e6e9b2..aa312a13f 100644
--- a/crypto/src/asn1/cms/AttributeTable.cs
+++ b/crypto/src/asn1/cms/AttributeTable.cs
@@ -1,5 +1,5 @@
using System;
-using System.Collections;
+using System.Collections.Generic;
using Org.BouncyCastle.Utilities;
@@ -7,76 +7,64 @@ namespace Org.BouncyCastle.Asn1.Cms
{
public class AttributeTable
{
- private readonly IDictionary attributes;
+ private readonly Dictionary<DerObjectIdentifier, object> m_attributes;
- public AttributeTable(
- IDictionary attrs)
+ public AttributeTable(IDictionary<DerObjectIdentifier, object> attrs)
{
- this.attributes = Platform.CreateHashtable(attrs);
+ m_attributes = new Dictionary<DerObjectIdentifier, object>(attrs);
}
- public AttributeTable(
- Asn1EncodableVector v)
+ public AttributeTable(Asn1EncodableVector v)
{
- this.attributes = Platform.CreateHashtable(v.Count);
+ m_attributes = new Dictionary<DerObjectIdentifier, object>(v.Count);
- foreach (Asn1Encodable o in v)
+ foreach (Asn1Encodable e in v)
{
- Attribute a = Attribute.GetInstance(o);
-
- AddAttribute(a);
+ AddAttribute(Attribute.GetInstance(e));
}
}
- public AttributeTable(
- Asn1Set s)
+ public AttributeTable(Asn1Set s)
{
- this.attributes = Platform.CreateHashtable(s.Count);
+ m_attributes = new Dictionary<DerObjectIdentifier, object>(s.Count);
- for (int i = 0; i != s.Count; i++)
+ foreach (Asn1Encodable e in s)
{
- Attribute a = Attribute.GetInstance(s[i]);
-
- AddAttribute(a);
+ AddAttribute(Attribute.GetInstance(e));
}
}
- public AttributeTable(
- Attributes attrs)
+ public AttributeTable(Attributes attrs)
: this(Asn1Set.GetInstance(attrs.ToAsn1Object()))
{
}
- private void AddAttribute(
- Attribute a)
+ private void AddAttribute(Attribute a)
{
DerObjectIdentifier oid = a.AttrType;
- object obj = attributes[oid];
- if (obj == null)
+ if (!m_attributes.TryGetValue(oid, out object existingValue))
{
- attributes[oid] = a;
+ m_attributes[oid] = a;
+ return;
}
- else
- {
- IList v;
-
- if (obj is Attribute)
- {
- v = Platform.CreateArrayList();
- v.Add(obj);
- v.Add(a);
- }
- else
- {
- v = (IList) obj;
-
- v.Add(a);
- }
+ if (existingValue is IList<Attribute> existingList)
+ {
+ existingList.Add(a);
+ return;
+ }
- attributes[oid] = v;
+ if (existingValue is Attribute existingAttr)
+ {
+ var newList = new List<Attribute>();
+ newList.Add(existingAttr);
+ newList.Add(a);
+ m_attributes[oid] = newList;
+ return;
}
+
+ throw new InvalidOperationException();
}
/// <summary>Return the first attribute matching the given OBJECT IDENTIFIER</summary>
@@ -84,16 +72,18 @@ namespace Org.BouncyCastle.Asn1.Cms
{
get
{
- object obj = attributes[oid];
+ if (!m_attributes.TryGetValue(oid, out object existingValue))
+ return null;
- if (obj is IList)
- {
- return (Attribute)((IList)obj)[0];
- }
+ if (existingValue is IList<Attribute> existingList)
+ return existingList[0];
- return (Attribute) obj;
- }
- }
+ if (existingValue is Attribute existingAttr)
+ return existingAttr;
+
+ throw new InvalidOperationException();
+ }
+ }
/**
* Return all the attributes matching the OBJECT IDENTIFIER oid. The vector will be
@@ -102,26 +92,30 @@ namespace Org.BouncyCastle.Asn1.Cms
* @param oid type of attribute required.
* @return a vector of all the attributes found of type oid.
*/
- public Asn1EncodableVector GetAll(
- DerObjectIdentifier oid)
+ public Asn1EncodableVector GetAll(DerObjectIdentifier oid)
{
Asn1EncodableVector v = new Asn1EncodableVector();
- object obj = attributes[oid];
-
- if (obj is IList)
+ if (m_attributes.TryGetValue(oid, out object existingValue))
{
- foreach (Attribute a in (IList)obj)
+ if (existingValue is IList<Attribute> existingList)
{
- v.Add(a);
+ foreach (var attr in existingList)
+ {
+ v.Add(attr);
+ }
+ }
+ else if (existingValue is Attribute existingAttr)
+ {
+ v.Add(existingAttr);
+ }
+ else
+ {
+ throw new InvalidOperationException();
}
- }
- else if (obj != null)
- {
- v.Add((Attribute) obj);
}
- return v;
+ return v;
}
public int Count
@@ -130,52 +124,60 @@ namespace Org.BouncyCastle.Asn1.Cms
{
int total = 0;
- foreach (object o in attributes.Values)
- {
- if (o is IList)
- {
- total += ((IList)o).Count;
- }
- else
- {
- ++total;
- }
- }
+ foreach (object existingValue in m_attributes.Values)
+ {
+ if (existingValue is IList<Attribute> existingList)
+ {
+ total += existingList.Count;
+ }
+ else if (existingValue is Attribute existingAttr)
+ {
+ ++total;
+ }
+ else
+ {
+ throw new InvalidOperationException();
+ }
+ }
return total;
}
}
- public IDictionary ToDictionary()
+ public IDictionary<DerObjectIdentifier, object> ToDictionary()
{
- return Platform.CreateHashtable(attributes);
+ return new Dictionary<DerObjectIdentifier, object>(m_attributes);
}
public Asn1EncodableVector ToAsn1EncodableVector()
{
Asn1EncodableVector v = new Asn1EncodableVector();
- foreach (object obj in attributes.Values)
+ foreach (object existingValue in m_attributes.Values)
{
- if (obj is IList)
+ if (existingValue is IList<Attribute> existingList)
{
- foreach (object el in (IList)obj)
+ foreach (Attribute existingAttr in existingList)
{
- v.Add(Attribute.GetInstance(el));
+ v.Add(existingAttr);
}
}
+ else if (existingValue is Attribute existingAttr)
+ {
+ v.Add(existingAttr);
+ }
else
{
- v.Add(Attribute.GetInstance(obj));
+ throw new InvalidOperationException();
}
}
- return v;
+ return v;
}
public Attributes ToAttributes()
{
- return new Attributes(this.ToAsn1EncodableVector());
+ return new Attributes(ToAsn1EncodableVector());
}
/**
@@ -187,7 +189,7 @@ namespace Org.BouncyCastle.Asn1.Cms
*/
public AttributeTable Add(DerObjectIdentifier attrType, Asn1Encodable attrValue)
{
- AttributeTable newTable = new AttributeTable(attributes);
+ AttributeTable newTable = new AttributeTable(m_attributes);
newTable.AddAttribute(new Attribute(attrType, new DerSet(attrValue)));
@@ -196,9 +198,9 @@ namespace Org.BouncyCastle.Asn1.Cms
public AttributeTable Remove(DerObjectIdentifier attrType)
{
- AttributeTable newTable = new AttributeTable(attributes);
+ AttributeTable newTable = new AttributeTable(m_attributes);
- newTable.attributes.Remove(attrType);
+ newTable.m_attributes.Remove(attrType);
return newTable;
}
|