-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameVerifier.java
More file actions
177 lines (160 loc) · 5.94 KB
/
Copy pathNameVerifier.java
File metadata and controls
177 lines (160 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SkyPro
// Курсовая работа «Введение в профессию и синтаксис языка»
// Константин Терских, kostus.online.1974@yandex.ru, 2024
// https://google.github.io/styleguide/javaguide.html
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Валидатор имени.
*
* @author Константин Терских, kostus.online.1974@yandex.ru, 2024
* @version 1.1
*/
public class NameVerifier {
/**
* Русский и английский алфавиты, разрешённые символы.
*/
public static final String ALLOWED_CHARS_DEFAULT =
"- абвгдеёжзийклмнопрстуфхцчшщъыьэюя" +
"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ" +
"abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* Минимальная длина имени по умолчанию.
*/
public static final int CHARS_MIN_DEFAULT = 1;
/**
* Максимальная длина имени по умолчанию.
*/
public static final int CHARS_MAX_DEFAULT = 250;
/**
* Разрешённые символы. См. {@link #ALLOWED_CHARS_DEFAULT}.
*/
@NotNull
private final String allowedChars;
/**
* Минимальная длина имени.
*/
private final int minLength;
/**
* Максимальная длина имени.
*/
private final int maxLength;
/**
* Конструктор. Всё устанавливается в значения по умолчанию.
*/
public NameVerifier() {
this.allowedChars = ALLOWED_CHARS_DEFAULT;
this.minLength = CHARS_MIN_DEFAULT;
this.maxLength = CHARS_MAX_DEFAULT;
}
/**
* Конструктор.
*
* @param allowedChars разрешённые символы
* @param minLength минимальная длина имени
* @param maxLength максимальная длина имени
*/
@SuppressWarnings("unused")
public NameVerifier(@Nullable String allowedChars, int minLength, int maxLength) {
if (allowedChars == null) {
this.allowedChars = ALLOWED_CHARS_DEFAULT;
} else {
this.allowedChars = allowedChars.trim().toLowerCase();
}
this.minLength = minLength;
this.maxLength = maxLength;
}
/**
* Проверка имя на валидность по длине.
*
* @param str строка для проверки
* @return результат проверки
*/
public boolean isGood(@Nullable String str) {
if (str == null) {
return false;
}
if (str.isEmpty() || str.trim().isEmpty()) {
return false;
}
int count = str.length();
return count >= minLength && count <= maxLength;
}
/**
* Символ пробела.
*/
public static final char SPACE = ' ';
/**
* Символ табуляции.
*/
public static final char TAB = '\t';
/**
* Удаление ненужных символов из строки:<br>
* подряд идущие пробелы и табуляции, цифры и т.п. См. {@link #allowedChars}.
*
* @param source исходная строка
* @return нормализованная строка
*/
@Nullable
public String removeUnwantedChars(@Nullable String source) {
if (source == null) {
return null;
}
// сначала просто обрезаем строку слева и справа
source = source.trim();
int count = source.length();
if (count <= 1) {
return source;
}
// затем уже строим новую строку, проверяя каждый символ
var sb = new StringBuilder();
char lastAppended = 0;
for (int i = 0; i < count; i++) {
char currentChar = source.charAt(i);
// убираем идущие подряд пробелы и табуляции
if (currentChar != SPACE && currentChar != TAB) {
appendIfAllowed(sb, currentChar);
} else if (lastAppended != SPACE && lastAppended != TAB) {
appendIfAllowed(sb, currentChar);
}
lastAppended = currentChar;
}
return sb.toString();
}
/**
* Присоединение символа к строке, если символ разрешён.
*
* @param sb StringBuilder
* @param ch char
*/
public void appendIfAllowed(@NotNull StringBuilder sb, char ch) {
if (allowedChars.indexOf(ch) < 0) {
return;
}
sb.append(ch);
}
/**
* Нормализация строки. Только капитализация с затрагиванием всех символов строки.<br>
*
* @param str ненормализованная строка; пример: иВаНОв
* @return нормализованная строка; пример: Иванов;<br>
* для null возвращает null, <br>
* для пустой строки возвращает ту же строку,<br>
* для строки с одним символом возвращает строку с нормализованным символом (и -> И)
*/
@Nullable
public static String normalize(@Nullable String str) {
if (str == null) {
return null;
}
str = str.trim().toLowerCase();
if (str.isEmpty()) {
return str;
}
if (str.length() == 1) {
return str.toUpperCase();
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
}