// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using NetTopologySuite.Features;
namespace Deltares.Maps;
public class AttributesDictionary : IAttributesTable
{
private readonly IAttributesTable attributes;
private readonly Dictionary attributeNames = new Dictionary();
public AttributesDictionary(IAttributesTable table)
{
attributes = table;
}
#region Implementation of IAttributesTable
private static void ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(string attributeName)
{
if (string.IsNullOrEmpty(attributeName))
{
throw new ArgumentNullException("attributeName");
}
}
public void Add(string attributeName, object value)
{
ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(attributeName);
if (value == null)
{
throw new ArgumentNullException("value");
}
attributes.Add(attributeName, value);
}
public void DeleteAttribute(string attributeName)
{
ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(attributeName);
string attrName = GetExistingName(attributeName);
if (attributes.Exists(attrName))
{
attributes.DeleteAttribute(attrName);
}
}
public Type GetType(string attributeName)
{
ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(attributeName);
string attrName = GetExistingName(attributeName);
return this[attrName].GetType();
}
public bool Exists(string attributeName)
{
ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(attributeName);
return !string.IsNullOrEmpty(GetExistingName(attributeName));
}
public string[] GetNames()
{
return attributes.GetNames();
}
public object[] GetValues()
{
return attributes.GetValues();
}
public object GetOptionalValue(string attributeName)
{
return null;
}
public object this[string attributeName]
{
get
{
ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(attributeName);
string attrName = GetExistingName(attributeName);
return attributes[attrName];
}
set
{
ThrowArgumentNullExceptionWhenAttributeIsNullOrEmpty(attributeName);
string attrName = GetExistingName(attributeName);
attributes[attrName] = value;
}
}
public int Count
{
get
{
return attributes.Count;
}
}
private string GetExistingName(string name)
{
if (!attributeNames.ContainsKey(name))
{
attributeNames[name] = string.Empty;
foreach (string existingName in attributes.GetNames())
{
if (existingName.Equals(name, StringComparison.OrdinalIgnoreCase))
{
attributeNames[name] = existingName;
}
}
}
return attributeNames[name];
}
#endregion
}