SearchBox/SearchBox/IdMapper.cs

50 lines
1.1 KiB
C#

using System;
using Stackoverflow.Utilities;
namespace SearchBox
{
public class IdNotFoundException : Exception { public IdNotFoundException(string message) : base(message) { } }
public class IdMapper
{
private int nextId = 0;
public BiDictionary<int, string> map = new BiDictionary<int, string>();
public IdMapper()
{
}
public int GetId(string pageName)
{
// Perform unicode normalization
pageName = pageName.Normalize(System.Text.NormalizationForm.FormC);
int result;
if (!map.TryGetBySecond(pageName, out result)) {
map.Add(result = nextId++, pageName);
}
return result;
}
public string GetPageName(int id)
{
string result;
if (!map.TryGetByFirst(id, out result))
throw new IdNotFoundException($"Error: Couldn't find {id} in the ID map.");
return result;
}
public void MovePageName(string oldPageName, string newPageName)
{
int id = map.GetBySecond(oldPageName);
map.RemoveBySecond(oldPageName);
map.Add(id, newPageName);
}
public void DeletePageName(string pageName)
{
map.RemoveBySecond(pageName);
}
}
}