// Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System.Xml.Serialization;
namespace OpenTap.Package
{
///
/// Uniquely identifies a package in the OpenTAP package system.
///
public class PackageIdentifier : IPackageIdentifier
{
///
/// Name of the package to which this object refers.
///
[XmlAttribute]
public string Name { get; set; }
///
/// The Semantic Version compliant version of the package.
///
[XmlAttribute]
public SemanticVersion Version { get; set; }
///
/// CPU Architecture
///
[XmlAttribute]
public CpuArchitecture Architecture { get; set; }
///
/// The operating system that this package supports
///
[XmlAttribute]
public string OS { get; set; }
///
/// Creates a package identifier
///
/// Name of the package.
/// Version of the package. This should be semver 2.0.0 compliant.
/// CPU architechture supported by the package.
/// Operating System supported by this package.
public PackageIdentifier(string packageName, string version, CpuArchitecture architecture, string os)
{
Name = packageName;
if (version != null)
Version = SemanticVersion.Parse(version);
Architecture = architecture;
OS = os;
}
///
/// Creates a package identifier
///
/// Name of the package.
/// Version of the package.
/// CPU architechture supported by the package.
/// Operating System supported by this package.
public PackageIdentifier(string packageName, SemanticVersion version, CpuArchitecture architecture, string os)
{
Name = packageName;
Version = version;
Architecture = architecture;
OS = os;
}
///
/// Creates a package identifier from another
///
public PackageIdentifier(IPackageIdentifier packageIdentifier) : this(packageIdentifier.Name, packageIdentifier.Version, packageIdentifier.Architecture, packageIdentifier.OS)
{
}
internal PackageIdentifier()
{
}
//override GetHashCode and Equals so PackageReference can be properly compared/distincted.
///
/// Returns the hash code for this PackageIdentifier.
///
///
public override int GetHashCode()
{
int hash = 0;
if (Name != null)
hash ^= Name.GetHashCode();
if (Version != null)
hash ^= Version.GetHashCode();
if (OS != null)
hash ^= OS.GetHashCode();
return hash ^ Architecture.GetHashCode();
}
///
/// Compare this PackageIdentifier to another object.
///
public override bool Equals(object obj)
{
var other = obj as IPackageIdentifier;
if (other == null) return false;
return other.Name == Name && other.Version == Version && other.OS == OS && other.Architecture == Architecture;
}
}
}