KPublicTransport

identifier.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "identifier_p.h"
7
8#include <QJsonArray>
9#include <QJsonObject>
10
11#include <algorithm>
12
13using namespace Qt::Literals;
14using namespace KPublicTransport;
15
16bool IdentifierSet::hasIdentifier(QAnyStringView type) const
17{
18 return std::ranges::find_if(m_identifiers, [type](const auto &id) {
19 return id.type == type;
20 }) != m_identifiers.end();
21}
22
23QString IdentifierSet::identifier(QAnyStringView type) const
24{
25 const auto it = std::ranges::find_if(m_identifiers, [type](const auto &id) {
26 return id.type == type;
27 });
28 return it == m_identifiers.end() ? QString() : (*it).value;
29}
30
31void IdentifierSet::setIdentifier(const QString &type, const QString &value)
32{
33 const auto it = std::ranges::find_if(m_identifiers, [type](const auto &id) {
34 return id.type == type;
35 });
36 if (it == m_identifiers.end()) {
37 if (!value.isEmpty()) {
38 m_identifiers.emplace_back(type, value);
39 }
40 } else {
41 if (value.isEmpty()) {
42 m_identifiers.erase(it);
43 } else {
44 (*it).value = value;
45 }
46 }
47}
48
49QStringList IdentifierSet::identifierTypes() const
50{
51 QStringList l;
52 l.reserve((qsizetype)m_identifiers.size());
53 std::ranges::transform(m_identifiers, std::back_inserter(l), [](const auto &id) { return id.type; });
54 return l;
55}
56
57IdentifierSet::PartialCompare IdentifierSet::compare(const IdentifierSet &other) const
58{
59 bool foundEqualId = false;
60 for (const auto &id :m_identifiers) {
61 const auto otherValue = other.identifier(id.type);
62 if (id.value.isEmpty() || otherValue.isEmpty()) {
63 continue;
64 }
65 if (id.value == otherValue) {
66 foundEqualId = true;
67 } else {
68 return NotEqual;
69 }
70 }
71 return foundEqualId ? Equal : NoIntersection;
72}
73
74void IdentifierSet::merge(const IdentifierSet &other)
75{
76 for (const auto &otherId : other.m_identifiers) {
77 if (otherId.value.isEmpty()) {
78 continue;
79 }
80 const auto it = std::ranges::find_if(m_identifiers, [&otherId](const auto &id) {
81 return id.type == otherId.type;
82 });
83 if (it == m_identifiers.end()) {
84 m_identifiers.push_back(otherId);
85 } else if ((*it).value.isEmpty()) {
86 (*it).value = otherId.value;
87 }
88 }
89}
90
91QJsonObject IdentifierSet::toJson() const
92{
93 QJsonObject obj;
94 for (const auto &id : m_identifiers) {
95 obj.insert(id.type, id.value);
96 }
97 return obj;
98}
99
100void IdentifierSet::fromJson(const QJsonObject &ids)
101{
102 m_identifiers.clear();
103 m_identifiers.reserve(ids.size());
104 for (auto it = ids.begin(); it != ids.end(); ++it) {
105 m_identifiers.emplace_back(it.key(), it.value().toString());
106 }
107}
Query operations and data types for accessing realtime public transport information from online servi...
iterator begin()
iterator end()
iterator insert(QLatin1StringView key, const QJsonValue &value)
qsizetype size() const const
void reserve(qsizetype size)
iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:50:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.