private CorrelationVectorV1(string baseVector, int extension, bool immutable)
 {
     this.BaseVector = baseVector;
     this.extension  = extension;
     this.Version    = CorrelationVectorVersion.V1;
     this.immutable  = immutable || CorrelationVectorV1.IsOversized(baseVector, extension);
 }
        /// <summary>
        /// Increments the current extension by one. Do this before passing the value to an
        /// outbound message header.
        /// </summary>
        /// <returns>
        /// The new value as a string that you can add to the outbound message header
        /// indicated by <see cref="HeaderName"/>.
        /// </returns>
        public override string Increment()
        {
            if (this.immutable)
            {
                return(this.Value);
            }
            int snapshot = 0;
            int next     = 0;

            do
            {
                snapshot = this.extension;
                if (snapshot == int.MaxValue)
                {
                    return(this.Value);
                }
                next = snapshot + 1;
                if (CorrelationVectorV1.IsOversized(this.BaseVector, next))
                {
                    this.immutable = true;
                    return(this.Value);
                }
            }while (snapshot != Interlocked.CompareExchange(ref this.extension, next, snapshot));
            return(string.Concat(this.BaseVector, ".", next));
        }
        /// <summary>
        /// Creates a new correlation vector by extending an existing value. This should be
        /// done at the entry point of an operation.
        /// </summary>
        /// <param name="correlationVector">
        /// Taken from the message header indicated by <see cref="HeaderName"/>.
        /// </param>
        /// <returns>A new correlation vector extended from the current vector.</returns>
        public new static CorrelationVectorV1 Extend(string correlationVector)
        {
            if (CorrelationVectorV1.IsImmutable(correlationVector))
            {
                return(CorrelationVectorV1.Parse(correlationVector));
            }

            if (CorrelationVectorV1.ValidateCorrelationVectorDuringCreation)
            {
                CorrelationVectorV1.Validate(correlationVector);
            }

            if (CorrelationVectorV1.IsOversized(correlationVector, 0))
            {
                return(CorrelationVectorV1.Parse(correlationVector + CorrelationVectorV1.TerminationSign));
            }

            return(new CorrelationVectorV1(correlationVector, 0, false));
        }