// Copyright (C) Stichting Deltares 2018. 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 System.Linq;
namespace Deltares.Dam.Data
{
public class ObjectMaterializer
{
private struct SetterInfo
{
public Action Function;
public bool Required;
}
///
/// Holds the property setter actions with the name as the key of the setter
///
private readonly Dictionary dict;
///
/// Holds the list of actions in a list so it is accessible with an index
///
private List setters;
public ObjectMaterializer()
{
this.dict = new Dictionary();
this.setters = new List();
}
///
/// Adds a new property setter
///
/// The name of the setter
/// The setter lambda expression
///
public void Add(string setterName, Action func, bool required)
{
SetterInfo columnInfo = new SetterInfo() { Function = func, Required = required };
dict.Add(setterName, columnInfo);
}
public void Add(string setterName, Action func)
{
Add(setterName, func, true);
}
///
/// Removes a property setter
///
/// The name of the setter
public void Remove(string setterName)
{
dict.Remove(setterName);
setters = dict.Values.ToList();
}
///
/// Returns the mapped keys
///
public IEnumerable MappingKeys
{
get { return this.dict.Keys; }
}
public bool IsRequired(string setterName)
{
return this.dict.ContainsKey(setterName) && this.dict[setterName].Required;
}
///
/// Gets the setter action by index
///
/// The index of the property set expression
///
public Action this[int index]
{
get
{
if (setters == null)
setters = dict.Values.ToList();
return setters[index].Function;
}
}
///
/// Gets the setter action by name
///
/// The name of the property set expression
///
public Action this[string name]
{
get
{
return dict[name].Function;
}
}
///
/// Gets the number of setter items
///
public int Count
{
get { return dict.Count; }
}
}
}