summary refs log tree commit diff
path: root/crypto/src/asn1/x509/qualified/QCStatement.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 21:32:14 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 21:32:14 +0700
commit3c9d8a7198b04fa80f034c372fba4eea65c31e3b (patch)
treee1ee1e6c0a747dc4025acb490fd3cf9f2cde9b12 /crypto/src/asn1/x509/qualified/QCStatement.cs
parentAsn1Sequence subclass static methods (diff)
downloadBouncyCastle.NET-ed25519-3c9d8a7198b04fa80f034c372fba4eea65c31e3b.tar.xz
Refactoring in Asn1.X509.Qualified
Diffstat (limited to 'crypto/src/asn1/x509/qualified/QCStatement.cs')
-rw-r--r--crypto/src/asn1/x509/qualified/QCStatement.cs73
1 files changed, 33 insertions, 40 deletions
diff --git a/crypto/src/asn1/x509/qualified/QCStatement.cs b/crypto/src/asn1/x509/qualified/QCStatement.cs
index bfa515392..cdbf78b2e 100644
--- a/crypto/src/asn1/x509/qualified/QCStatement.cs
+++ b/crypto/src/asn1/x509/qualified/QCStatement.cs
@@ -1,7 +1,5 @@
 using System;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.X509.Qualified
 {
     /**
@@ -15,65 +13,60 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified
     public class QCStatement
         : Asn1Encodable
     {
-        private readonly DerObjectIdentifier	qcStatementId;
-        private readonly Asn1Encodable			qcStatementInfo;
-
-		public static QCStatement GetInstance(
-            object obj)
+        public static QCStatement GetInstance(object obj)
         {
-            if (obj == null || obj is QCStatement)
-            {
-                return (QCStatement) obj;
-            }
+            if (obj == null)
+                return null;
+            if (obj is QCStatement qcStatement)
+                return qcStatement;
+            return new QCStatement(Asn1Sequence.GetInstance(obj));
+        }
 
-			if (obj is Asn1Sequence)
-            {
-				return new QCStatement(Asn1Sequence.GetInstance(obj));
-            }
+        public static QCStatement GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new QCStatement(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
 
-			throw new ArgumentException("unknown object in GetInstance: " + Platform.GetTypeName(obj), "obj");
-		}
+        public static QCStatement GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new QCStatement(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-		private QCStatement(
-            Asn1Sequence seq)
+        private readonly DerObjectIdentifier m_statementId;
+        private readonly Asn1Encodable m_statementInfo;
+
+        private QCStatement(Asn1Sequence seq)
         {
-			qcStatementId = DerObjectIdentifier.GetInstance(seq[0]);
+            int count = seq.Count;
+            if (count < 1 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            m_statementId = DerObjectIdentifier.GetInstance(seq[0]);
 
 			if (seq.Count > 1)
 			{
-				qcStatementInfo = seq[1];
+				m_statementInfo = seq[1];
 			}
         }
 
-		public QCStatement(
-            DerObjectIdentifier qcStatementId)
+        // TODO[api] Rename parameter
+		public QCStatement(DerObjectIdentifier qcStatementId)
+            : this(qcStatementId, null)
         {
-            this.qcStatementId = qcStatementId;
         }
 
-        public QCStatement(
-            DerObjectIdentifier qcStatementId,
-            Asn1Encodable       qcStatementInfo)
+        // TODO[api] Rename parameters
+        public QCStatement(DerObjectIdentifier qcStatementId, Asn1Encodable qcStatementInfo)
         {
-            this.qcStatementId = qcStatementId;
-            this.qcStatementInfo = qcStatementInfo;
+            m_statementId = qcStatementId ?? throw new ArgumentNullException(nameof(qcStatementId));
+            m_statementInfo = qcStatementInfo;
         }
 
-		public DerObjectIdentifier StatementId
-		{
-			get { return qcStatementId; }
-		}
+        public DerObjectIdentifier StatementId => m_statementId;
 
-		public Asn1Encodable StatementInfo
-		{
-			get { return qcStatementInfo; }
-		}
+        public Asn1Encodable StatementInfo => m_statementInfo;
 
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(qcStatementId);
-            v.AddOptional(qcStatementInfo);
-            return new DerSequence(v);
+            return m_statementInfo == null
+                ?  new DerSequence(m_statementId)
+                :  new DerSequence(m_statementId, m_statementInfo);
         }
     }
 }