Keyple Util C++ Library - 2.4.0
Component of the Keyple C++ middleware
KeypleAssert.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/core/util/KeypleAssert.hpp"
15
16#include <string>
17#include <vector>
18
19#include "keyple/core/util/HexUtil.hpp"
20
21namespace keyple {
22namespace core {
23namespace util {
24
25const char* Assert::ARGUMENT = "Argument [";
26const char* Assert::CONDITION = "Condition [";
27const char* Assert::HAS_A_VALUE = "] has a value [";
28const char* Assert::LESS_THAN = "] less than [";
29const char* Assert::GREATER_THAN = "] greater than [";
30const char* Assert::IS_NULL = "] is null.";
31const char* Assert::IS_EMPTY = "] is empty.";
32const char* Assert::IS_FALSE = "] is false.";
33const char* Assert::IS_NOT_HEX = "] is not a hex string.";
34const char* Assert::NOT_EQUAL_TO = "] not equal to [";
35const char* Assert::CLOSING_BRACKET = "].";
36
37Assert::Assert()
38{
39}
40
41Assert&
42Assert::getInstance()
43{
44 static Assert INSTANCE;
45
46 return INSTANCE;
47}
48
49Assert&
50Assert::notEmpty(const std::string& obj, const std::string& name)
51{
52 if (obj == "") {
53 throw IllegalArgumentException(ARGUMENT + name + IS_EMPTY);
54 }
55
56 return *this;
57}
58
59Assert&
60Assert::notEmpty(const std::vector<uint8_t>& obj, const std::string& name)
61{
62 if (obj.size() == 0) {
63 throw IllegalArgumentException(ARGUMENT + name + IS_EMPTY);
64 }
65
66 return *this;
67}
68
69Assert&
70Assert::isTrue(const bool condition, const std::string& name)
71{
72 if (!condition) {
73 throw IllegalArgumentException(CONDITION + name + IS_FALSE);
74 }
75
76 return *this;
77}
78
79Assert&
80Assert::greaterOrEqual(
81 const size_t number, const size_t minValue, const std::string& name)
82{
83 if (number < minValue) {
84 throw IllegalArgumentException(
85 ARGUMENT + name + HAS_A_VALUE + std::to_string(number) + LESS_THAN
86 + std::to_string(minValue) + CLOSING_BRACKET);
87 }
88
89 return *this;
90}
91
92Assert&
93Assert::isEqual(
94 const size_t number, const size_t value, const std::string& name)
95{
96 if (number != value) {
97 throw IllegalArgumentException(
98 ARGUMENT + name + HAS_A_VALUE + std::to_string(number)
99 + NOT_EQUAL_TO + std::to_string(value) + CLOSING_BRACKET);
100 }
101
102 return *this;
103}
104
105Assert&
106Assert::isInRange(
107 const size_t number,
108 const size_t minValue,
109 const size_t maxValue,
110 const std::string& name)
111{
112 if (number < minValue) {
113 throw IllegalArgumentException(
114 ARGUMENT + name + HAS_A_VALUE + std::to_string(number) + LESS_THAN
115 + std::to_string(minValue) + CLOSING_BRACKET);
116 } else if (number > maxValue) {
117 throw IllegalArgumentException(
118 ARGUMENT + name + HAS_A_VALUE + std::to_string(number)
119 + GREATER_THAN + std::to_string(maxValue) + CLOSING_BRACKET);
120 }
121
122 return *this;
123}
124
125Assert&
126Assert::isHexString(const std::string& hex, const std::string& name)
127{
128 if (!HexUtil::isValid(hex)) {
129 throw IllegalArgumentException(ARGUMENT + name + IS_NOT_HEX);
130 }
131
132 return *this;
133}
134
135} /* namespace util */
136} /* namespace core */
137} /* namespace keyple */