Skip to content

Commit 9a0a844

Browse files
Added Temperature implementation (#30)
1 parent b4e3564 commit 9a0a844

12 files changed

+1221
-0
lines changed

OnixLabs.Core.UnitTests/Units/TemperatureTests.cs

Lines changed: 470 additions & 0 deletions
Large diffs are not rendered by default.

OnixLabs.Core/OnixLabs.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050
<Using Include="OnixLabs.Core.Collections.Collection" Static="True" />
5151
</ItemGroup>
5252

53+
5354
</Project>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Core.Units;
16+
17+
public readonly partial struct Temperature<T>
18+
{
19+
/// <summary>
20+
/// Computes the sum of the specified <see cref="Temperature{T}"/> values.
21+
/// </summary>
22+
/// <param name="left">The left-hand value to add to.</param>
23+
/// <param name="right">The right-hand value to add.</param>
24+
/// <returns>Returns the sum of the specified <see cref="Temperature{T}"/> values.</returns>
25+
public static Temperature<T> Add(Temperature<T> left, Temperature<T> right)
26+
{
27+
return new Temperature<T>(left.Kelvin + right.Kelvin);
28+
}
29+
30+
/// <summary>
31+
/// Computes the difference between the specified <see cref="Temperature{T}"/> values.
32+
/// </summary>
33+
/// <param name="left">The left-hand value to subtract from.</param>
34+
/// <param name="right">The right-hand value to subtract.</param>
35+
/// <returns>Returns the difference between the specified <see cref="Temperature{T}"/> values.</returns>
36+
public static Temperature<T> Subtract(Temperature<T> left, Temperature<T> right)
37+
{
38+
return new Temperature<T>(left.Kelvin - right.Kelvin);
39+
}
40+
41+
/// <summary>
42+
/// Computes the product of the specified <see cref="Temperature{T}"/> values.
43+
/// </summary>
44+
/// <param name="left">The left-hand value to multiply.</param>
45+
/// <param name="right">The right-hand value to multiply by.</param>
46+
/// <returns>Returns the product of the specified <see cref="Temperature{T}"/> values.</returns>
47+
public static Temperature<T> Multiply(Temperature<T> left, Temperature<T> right)
48+
{
49+
return new Temperature<T>(left.Kelvin * right.Kelvin);
50+
}
51+
52+
/// <summary>
53+
/// Computes the quotient of the specified <see cref="Temperature{T}"/> values.
54+
/// </summary>
55+
/// <param name="left">The left-hand value to divide.</param>
56+
/// <param name="right">The right-hand value to divide by.</param>
57+
/// <returns>Returns the quotient of the specified <see cref="Temperature{T}"/> values.</returns>
58+
public static Temperature<T> Divide(Temperature<T> left, Temperature<T> right)
59+
{
60+
return new Temperature<T>(left.Kelvin / right.Kelvin);
61+
}
62+
63+
/// <summary>
64+
/// Computes the sum of the specified <see cref="Temperature{T}"/> values.
65+
/// </summary>
66+
/// <param name="left">The left-hand value to add to.</param>
67+
/// <param name="right">The right-hand value to add.</param>
68+
/// <returns>Returns the sum of the specified <see cref="Temperature{T}"/> values.</returns>
69+
public static Temperature<T> operator +(Temperature<T> left, Temperature<T> right)
70+
{
71+
return Add(left, right);
72+
}
73+
74+
/// <summary>
75+
/// Computes the difference between the specified <see cref="Temperature{T}"/> values.
76+
/// </summary>
77+
/// <param name="left">The left-hand value to subtract from.</param>
78+
/// <param name="right">The right-hand value to subtract.</param>
79+
/// <returns>Returns the difference between the specified <see cref="Temperature{T}"/> values.</returns>
80+
public static Temperature<T> operator -(Temperature<T> left, Temperature<T> right)
81+
{
82+
return Subtract(left, right);
83+
}
84+
85+
/// <summary>
86+
/// Computes the product of the specified <see cref="Temperature{T}"/> values.
87+
/// </summary>
88+
/// <param name="left">The left-hand value to multiply.</param>
89+
/// <param name="right">The right-hand value to multiply by.</param>
90+
/// <returns>Returns the product of the specified <see cref="Temperature{T}"/> values.</returns>
91+
public static Temperature<T> operator *(Temperature<T> left, Temperature<T> right)
92+
{
93+
return Multiply(left, right);
94+
}
95+
96+
/// <summary>
97+
/// Computes the quotient of the specified <see cref="Temperature{T}"/> values.
98+
/// </summary>
99+
/// <param name="left">The left-hand value to divide.</param>
100+
/// <param name="right">The right-hand value to divide by.</param>
101+
/// <returns>Returns the quotient of the specified <see cref="Temperature{T}"/> values.</returns>
102+
public static Temperature<T> operator /(Temperature<T> left, Temperature<T> right)
103+
{
104+
return Divide(left, right);
105+
}
106+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace OnixLabs.Core.Units;
18+
19+
public readonly partial struct Temperature<T> : IComparable<Temperature<T>>, IComparable
20+
{
21+
/// <summary>
22+
/// Compares two <see cref="Temperature{T}"/> values and returns an integer that indicates
23+
/// whether the left-hand value is less than, equal to, or greater than the right-hand value.
24+
/// </summary>
25+
/// <param name="left">The left-hand value to compare.</param>
26+
/// <param name="right">The right-hand value to compare.</param>
27+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
28+
public static int Compare(Temperature<T> left, Temperature<T> right)
29+
{
30+
return left.Kelvin.CompareTo(right.Kelvin);
31+
}
32+
33+
/// <summary>
34+
/// Compares the current instance with another object of the same type and returns an integer that indicates
35+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
36+
/// other object.
37+
/// </summary>
38+
/// <param name="other">An object to compare with this instance.</param>
39+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
40+
public int CompareTo(Temperature<T> other)
41+
{
42+
return Compare(this, other);
43+
}
44+
45+
/// <summary>
46+
/// Compares the current instance with another object of the same type and returns an integer that indicates
47+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
48+
/// other object.
49+
/// </summary>
50+
/// <param name="obj">An object to compare with this instance.</param>
51+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
52+
public int CompareTo(object? obj)
53+
{
54+
return this.CompareObject(obj);
55+
}
56+
57+
/// <summary>
58+
/// Performs a greater than comparison check between two <see cref="Temperature{T}"/> values.
59+
/// </summary>
60+
/// <param name="left">The left-hand value to compare.</param>
61+
/// <param name="right">The right-hand value to compare.</param>
62+
/// <returns>Returns true if the left-hand operand is greater than right-hand operand; otherwise, false.</returns>
63+
public static bool operator >(Temperature<T> left, Temperature<T> right)
64+
{
65+
return Compare(left, right) is 1;
66+
}
67+
68+
/// <summary>
69+
/// Performs a greater than or equal comparison check between two <see cref="Temperature{T}"/> values.
70+
/// </summary>
71+
/// <param name="left">The left-hand value to compare.</param>
72+
/// <param name="right">The right-hand value to compare.</param>
73+
/// <returns>Returns true if the left-hand operand is greater than or equal to right-hand operand; otherwise, false.</returns>
74+
public static bool operator >=(Temperature<T> left, Temperature<T> right)
75+
{
76+
return Compare(left, right) is 1 or 0;
77+
}
78+
79+
/// <summary>
80+
/// Performs a less than comparison check between two <see cref="Temperature{T}"/> values.
81+
/// </summary>
82+
/// <param name="left">The left-hand value to compare.</param>
83+
/// <param name="right">The right-hand value to compare.</param>
84+
/// <returns>Returns true if the left-hand operand is less than right-hand operand; otherwise, false.</returns>
85+
public static bool operator <(Temperature<T> left, Temperature<T> right)
86+
{
87+
return Compare(left, right) is -1;
88+
}
89+
90+
/// <summary>
91+
/// Performs a less than or equal comparison check between two <see cref="Temperature{T}"/> values.
92+
/// </summary>
93+
/// <param name="left">The left-hand value to compare.</param>
94+
/// <param name="right">The right-hand value to compare.</param>
95+
/// <returns>Returns true if the left-hand operand is less than or equal to right-hand operand; otherwise, false.</returns>
96+
public static bool operator <=(Temperature<T> left, Temperature<T> right)
97+
{
98+
return Compare(left, right) is -1 or 0;
99+
}
100+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Core.Units;
16+
17+
public readonly partial struct Temperature<T>
18+
{
19+
/// <summary>
20+
/// Represents an absolute zero (0°K) <see cref="Temperature{T}"/> value.
21+
/// </summary>
22+
public static Temperature<T> Zero => default;
23+
24+
/// <summary>
25+
/// Gets the default format.
26+
/// </summary>
27+
private const string DefaultFormat = "K";
28+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright © 2020 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
17+
namespace OnixLabs.Core.Units;
18+
19+
public readonly partial struct Temperature<T> : IEquatable<Temperature<T>>
20+
{
21+
/// <summary>
22+
/// Compares two instances of <see cref="Temperature{T}"/> to determine whether their values are equal.
23+
/// </summary>
24+
/// <param name="left">The left-hand value to compare.</param>
25+
/// <param name="right">The right-hand value to compare.</param>
26+
/// <returns>Returns true if the two specified instances are equal; otherwise, false.</returns>
27+
public static bool Equals(Temperature<T> left, Temperature<T> right)
28+
{
29+
return left.Kelvin == right.Kelvin;
30+
}
31+
32+
/// <summary>
33+
/// Indicates whether the current object is equal to another object of the same type.
34+
/// </summary>
35+
/// <param name="other">An object to compare with this object.</param>
36+
/// <returns>Returns true if the current object is equal to the other parameter; otherwise, false.</returns>
37+
public bool Equals(Temperature<T> other)
38+
{
39+
return Equals(this, other);
40+
}
41+
42+
/// <summary>
43+
/// Checks for equality between this instance and another object.
44+
/// </summary>
45+
/// <param name="obj">The object to check for equality.</param>
46+
/// <returns>true if the object is equal to this instance; otherwise, false.</returns>
47+
public override bool Equals(object? obj)
48+
{
49+
return obj is Temperature<T> other && Equals(other);
50+
}
51+
52+
/// <summary>
53+
/// Serves as a hash code function for this instance.
54+
/// </summary>
55+
/// <returns>A hash code for this instance.</returns>
56+
public override int GetHashCode()
57+
{
58+
return HashCode.Combine(Kelvin);
59+
}
60+
61+
/// <summary>
62+
/// Performs an equality check between two object instances.
63+
/// </summary>
64+
/// <param name="left">The left-hand value to compare.</param>
65+
/// <param name="right">The right-hand value to compare.</param>
66+
/// <returns>True if the instances are equal; otherwise, false.</returns>
67+
public static bool operator ==(Temperature<T> left, Temperature<T> right)
68+
{
69+
return Equals(left, right);
70+
}
71+
72+
/// <summary>
73+
/// Performs an inequality check between two object instances.
74+
/// </summary>
75+
/// <param name="left">The left-hand value to compare.</param>
76+
/// <param name="right">The right-hand value to compare.</param>
77+
/// <returns>True if the instances are not equal; otherwise, false.</returns>
78+
public static bool operator !=(Temperature<T> left, Temperature<T> right)
79+
{
80+
return !Equals(left, right);
81+
}
82+
}

0 commit comments

Comments
 (0)