Skip to content

Commit

Permalink
feat: Add create new object if not exists when add to directions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandini committed Mar 26, 2021
1 parent 612e99c commit a4892b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/RightTurn/ITurnDirections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public interface ITurnDirections
{
T Add<T>() where T : new();
T Add<T>(T value);
T Get<T>();
T TryGet<T>();
Expand Down
15 changes: 12 additions & 3 deletions src/RightTurn/TurnDirections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
namespace RightTurn
{
internal class TurnDirections : Dictionary<Type, object>, ITurnDirections
{
{
public T Add<T>(T value)
{
this[typeof(T)] = value;
return value;
}

public T Add<T>() where T : new()
{
if (ContainsKey(typeof(T)))
return (T)this[typeof(T)];

var value = new T();
this[typeof(T)] = value;
return value;
}

public bool Have<T>()
{
return ContainsKey(typeof(T));
Expand All @@ -32,7 +42,6 @@ public T TryGet<T>()
return default;

return (T)this[typeof(T)];
}

}
}
}

0 comments on commit a4892b5

Please sign in to comment.