Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.1.8 #21

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 136 additions & 0 deletions Assets/BetterLocators/Runtime/Extensions/ILocatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using Better.Commons.Runtime.Utility;
using Better.Locators.Runtime;

public static class ILocatorExtensions
{
public static bool TryAdd<TElement>(this ILocator<Type, TElement> self, TElement element)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return false;
}

if (element == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(element));
return false;
}

var key = element.GetType();
return self.TryAdd(key, element);
}

public static void Add<TKey, TElement>(this ILocator<TKey, TElement> self, TKey key, TElement element)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return;
}

if (key == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(key));
return;
}

if (element == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(element));
return;
}

if (!self.TryAdd(key, element))
{
var message = $"Invalid adding {nameof(element)} by {nameof(key)}({key})";
DebugUtility.LogException<ArgumentNullException>(message);
}
}

public static void Add<TElement>(this ILocator<Type, TElement> self, TElement element)
{
if (element == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(element));
return;
}

var key = element.GetType();
self.Add(key, element);
}

public static bool ContainsKey<TElement, TDerived>(this ILocator<Type, TElement> self)
where TDerived : TElement
{
var key = typeof(TDerived);
return self.ContainsKey(key);
}

public static bool TryGet<TKey, TElement, TDerived>(this ILocator<TKey, TElement> self, TKey key, out TDerived element)
where TDerived : TElement
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
element = default;
return false;
}

if (self.TryGet(key, out var located)
&& located is TDerived casted)
{
element = casted;
return true;
}

element = default;
return false;
}

public static bool TryGet<TElement, TDerived>(this ILocator<Type, TElement> self, out TDerived element)
where TDerived : TElement
{
var key = typeof(TDerived);
return self.TryGet(key, out element);
}

public static TElement Get<TKey, TElement>(this ILocator<TKey, TElement> self, TKey key)
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return default;
}

if (!self.TryGet(key, out var element))
{
var message = $"Invalid getting {nameof(element)} by {nameof(key)}({key})";
DebugUtility.LogException<InvalidOperationException>(message);
return default;
}

return element;
}

public static TDerived Get<TElement, TDerived>(this ILocator<Type, TElement> self)
where TDerived : TElement
{
if (self == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(self));
return default;
}

var type = typeof(TDerived);
var element = (TDerived)self.Get(type);
if (element == null)
{
var message = $"Invalid getting {nameof(element)} by {nameof(type)}({type})";
DebugUtility.LogException<InvalidOperationException>(message);
}

return element;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/BetterLocators/Runtime/Implementations.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions Assets/BetterLocators/Runtime/Implementations/Locator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Better.Commons.Runtime.Utility;

namespace Better.Locators.Runtime
{
public class Locator<TKey, TElement> : ILocator<TKey, TElement>
{
public event Action Changed;
public event OnElementChanged ElementAdded;
public event OnElementChanged ElementRemoved;

private Dictionary<TKey, TElement> _sourceMap;

public int Count => _sourceMap.Count;

public Locator()
{
_sourceMap = new();
}

public delegate void OnElementChanged(TKey key, TElement element);

public bool TryAdd(TKey key, TElement element)
{
if (element == null)
{
return false;
}

if (ContainsElement(element))
{
return false;
}

_sourceMap.Add(key, element);
OnAdded(key, element);
return true;
}

protected virtual void OnAdded(TKey key, TElement element)
{
ElementAdded?.Invoke(key, element);
OnChanged();
}

public bool ContainsKey(TKey key)
{
if (key == null)
{
DebugUtility.LogException<InvalidOperationException>(nameof(key));
return false;
}

return _sourceMap.ContainsKey(key);
}

public bool ContainsElement(TElement element)
{
if (element == null)
{
DebugUtility.LogException<InvalidOperationException>(nameof(element));
return false;
}

return _sourceMap.ContainsValue(element);
}

public TKey[] GetKeys()
{
return _sourceMap.Keys.ToArray();
}

public TElement[] GetElements()
{
return _sourceMap.Values.ToArray();
}

public bool TryGet(TKey key, out TElement element)
{
if (key == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(key));
element = default;
return false;
}

return _sourceMap.TryGetValue(key, out element);
}

public bool Remove(TKey key)
{
if (key == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(key));
return false;
}

var removed = _sourceMap.Remove(key, out var element);
if (removed)
{
OnRemoved(key, element);
}

return removed;
}

public bool Remove(TElement element)
{
if (element == null)
{
DebugUtility.LogException<ArgumentNullException>(nameof(element));
return false;
}

var removed = true; TKey key = default; //_sourceMap.Remove(element, out var key); TODO: Commons DictionaryExtensions dependency
if (removed)
{
OnRemoved(key, element);
}

return removed;
}

protected virtual void OnRemoved(TKey key, TElement element)
{
ElementRemoved?.Invoke(key, element);
OnChanged();
}

public void Clear()
{
_sourceMap.Clear();
OnChanged();
}

protected virtual void OnChanged()
{
Changed?.Invoke();
}
}

public class Locator<TElement> : Locator<Type, TElement>
{
}
}
3 changes: 3 additions & 0 deletions Assets/BetterLocators/Runtime/Implementations/Locator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading