-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 330ccc9
Showing
8 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.idea | ||
/out | ||
/*.iml | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# StringFormat | ||
Форматирование строк. | ||
|
||
### Передача одного параметра: | ||
```java | ||
String strTest1 = "Hello %NAME%!"; | ||
|
||
StringFormat.format(strTest1, "NAME", "World"); | ||
|
||
/* | ||
Результат выполнения: Hello World! | ||
*/ | ||
``` | ||
|
||
### Передача нескольких параметров: | ||
```java | ||
String strTest2 = "Hello %NAME%! It's %LANGUAGE%!"; | ||
|
||
Map<String, String> params = new HashMap<>(); | ||
params.put("NAME", "World"); | ||
params.put("LANGUAGE", "Java"); | ||
|
||
StringFormat.format(strTest2, params); | ||
|
||
/* | ||
Результат выполнения: Hello World! It's Java! | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[+] - нужно сделать | ||
[?] - возможно нужно сделать | ||
[>] - перенос в новую версию | ||
[x] - не делать | ||
|
||
# CORE | ||
* | ||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright © 2022 Алексей Каленчуков | ||
~ GitHub: https://github.com/kalenchukov | ||
~ E-mail: mailto:[email protected] | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>dev.kalenchukov</groupId> | ||
<artifactId>string-format</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<name>StringFormat</name> | ||
<description>Строковое форматирование</description> | ||
<url>https://github.com/kalenchukov/StringFormat</url> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>https://opensource.org/licenses/MIT</url> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>kalenchukov</id> | ||
<name>Алексей Каленчуков</name> | ||
<email>[email protected]</email> | ||
<url>https://github.com/kalenchukov</url> | ||
</developer> | ||
</developers> | ||
|
||
<properties> | ||
<maven.compiler.source>16</maven.compiler.source> | ||
<maven.compiler.target>16</maven.compiler.target> | ||
<encoding>UTF-8</encoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jetbrains</groupId> | ||
<artifactId>annotations</artifactId> | ||
<version>23.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
165 changes: 165 additions & 0 deletions
165
src/main/java/dev/kalenchukov/stringformat/StringFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
*/ | ||
|
||
package dev.kalenchukov.stringformat; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Класс содержит статические методы для форматирования строк. | ||
*/ | ||
public class StringFormat | ||
{ | ||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Integer param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Long param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Short param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Float param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Double param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Byte param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Character param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* @see #format(String, String, String). | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final Object param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return StringFormat.format(value, key, String.valueOf(param)); | ||
} | ||
|
||
/** | ||
* Форматирует строку с заданным ключом и параметром. | ||
* | ||
* @param value Строка в которой необходимо выполнить форматирование. | ||
* @param key Ключ который необходимо найти в строке. | ||
* @param param Параметр которым необходимо заменить значение ключа. | ||
* @return Строку в которой все значения ключа, обрамлённые символом "%", заменены значением переданного параметра. | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final String key, @NotNull final String param) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(key); | ||
Objects.requireNonNull(param); | ||
|
||
return value.replaceAll("%" + key + "%", param); | ||
} | ||
|
||
/** | ||
* Форматирует строку с несколькими ключами и параметрами. | ||
* | ||
* @param value Строка в которой необходимо выполнить форматирование. | ||
* @param params Коллекция с ключами и параметрами которые необходимо заменить в строке. | ||
* @return Строку в которой все значения ключей коллекции, обрамлённые символом "%", заменены соответствующими значениями коллекции. | ||
*/ | ||
@NotNull | ||
public static String format(@NotNull String value, @NotNull final Map<@NotNull String, @NotNull String> params) | ||
{ | ||
Objects.requireNonNull(value); | ||
Objects.requireNonNull(params); | ||
|
||
for (Map.Entry<@NotNull String, @NotNull String> param : params.entrySet()) | ||
{ | ||
Objects.requireNonNull(param.getKey()); | ||
Objects.requireNonNull(param.getValue()); | ||
|
||
value = StringFormat.format(value, param.getKey(), param.getValue()); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
*/ | ||
package dev.kalenchukov.stringformat; |
30 changes: 30 additions & 0 deletions
30
src/main/java/dev/kalenchukov/stringformat/tests/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
*/ | ||
|
||
package dev.kalenchukov.stringformat.tests; | ||
|
||
import dev.kalenchukov.stringformat.StringFormat; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class Test | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
String strTest1 = "Hello %NAME%!"; | ||
|
||
System.out.println(StringFormat.format(strTest1, "NAME", "World")); | ||
|
||
String strTest2 = "Hello %NAME%! It's %LANGUAGE%!"; | ||
|
||
Map<String, String> params = new HashMap<>(); | ||
params.put("NAME", "World"); | ||
params.put("LANGUAGE", "Java"); | ||
|
||
System.out.println(StringFormat.format(strTest2, params)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module dev.kalenchukov.stringformat | ||
{ | ||
requires org.jetbrains.annotations; | ||
|
||
exports dev.kalenchukov.stringformat; | ||
} |