|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2021, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.Linq; |
| 26 | +using System.Reflection; |
| 27 | + |
| 28 | +namespace BH.oM.Base |
| 29 | +{ |
| 30 | + public abstract class Enumeration : IObject, IComparable, IEnum // TODO: Fix serialisation so we can remove the inheritance from BHoMObject |
| 31 | + { |
| 32 | + /***************************************************/ |
| 33 | + /**** Properties ****/ |
| 34 | + /***************************************************/ |
| 35 | + |
| 36 | + public string Value { get; private set; } |
| 37 | + |
| 38 | + public string Description |
| 39 | + { |
| 40 | + get |
| 41 | + { |
| 42 | + return m_Description.Length > 0 ? m_Description : Value; |
| 43 | + } |
| 44 | + private set |
| 45 | + { |
| 46 | + m_Description = value; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /***************************************************/ |
| 52 | + /**** Constructors ****/ |
| 53 | + /***************************************************/ |
| 54 | + |
| 55 | + protected Enumeration(string value, string description = "") |
| 56 | + { |
| 57 | + Value = value; |
| 58 | + m_Description = description; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /***************************************************/ |
| 63 | + /**** Public Methods ****/ |
| 64 | + /***************************************************/ |
| 65 | + |
| 66 | + public override string ToString() |
| 67 | + { |
| 68 | + return Value; |
| 69 | + } |
| 70 | + |
| 71 | + /***************************************************/ |
| 72 | + |
| 73 | + public override bool Equals(object obj) |
| 74 | + { |
| 75 | + Enumeration otherValue = obj as Enumeration; |
| 76 | + if (otherValue == null) |
| 77 | + return false; |
| 78 | + |
| 79 | + return GetType().Equals(obj.GetType()) |
| 80 | + && Value.Equals(otherValue.Value); |
| 81 | + } |
| 82 | + |
| 83 | + /***************************************************/ |
| 84 | + |
| 85 | + public override int GetHashCode() |
| 86 | + { |
| 87 | + return Value.GetHashCode(); |
| 88 | + } |
| 89 | + |
| 90 | + /***************************************************/ |
| 91 | + |
| 92 | + public int CompareTo(object other) |
| 93 | + { |
| 94 | + return Value.CompareTo(((Enumeration)other).Value); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /***************************************************/ |
| 99 | + /**** Private Fields ****/ |
| 100 | + /***************************************************/ |
| 101 | + |
| 102 | + private string m_Description = ""; |
| 103 | + |
| 104 | + |
| 105 | + /***************************************************/ |
| 106 | + /**** Static Methods ****/ |
| 107 | + /***************************************************/ |
| 108 | + |
| 109 | + public static bool Equals(IEnum a, IEnum b) |
| 110 | + { |
| 111 | + if (a == null || b == null) |
| 112 | + return false; |
| 113 | + |
| 114 | + return a.GetType().Equals(b.GetType()) |
| 115 | + && a.Value.Equals(b.Value); |
| 116 | + } |
| 117 | + |
| 118 | + /***************************************************/ |
| 119 | + |
| 120 | + public static IEnumerable<T> GetAll<T>() where T : Enumeration |
| 121 | + { |
| 122 | + return typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly) |
| 123 | + .Select(f => f.GetValue(null)) |
| 124 | + .Cast<T>(); |
| 125 | + } |
| 126 | + |
| 127 | + /***************************************************/ |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments