Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mass modify classes #16

Merged
merged 14 commits into from
Mar 16, 2024
7 changes: 0 additions & 7 deletions src/Lib/Enums/ParsedNetAddressStringType.cs

This file was deleted.

23 changes: 10 additions & 13 deletions src/Lib/Models/BinaryNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,23 @@ public class BinaryNumber
/// <param name="byteItem">A byte value.</param>
public BinaryNumber(byte byteItem)
{
List<BitRepresentation> bitValuesList = new();

BitArray bitArray = new(new byte[] { byteItem });

BitValues = new BitRepresentation[bitArray.Length];

for (int i = bitArray.Length - 1; i >= 0; i--)
{
bitValuesList.Add(new(i, bitArray[i]));
BitValues[i] = new BitRepresentation(
position: i,
isOn: bitArray[i]
);
}

bitValues = bitValuesList.ToArray();
}

/// <summary>
/// An array of the bits used for the binary number.
/// </summary>
public BitRepresentation[] BitValues
{
get => bitValues;
}

private readonly BitRepresentation[] bitValues;
public BitRepresentation[] BitValues { get; }

/// <summary>
/// Get the amount of unused bits.
Expand All @@ -42,7 +39,7 @@ public int GetUnusedBits()
{
int bitsUnused = 0;

foreach (BitRepresentation bitItem in bitValues)
foreach (BitRepresentation bitItem in BitValues)
{
bitsUnused += bitItem.BitActualValue;
}
Expand All @@ -58,7 +55,7 @@ public int GetUsedBits()
{
int bitsUsed = 255;

foreach (BitRepresentation bitItem in bitValues)
foreach (BitRepresentation bitItem in BitValues)
{
bitsUsed -= bitItem.BitActualValue;
}
Expand Down
21 changes: 6 additions & 15 deletions src/Lib/Models/BitRepresentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,37 @@ public class BitRepresentation
/// <param name="isOn">The bit is on and used.</param>
public BitRepresentation(int position, bool isOn)
{
bitPosition = position;
bitIsOn = isOn;
BitPosition = position;
BitIsOn = isOn;
}

/// <summary>
/// The position of the bit.
/// </summary>
public int BitPosition
{
get => bitPosition;
}
public int BitPosition { get; }

/// <summary>
/// The value of the bit.
/// 2^bitPosition
/// </summary>
public int BitValue
{
get => (int)Math.Pow(2, bitPosition);
get => (int)Math.Pow(2, BitPosition);
}

/// <summary>
/// If the bit is on or not.
/// </summary>
public bool BitIsOn
{
get => bitIsOn;
}
public bool BitIsOn { get; }

/// <summary>
/// The actual value of the bit if it's on or not.
/// </summary>
public int BitActualValue
{
get => bitIsOn ? BitValue : 0;
get => BitIsOn ? BitValue : 0;
}

private readonly int bitPosition;
private readonly bool bitIsOn;

public override string ToString()
{
return $"{BitActualValue}";
Expand Down
70 changes: 26 additions & 44 deletions src/Lib/Models/IPv4Subnet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using SmallsOnline.Subnetting.Lib.Enums;

namespace SmallsOnline.Subnetting.Lib.Models;

Expand All @@ -15,10 +14,10 @@ public class IPv4Subnet
/// <param name="cidr">The CIDR mask for the subnet.</param>
public IPv4Subnet(IPAddress ipAddress, double cidr)
{
_subnetMask = new(cidr);
_networkAddress = GetSubnetBoundary(ipAddress, _subnetMask);
_broadcastAddress = GetBroadcastAddress(_networkAddress, _subnetMask);
_usableHostRange = new(_networkAddress, _broadcastAddress);
SubnetMask = new(cidr);
NetworkAddress = GetSubnetBoundary(ipAddress, SubnetMask);
BroadcastAddress = GetBroadcastAddress(NetworkAddress, SubnetMask);
UsableHostRange = new(NetworkAddress, BroadcastAddress);
}

/// <summary>
Expand All @@ -28,10 +27,10 @@ public IPv4Subnet(IPAddress ipAddress, double cidr)
/// <param name="subnetMask">The subnet mask.</param>
public IPv4Subnet(IPAddress ipAddress, IPv4SubnetMask subnetMask)
{
_subnetMask = subnetMask;
_networkAddress = GetSubnetBoundary(ipAddress, _subnetMask);
_broadcastAddress = GetBroadcastAddress(_networkAddress, _subnetMask);
_usableHostRange = new(_networkAddress, _broadcastAddress);
SubnetMask = subnetMask;
NetworkAddress = GetSubnetBoundary(ipAddress, SubnetMask);
BroadcastAddress = GetBroadcastAddress(NetworkAddress, SubnetMask);
UsableHostRange = new(NetworkAddress, BroadcastAddress);
}

/// <summary>
Expand All @@ -41,10 +40,10 @@ public IPv4Subnet(IPAddress ipAddress, IPv4SubnetMask subnetMask)
/// <param name="subnetMask">The subnet mask.</param>
public IPv4Subnet(IPAddress ipAddress, IPAddress subnetMask)
{
_subnetMask = new(subnetMask.GetAddressBytes());
_networkAddress = GetSubnetBoundary(ipAddress, _subnetMask);
_broadcastAddress = GetBroadcastAddress(_networkAddress, _subnetMask);
_usableHostRange = new(_networkAddress, _broadcastAddress);
SubnetMask = new(subnetMask.GetAddressBytes());
NetworkAddress = GetSubnetBoundary(ipAddress, SubnetMask);
BroadcastAddress = GetBroadcastAddress(NetworkAddress, SubnetMask);
UsableHostRange = new(NetworkAddress, BroadcastAddress);
}

/// <summary>
Expand All @@ -55,87 +54,70 @@ public IPv4Subnet(IPAddress ipAddress, IPAddress subnetMask)
/// <param name="networkString">A network written in a string format.</param>
public IPv4Subnet(string networkString)
{
ParsedNetAddressString parsedNetAddress = new(networkString);
ParsedNetworkAddressString parsedNetAddress = new(networkString);

_subnetMask = parsedNetAddress.ParsedType switch
SubnetMask = parsedNetAddress.ParsedType switch
{
ParsedNetAddressStringType.SubnetMask => new(parsedNetAddress.SubnetMask.GetAddressBytes()),
ParsedNetworkAddressStringType.SubnetMask => new(parsedNetAddress.SubnetMask!.GetAddressBytes()),
_ => new(parsedNetAddress.CidrNotation)
};

_networkAddress = GetSubnetBoundary(parsedNetAddress.IPAddress, _subnetMask);
_broadcastAddress = GetBroadcastAddress(_networkAddress, _subnetMask);
_usableHostRange = new(_networkAddress, _broadcastAddress);
NetworkAddress = GetSubnetBoundary(parsedNetAddress.IPAddress, SubnetMask);
BroadcastAddress = GetBroadcastAddress(NetworkAddress, SubnetMask);
UsableHostRange = new(NetworkAddress, BroadcastAddress);
}

/// <summary>
/// The network address of the subnet.
/// </summary>
public IPAddress NetworkAddress
{
get => _networkAddress;
}
public IPAddress NetworkAddress { get; }

/// <summary>
/// The subnet mask of the subnet.
/// </summary>
public IPv4SubnetMask SubnetMask
{
get => _subnetMask;
}
public IPv4SubnetMask SubnetMask { get; }

/// <summary>
/// The CIDR mask of the subnet.
/// </summary>
public double CidrMask
{
get => _subnetMask.CidrNotation;
get => SubnetMask.CidrNotation;
}

/// <summary>
/// The broadcast address of the subnet.
/// </summary>
public IPAddress BroadcastAddress
{
get => _broadcastAddress;
}
public IPAddress BroadcastAddress { get; }

/// <summary>
/// The total amount of addresses in the subnet.
/// </summary>
public double TotalAddresses
{
get => _subnetMask.TotalAddresses;
get => SubnetMask.TotalAddresses;
}

/// <summary>
/// The total amount of usable addresses in the subnet.
/// </summary>
public double UsableAddresses
{
get => _subnetMask.TotalAddresses - 2;
get => SubnetMask.TotalAddresses - 2;
}

/// <summary>
/// The range of hosts available for use in the subnet.
/// </summary>
public UsableHostRange UsableHostRange
{
get => _usableHostRange;
}

private readonly IPAddress _networkAddress;
private readonly IPv4SubnetMask _subnetMask;
private readonly IPAddress _broadcastAddress;
private readonly UsableHostRange _usableHostRange;
public UsableHostRange UsableHostRange { get; }

/// <summary>
/// Display the subnet as a string.
/// </summary>
/// <returns>A string representation of the subnet with the network address and CIDR mask.</returns>
public override string ToString()
{
return $"{_networkAddress}/{CidrMask}";
return $"{NetworkAddress}/{CidrMask}";
}

/// <summary>
Expand Down
Loading