Skip to content

Commit

Permalink
Changed SupportedAlgorithms class to Registry Design Pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
arabasso committed Jan 27, 2019
1 parent 7cb108e commit 7a7926c
Showing 1 changed file with 57 additions and 39 deletions.
96 changes: 57 additions & 39 deletions source/Octodiff/Core/SupportedAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,77 @@

namespace Octodiff.Core
{
public static class SupportedAlgorithms
public interface IHashingSupportedAlgorithm
{
public static class Hashing
IHashAlgorithm Default();
IHashAlgorithm Create(string algorithm);
}

public class DefaultHashingSupportedAlgorithm
: IHashingSupportedAlgorithm
{
public IHashAlgorithm Sha1()
{
public static IHashAlgorithm Sha1()
{
return new HashAlgorithmWrapper("SHA1", SHA1.Create());
}
return new HashAlgorithmWrapper("SHA1", SHA1.Create());
}

public static IHashAlgorithm Default()
{
return Sha1();
}
public virtual IHashAlgorithm Default()
{
return Sha1();
}

public static IHashAlgorithm Create(string algorithm)
{
if (algorithm == "SHA1")
return Sha1();
public virtual IHashAlgorithm Create(string algorithm)
{
if (algorithm == "SHA1")
return Sha1();

throw new CompatibilityException(
$"The hash algorithm '{algorithm}' is not supported in this version of Octodiff");
}
throw new CompatibilityException(
$"The hash algorithm '{algorithm}' is not supported in this version of Octodiff");
}
}

public static class Checksum
{
public interface IChecksumSupportedAlgorithm
{
IRollingChecksum Default();
IRollingChecksum Create(string algorithm);
}

public class DefaultChecksumSupportedAlgorithm
: IChecksumSupportedAlgorithm
{
#pragma warning disable 618
public static IRollingChecksum Adler32Rolling(bool useV2 = false)
{
if (useV2)
return new Adler32RollingChecksumV2();
public IRollingChecksum Adler32Rolling(bool useV2 = false)
{
if (useV2)
return new Adler32RollingChecksumV2();

return new Adler32RollingChecksum();
}
return new Adler32RollingChecksum();
}
#pragma warning restore 618

public static IRollingChecksum Default()
{
return Adler32Rolling();
}
public virtual IRollingChecksum Default()
{
return Adler32Rolling();
}

public static IRollingChecksum Create(string algorithm)
public virtual IRollingChecksum Create(string algorithm)
{
switch (algorithm)
{
switch (algorithm)
{
case "Adler32":
return Adler32Rolling();
case "Adler32V2":
return Adler32Rolling(true);
}
throw new CompatibilityException(
$"The rolling checksum algorithm '{algorithm}' is not supported in this version of Octodiff");
case "Adler32":
return Adler32Rolling();
case "Adler32V2":
return Adler32Rolling(true);
}
throw new CompatibilityException(
$"The rolling checksum algorithm '{algorithm}' is not supported in this version of Octodiff");
}
}

public static class SupportedAlgorithms
{
public static IHashingSupportedAlgorithm Hashing { get; set; } = new DefaultHashingSupportedAlgorithm();

public static IChecksumSupportedAlgorithm Checksum { get; set; } = new DefaultChecksumSupportedAlgorithm();
}
}

0 comments on commit 7a7926c

Please sign in to comment.