| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FileExtension.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | namespace MyNet.IO.FileExtensions; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents a file extension, ensuring it is normalized (lowercase and starts with a dot). |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class FileExtension |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="FileExtension"/> class with the specified extension value. The cons |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="value">The file extension value.</param> |
| | | 21 | | /// <exception cref="ArgumentException">Thrown when the <paramref name="value"/> is null, empty, or consists only of |
| | | 22 | | public FileExtension(string value) |
| | | 23 | | { |
| | 147 | 24 | | if (string.IsNullOrWhiteSpace(value)) |
| | 0 | 25 | | throw new ArgumentException("Extension cannot be null or empty.", nameof(value)); |
| | | 26 | | |
| | 147 | 27 | | Value = Normalize(value); |
| | 147 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the normalized file extension value. The value is stored in lowercase and starts with a dot, ensuring consi |
| | | 32 | | /// </summary> |
| | | 33 | | public string Value { get; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Normalizes the file extension by trimming whitespace, converting it to lowercase, and ensuring it starts with a |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="extension">The file extension to normalize.</param> |
| | | 39 | | /// <returns>The normalized file extension.</returns> |
| | | 40 | | private static string Normalize(string extension) |
| | | 41 | | { |
| | 147 | 42 | | extension = extension.Trim().ToLower(CultureInfo.CurrentCulture); |
| | 147 | 43 | | return extension.StartsWith('.') ? extension : $".{extension}"; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Returns a string representation of the file extension, which is the normalized value. This method overrides the |
| | | 48 | | /// </summary> |
| | | 49 | | /// <returns>The normalized file extension value.</returns> |
| | 3 | 50 | | public override string ToString() => Value; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Determines whether the specified object is equal to the current file extension. Two <see cref="FileExtension"/> |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="obj">The object to compare with the current file extension.</param> |
| | | 56 | | /// <returns><c>true</c> if the specified object is equal to the current file extension; otherwise, <c>false</c>.</r |
| | 3 | 57 | | public override bool Equals(object? obj) => obj is FileExtension other && Value == other.Value; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Returns a hash code for the file extension, which is based on the normalized value. This method overrides the de |
| | | 61 | | /// </summary> |
| | | 62 | | /// <returns>A hash code for the current file extension.</returns> |
| | 6 | 63 | | public override int GetHashCode() => Value.GetHashCode(StringComparison.OrdinalIgnoreCase); |
| | | 64 | | } |
| | | 65 | | |