DocumentId.cs 3.7 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5

using System;
using System.Collections.Generic;
using System.Diagnostics;
6
using Microsoft.CodeAnalysis.Shared.Extensions;
P
Pilchie 已提交
7 8 9 10 11
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
{
    /// <summary>
12
    /// An identifier that can be used to retrieve the same <see cref="Document"/> across versions of the
P
Pilchie 已提交
13 14
    /// workspace.
    /// </summary>
15
    [DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
16
    public sealed class DocumentId : IEquatable<DocumentId>, IObjectWritable
P
Pilchie 已提交
17
    {
18 19
        public ProjectId ProjectId { get; }
        public Guid Id { get; }
P
Pilchie 已提交
20

21
        private readonly string _debugName;
P
Pilchie 已提交
22

23
        private DocumentId(ProjectId projectId, Guid guid, string debugName)
24 25 26
        {
            this.ProjectId = projectId;
            this.Id = guid;
27
            _debugName = debugName;
28 29
        }

P
Pilchie 已提交
30
        /// <summary>
31
        /// Creates a new <see cref="DocumentId"/> instance.
P
Pilchie 已提交
32 33 34 35 36 37 38
        /// </summary>
        /// <param name="projectId">The project id this document id is relative to.</param>
        /// <param name="debugName">An optional name to make this id easier to recognize while debugging.</param>
        public static DocumentId CreateNewId(ProjectId projectId, string debugName = null)
        {
            if (projectId == null)
            {
J
Jared Parsons 已提交
39
                throw new ArgumentNullException(nameof(projectId));
P
Pilchie 已提交
40 41
            }

42
            return new DocumentId(projectId, Guid.NewGuid(), debugName);
P
Pilchie 已提交
43 44
        }

J
Jared Parsons 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        public static DocumentId CreateFromSerialized(ProjectId projectId, Guid id, string debugName = null)
        {
            if (projectId == null)
            {
                throw new ArgumentNullException(nameof(projectId));
            }

            if (id == Guid.Empty)
            {
                throw new ArgumentException(nameof(id));
            }

            return new DocumentId(projectId, id, debugName);
        }

60 61
        internal string DebugName => _debugName;

62
        internal string GetDebuggerDisplay()
P
Pilchie 已提交
63
        {
64
            return string.Format("({0}, #{1} - {2})", this.GetType().Name, this.Id, _debugName);
P
Pilchie 已提交
65 66 67 68
        }

        public override string ToString()
        {
69
            return GetDebuggerDisplay();
P
Pilchie 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj as DocumentId);
        }

        public bool Equals(DocumentId other)
        {
            // Technically, we don't need to check project id.
            return
                !ReferenceEquals(other, null) &&
                this.Id == other.Id &&
                this.ProjectId == other.ProjectId;
        }

        public override int GetHashCode()
        {
            return Hash.Combine(this.ProjectId, this.Id.GetHashCode());
        }

        public static bool operator ==(DocumentId left, DocumentId right)
        {
            return EqualityComparer<DocumentId>.Default.Equals(left, right);
        }

        public static bool operator !=(DocumentId left, DocumentId right)
        {
            return !(left == right);
        }
100

V
vsadov 已提交
101
        bool IObjectWritable.ShouldReuseInSerialization => true;
102

103 104 105 106
        void IObjectWritable.WriteTo(ObjectWriter writer)
        {
            ProjectId.WriteTo(writer);

107
            writer.WriteGuid(Id);
108 109 110 111 112 113 114
            writer.WriteString(DebugName);
        }

        internal static DocumentId ReadFrom(ObjectReader reader)
        {
            var projectId = ProjectId.ReadFrom(reader);

115
            var guid = reader.ReadGuid();
116 117 118 119
            var debugName = reader.ReadString();

            return CreateFromSerialized(projectId, guid, debugName);
        }
P
Pilchie 已提交
120
    }
121
}