SectionPairSection.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-13
  6. * Changed: 2023-02-15
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  10. #include <ls-std/core/evaluator/IndexOutOfBoundsEvaluator.hpp>
  11. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  12. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  13. #include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
  14. #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
  15. ls::std::io::SectionPairSection::SectionPairSection(const ls::std::io::section_pair_identifier &_sectionId) : ls::std::core::Class("SectionPairSection")
  16. {
  17. this->_setSectionId(_sectionId);
  18. }
  19. ls::std::io::SectionPairSection::~SectionPairSection() = default;
  20. void ls::std::io::SectionPairSection::add(const section_pair_row_list_element &_row)
  21. {
  22. ls::std::core::NullPointerArgumentEvaluator{::std::reinterpret_pointer_cast<void>(_row)}.evaluate();
  23. this->_rowExistenceCheck(_row->getKey());
  24. this->rows.push_back(_row);
  25. }
  26. ls::std::io::section_pair_row_list_element ls::std::io::SectionPairSection::get(size_t _index)
  27. {
  28. ls::std::core::IndexOutOfBoundsEvaluator{_index, this->rows.size()}.evaluate();
  29. ls::std::io::section_pair_row_list_element element{};
  30. size_t index{};
  31. for (const auto &_element : this->rows)
  32. {
  33. if (index == _index)
  34. {
  35. element = _element;
  36. break;
  37. }
  38. ++index;
  39. }
  40. return element;
  41. }
  42. ls::std::io::section_pair_row_list ls::std::io::SectionPairSection::getList()
  43. {
  44. return this->rows;
  45. }
  46. size_t ls::std::io::SectionPairSection::getRowAmount()
  47. {
  48. return this->rows.size();
  49. }
  50. ls::std::io::section_pair_identifier ls::std::io::SectionPairSection::getSectionId()
  51. {
  52. return this->sectionId;
  53. }
  54. void ls::std::io::SectionPairSection::setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
  55. {
  56. this->_setSectionId(_sectionId);
  57. }
  58. bool ls::std::io::SectionPairSection::_hasRow(const ls::std::io::section_pair_identifier &_key)
  59. {
  60. bool rowExists{};
  61. for (const auto &_row : this->rows)
  62. {
  63. if (_row->getKey() == _key)
  64. {
  65. rowExists = true;
  66. break;
  67. }
  68. }
  69. return rowExists;
  70. }
  71. void ls::std::io::SectionPairSection::_rowExistenceCheck(const ls::std::io::section_pair_identifier &_key)
  72. {
  73. if (this->_hasRow(_key))
  74. {
  75. ::std::string message = this->getClassName() + ": row key \"" + _key + "\" already exists in section \"" + this->sectionId + "\"!";
  76. throw ls::std::core::IllegalArgumentException{message};
  77. }
  78. }
  79. void ls::std::io::SectionPairSection::_setSectionId(const ls::std::io::section_pair_identifier &_sectionId)
  80. {
  81. ls::std::core::EmptyStringArgumentEvaluator{_sectionId}.evaluate();
  82. ls::std::io::SectionPairIdentifierArgumentEvaluator(_sectionId, this->getClassName() + ": argument \"_sectionId\" contains invalid characters!").evaluate();
  83. this->sectionId = _sectionId;
  84. }