diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CacheEnum.cs b/Assets/UniGLTF/Runtime/UniGLTF/Util/CacheEnum.cs deleted file mode 100644 index 5c90f6282..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/Util/CacheEnum.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; - -namespace UniGLTF -{ - public sealed class CacheEnum - { - public static T Parse(string name, bool ignoreCase = false) where T : struct, Enum - { - if(ignoreCase) - { - return CacheParse.ParseIgnoreCase(name); - } - else - { - return CacheParse.Parse(name); - } - } - - public static T TryParseOrDefault(string name, bool ignoreCase = false, T defaultValue=default(T)) where T : struct, Enum - { - try - { - if(ignoreCase) - { - return CacheParse.ParseIgnoreCase(name); - } - else - { - return CacheParse.Parse(name); - } - } - catch - { - return defaultValue; - } - } - - public static T[] GetValues() where T : struct, Enum - { - return CacheValues.Values; - } - - private static class CacheParse where T : struct, Enum - { - private static Dictionary _values = new Dictionary(); - private static Dictionary _ignoreCaseValues = new Dictionary(StringComparer.OrdinalIgnoreCase); - - static CacheParse() - { - } - - public static T ParseIgnoreCase(string name) - { - if(_ignoreCaseValues.TryGetValue(name, out var value)) - { - return value; - } - else - { - T result; - value = Enum.TryParse(name, true, out result) - ? result - : throw new ArgumentException(nameof(result)); - _ignoreCaseValues.Add(name, value); - return value; - } - } - - public static T Parse(string name) - { - if(_values.TryGetValue(name, out var value)) - { - return value; - } - else - { - T result; - value = Enum.TryParse(name, false, out result) - ? result - : throw new ArgumentException(nameof(result)); - _values.Add(name, value); - return value; - } - } - } - - private static class CacheValues where T : struct, Enum - { - public static readonly T[] Values; - - static CacheValues() - { - Values = Enum.GetValues(typeof(T)) as T[]; - } - } - } -} - diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs index 7cad86e75..702350931 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnum.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; namespace UniGLTF { @@ -7,14 +7,7 @@ namespace UniGLTF { public static T Parse(string name, bool ignoreCase = false) where T : struct, Enum { - if (ignoreCase) - { - return CachedEnumType.ParseIgnoreCase(name); - } - else - { - return CachedEnumType.Parse(name); - } + return CachedEnumType.Parse(name, ignoreCase); } public static T TryParseOrDefault(string name, bool ignoreCase = false, T defaultValue = default) @@ -22,14 +15,7 @@ namespace UniGLTF { try { - if (ignoreCase) - { - return CachedEnumType.ParseIgnoreCase(name); - } - else - { - return CachedEnumType.Parse(name); - } + return Parse(name, ignoreCase: ignoreCase); } catch { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs index e3983140d..51b59d0ea 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Util/CachedEnum/CachedEnumType.cs @@ -5,8 +5,8 @@ namespace UniGLTF { internal static class CachedEnumType where T : struct, Enum { - private static Dictionary _values = new Dictionary(); - private static Dictionary _ignoreCaseValues = new Dictionary(StringComparer.OrdinalIgnoreCase); + private static readonly Dictionary _values = new Dictionary(); + private static readonly Dictionary _ignoreCaseValues = new Dictionary(StringComparer.OrdinalIgnoreCase); private static T[] _allValues; public static T[] Values @@ -22,38 +22,21 @@ namespace UniGLTF } } - public static T ParseIgnoreCase(string name) + public static T Parse(string name, bool ignoreCase) { - if(_ignoreCaseValues.TryGetValue(name, out var value)) + var caches = ignoreCase ? _ignoreCaseValues : _values; + if (caches.TryGetValue(name, out var ignoreCaseValue)) { - return value; + return ignoreCaseValue; } - else - { - T result; - value = Enum.TryParse(name, true, out result) - ? result - : throw new ArgumentException(nameof(result)); - _ignoreCaseValues.Add(name, value); - return value; - } - } - public static T Parse(string name) - { - if(_values.TryGetValue(name, out var value)) + if (Enum.TryParse(name, ignoreCase, out var result)) { - return value; - } - else - { - T result; - value = Enum.TryParse(name, false, out result) - ? result - : throw new ArgumentException(nameof(result)); - _values.Add(name, value); - return value; + _values.Add(name, result); + return result; } + + throw new ArgumentException(name); } } } \ No newline at end of file