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