File indexing completed on 2024-12-29 04:49:58

0001 /*
0002     SPDX-FileCopyrightText: 2019-2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "extractorresult.h"
0008 
0009 #include <KItinerary/JsonLdDocument>
0010 
0011 using namespace KItinerary;
0012 
0013 ExtractorResult::ExtractorResult() = default;
0014 
0015 ExtractorResult::ExtractorResult(const QJsonArray &result)
0016     : m_jsonLdResult(result)
0017 {
0018 }
0019 
0020 ExtractorResult::ExtractorResult(const QList<QVariant> &result)
0021     : m_result(result) {}
0022 
0023 ExtractorResult::~ExtractorResult() = default;
0024 
0025 bool ExtractorResult::isEmpty() const
0026 {
0027     return m_result.isEmpty() && m_jsonLdResult.isEmpty();
0028 }
0029 
0030 int ExtractorResult::size() const
0031 {
0032     return std::max(m_result.size(), m_jsonLdResult.size());
0033 }
0034 
0035 QJsonArray ExtractorResult::jsonLdResult() const
0036 {
0037     if (m_jsonLdResult.isEmpty()) {
0038         m_jsonLdResult = JsonLdDocument::toJson(m_result);
0039     }
0040     return m_jsonLdResult;
0041 }
0042 
0043 QList<QVariant> ExtractorResult::result() const {
0044     if (m_result.isEmpty()) {
0045         m_result = JsonLdDocument::fromJson(m_jsonLdResult);
0046     }
0047     return m_result;
0048 }
0049 
0050 void ExtractorResult::append(ExtractorResult &&other)
0051 {
0052     if (other.isEmpty()) {
0053         return;
0054     }
0055 
0056     if (isEmpty()) {
0057         m_result = std::move(other.m_result);
0058         m_jsonLdResult = std::move(other.m_jsonLdResult);
0059         return;
0060     }
0061 
0062     if (!m_result.isEmpty()) {
0063         auto r = other.result();
0064         m_result.reserve(m_result.size() + r.size());
0065         std::copy(r.begin(), r.end(), std::back_inserter(m_result));
0066     }
0067     if (!m_jsonLdResult.isEmpty()) {
0068         auto r = other.jsonLdResult();
0069         std::copy(r.begin(), r.end(), std::back_inserter(m_jsonLdResult));
0070     }
0071 }