sourcegit/src/Models/User.cs

50 lines
1.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
2020-07-03 00:24:31 -07:00
namespace SourceGit.Models
{
public class User
{
public static readonly User Invalid = new User();
2020-07-03 00:24:31 -07:00
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
2020-07-03 00:24:31 -07:00
2024-07-14 09:30:31 -07:00
public User()
{
// Only used by User.Invalid
}
public User(string data)
{
var nameEndIdx = data.IndexOf('±', StringComparison.Ordinal);
Name = nameEndIdx > 0 ? data.Substring(0, nameEndIdx) : string.Empty;
Email = data.Substring(nameEndIdx + 1);
_hash = data.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj == null || !(obj is User))
return false;
var other = obj as User;
return Name == other.Name && Email == other.Email;
}
2020-07-03 00:24:31 -07:00
public override int GetHashCode()
{
2024-07-14 09:30:31 -07:00
return _hash;
}
2020-07-03 00:24:31 -07:00
public static User FindOrAdd(string data)
{
2024-07-14 09:30:31 -07:00
return _caches.GetOrAdd(data, key => new User(key));
2020-07-03 00:24:31 -07:00
}
private static ConcurrentDictionary<string, User> _caches = new ConcurrentDictionary<string, User>();
2024-07-14 09:30:31 -07:00
private readonly int _hash;
2020-07-03 00:24:31 -07:00
}
}