dolittle/DotNET.SDK

View on GitHub
Source/Artifacts/Artifact.cs

Summary

Maintainability
A
0 mins
Test Coverage
D
65%
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using Dolittle.SDK.Common.Model;
using Dolittle.SDK.Concepts;

namespace Dolittle.SDK.Artifacts;

/// <summary>
/// Represents the base representation of an artifact.
/// </summary>
/// <typeparam name="TId">The <see cref="Type" /> of the <see cref="ArtifactId" />.</typeparam>
public abstract class Artifact<TId> : IIdentifier<TId>, IEquatable<Artifact<TId>>
    where TId : ArtifactId
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Artifact{TId}"/> class.
    /// </summary>
    /// <param name="id"><typeparamref name="TId">Id</typeparamref> of the <see cref="Artifact{TId}"/>.</param>
    /// <param name="alias">The alias of the <see cref="Artifact{TId}"/>.</param>
    protected Artifact(TId id, string? alias = null)
        : this(id, Generation.First, alias)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Artifact{TId}"/> class.
    /// </summary>
    /// <param name="id"><typeparamref name="TId">Id</typeparamref> of the <see cref="Artifact{TId}"/>.</param>
    /// <param name="generation"><see cref="Generation">Generation</see> of the <see cref="Artifact{TId}"/>.</param>
    /// <param name="alias">The alias of the <see cref="Artifact{TId}"/>.</param>
    protected Artifact(TId id, Generation generation, string? alias = null)
    {
        Id = id;
        Generation = generation;
        Alias = alias;
    }
    
    /// <summary>
    /// Gets the <typeparamref name="TId">Id</typeparamref> of the <see cref="Artifact{TId}"/>.
    /// </summary>
    public TId Id { get; }

    /// <inheritdoc />
    public string? Alias { get; }

    /// <summary>
    /// Gets the <see cref="Generation">Generation</see> of the <see cref="Artifact{TId}"/>.
    /// </summary>
    public Generation Generation { get; }

    /// <inheritdoc />
    Guid IIdentifier.Id => Id.Value;

    /// <inheritdoc />
    public bool CanCoexistWith(IIdentifier<ConceptAs<Guid>> identifier)
    {
        if (identifier.GetType() != GetType())
        {
            return false;
        }
        var artifact = identifier as Artifact<TId>;
        return Id.Equals(artifact?.Id) && !Generation.Equals(artifact?.Generation);
    }

    /// <inheritdoc />
    public bool CanCoexistWith(IIdentifier identifier) => identifier is IIdentifier<TId> typedIdentifier && CanCoexistWith(typedIdentifier);

    /// <inheritdoc />
    public bool Equals(Artifact<TId> other)
    {
        if (ReferenceEquals(null, other))
        {
            return false;
        }
        if (ReferenceEquals(this, other))
        {
            return true;
        }
        return EqualityComparer<TId>.Default.Equals(Id, other.Id) && Equals(Generation, other.Generation);
    }

    /// <inheritdoc />
    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        return obj.GetType() == GetType() && Equals((Artifact<TId>) obj);
    }

    /// <inheritdoc />
    public override int GetHashCode()
        => HashCode.Combine(Id, Generation);

    public override string ToString()
        => string.IsNullOrEmpty(Alias)
            ? $"{GetType().Name}({Id.Value} Generation: {Generation})"
            : $"{GetType().Name}({Alias} {Id.Value} Generation: {Generation})";
}