Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileExtension.cs « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7838211460d943dcfe6909a4864b4a3b3c5a05e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */

using System.Collections.Generic;

namespace UVtools.Core
{
    /// <summary>
    /// Represents a file extension for slicer file formats
    /// </summary>
    public sealed class FileExtension
    {
        #region Properties
        /// <summary>
        /// Gets the extension name without the dot (.)
        /// </summary>
        public string Extension { get; }
        
        /// <summary>
        /// Gets the extension description
        /// </summary>
        public string Description { get; }

        /// <summary>
        /// Gets the file filter for open and save dialogs
        /// </summary>
        public string Filter => $@"{Description} (*.{Extension})|*.{Extension}";
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="extension">The extension name without the dot (.)</param>
        /// <param name="description">The extension description</param>
        public FileExtension(string extension, string description)
        {
            Extension = extension;
            Description = description;
        }
        #endregion

        #region Overrides

        public override string ToString()
        {
            return $"{nameof(Extension)}: {Extension}, {nameof(Description)}: {Description}";
        }

        private bool Equals(FileExtension other)
        {
            return Extension == other.Extension;
        }

        public override bool Equals(object obj)
        {
            return ReferenceEquals(this, obj) || obj is FileExtension other && Equals(other);
        }

        public override int GetHashCode()
        {
            return (Extension != null ? Extension.GetHashCode() : 0);
        }

        private sealed class ExtensionEqualityComparer : IEqualityComparer<FileExtension>
        {
            public bool Equals(FileExtension x, FileExtension y)
            {
                if (ReferenceEquals(x, y)) return true;
                if (ReferenceEquals(x, null)) return false;
                if (ReferenceEquals(y, null)) return false;
                if (x.GetType() != y.GetType()) return false;
                return x.Extension == y.Extension;
            }

            public int GetHashCode(FileExtension obj)
            {
                return (obj.Extension != null ? obj.Extension.GetHashCode() : 0);
            }
        }

        public static IEqualityComparer<FileExtension> ExtensionComparer { get; } = new ExtensionEqualityComparer();
        #endregion
    }
}