' Classes/System/Version.ab

Namespace System

Class Version
	' Inherits ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
Public
	' Constractor
	Sub Version()
		major = 0
		minor = 0
		build = -1
		revision = -1
	End Sub

	'Sub Version(s As String)
	'End Sub

	Sub Version(major As Long, minor As Long)
		This.major = major
		This.minor = minor
		This.build = -1
		This.revision = -1
	End Sub

	Sub Version(major As Long, minor As Long, build As Long)
		This.major = major
		This.minor = minor
		This.build = build
		This.revision = -1
	End Sub

	Sub Version(major As Long, minor As Long, build As Long, revision As Long)
		This.major = major
		This.minor = minor
		This.build = build
		This.revision = revision
	End Sub

	Const Function Major() As Long
		Return major
	End Function

	Const Function Minor() As Long
		Return minor
	End Function

	Const Function Build() As Long
		Return build
	End Function

	Const Function Revision() As Long
		Return revision
	End Function

	Const Function MajorRevision() As Integer
		Return HIWORD(revision) As Integer
	End Function

	Const Function MinorRevision() As Integer
		Return LOWORD(revision) As Integer
	End Function

	Const Function CompareTo(v As Version) As Long
		CompareTo = major - v.major
		If CompareTo <> 0 Then Exit Function
		CompareTo = minor - v.minor
		If CompareTo <> 0 Then Exit Function
		CompareTo = build - v.build
		If CompareTo <> 0 Then Exit Function
		CompareTo = revision - v.revision
	End Function

	Const Function Equals(v As Version) As Boolean
		Return CompareTo(v) = 0
	End Function

	Override Function GetHashCode() As Long
		Return _System_BSwap(major As DWord) Xor minor Xor build Xor _System_BSwap(revision As DWord)
	End Function

	Function Operator ==(v As Version) As Boolean
		Return CompareTo(v) = 0
	End Function

	Function Operator <>(v As Version) As Boolean
		Return CompareTo(v) <> 0
	End Function

	Function Operator <(v As Version) As Boolean
		Return CompareTo(v) < 0
	End Function

	Function Operator >(v As Version) As Boolean
		Return CompareTo(v) > 0
	End Function

	Function Operator <=(v As Version) As Boolean
		Return CompareTo(v) <= 0
	End Function

	Function Operator >=(v As Version) As Boolean
		Return CompareTo(v) >= 0
	End Function

	Override Function ToString() As String
		ToString = Str$(major) + "." + Str$(minor)
		If build >= 0 Then
			ToString = ToString + "." + Str$(build)
			If revision >= 0 Then
				ToString = ToString + "." + Str$(revision)
			End If
		End If
	End Function

	Function ToString(fieldCount As Long) As String
		If fieldCount < 0 Or fieldCount > 4 Then
			Throw New ArgumentOutOfRangeException("fieldCount")
		End If
		ToString = ""
		If fieldCount = 0 Then Exit Function
		ToString = Str$(major)
		If fieldCount = 1 Then Exit Function
		ToString = ToString + "." + Str$(minor)
		If fieldCount = 2 Then Exit Function

		If build < 0 Then
			Throw New ArgumentException
		End If
		ToString = ToString + "." + Str$(build)
		If fieldCount = 3 Then Exit Function

		If revision < 0 Then
			Throw New ArgumentException
		End If
		ToString = ToString + "." + Str$(revision)
	End Function
Private
	major As Long
	minor As Long
	build As Long
	revision As Long
End Class

End Namespace 'System
