|
2 | 2 |
|
3 | 3 | ## Introduction
|
4 | 4 |
|
5 |
| -This module is part of `JDevKit`, an open-source Java Development Kit that provides a set of convenient tools to streamline code development and enhance productivity. This module serves as the useful toolkit for the library, contains a collection of utility classes commonly used in all Java Application development. |
| 5 | +This module provides a set of utilities to streamline Java codes. |
6 | 6 |
|
7 |
| -## Prerequisites |
| 7 | +## Features |
8 | 8 |
|
9 |
| -This whole `JDevKit` is developed by **JDK 17**, which means you have to use JDK 17 for better experience. |
| 9 | +- AES encryption and decryption; |
| 10 | +- Base64 encode and decode; |
| 11 | +- Boolean calculation; |
| 12 | +- Reduce `if...else...` with **lambdas**; |
| 13 | +- Hash calculation for strings; |
| 14 | +- Convert Java beans to map and map to Java beans; |
| 15 | +- Simplified range generator. |
10 | 16 |
|
11 |
| -## Installation |
12 |
| - |
13 |
| -### If you are using `Maven` |
14 |
| - |
15 |
| -It is quite simple to install this module by `Maven`. The only thing you need to do is find your `pom.xml` file in the project, then find the `<dependencies>` node in the `<project>` node, and add the following codes to `<dependencies>` node: |
16 |
| - |
17 |
| -```xml |
18 |
| -<dependency> |
19 |
| - <groupId>cn.org.codecrafters</groupId> |
20 |
| - <artifactId>devkit-utils</artifactId> |
21 |
| - <version>${devkit-utils.version}</version> |
22 |
| -</dependency> |
23 |
| -``` |
24 |
| - |
25 |
| -And run `mvn dependency:get` in your project root folder(i.e., if your `pom.xml` is located at `/path/to/your/project/pom.xml`, then your current work folder should be `/path/to/your/project`), then `Maven` will automatically download the `jar` archive from `Maven Central Repository`. This could be **MUCH EASIER** if you are using IDE(i.e., IntelliJ IDEA), the only thing you need to do is click the refresh button of `Maven`. |
26 |
| - |
27 |
| -If you are restricted using the Internet, and have to make `Maven` offline, you could follow the following steps. |
28 |
| - |
29 |
| -1. Download the `jar` file from any place you can get and transfer the `jar` files to your work computer. |
30 |
| -2. Move the `jar` files to your local `Maven` Repository as the path of `/path/to/maven_local_repo/cn/org/codecrafters/devkit-utils/`. |
31 |
| - |
32 |
| -### If you are using `Gradle` |
33 |
| - |
34 |
| -Add this module to your project with `Gradle` is much easier than doing so with `Maven`. |
35 |
| - |
36 |
| -Find `build.gradle` in the needed project, and add the following code to the `dependencies` closure in the build script: |
37 |
| - |
38 |
| -```groovy |
39 |
| -implementation 'cn.org.codecrafters:devkit-utils:${devkit-utils.version}' |
40 |
| -``` |
41 |
| - |
42 |
| -### If you are not using `Maven` or `Gradle` |
43 |
| - |
44 |
| -1. Download the `jar` file from the Internet. |
45 |
| -2. Create a folder in your project and name it as a name you like(i.e., for me, I prefer `vendor`). |
46 |
| -3. Put the `jar` file to the folder you just created in Step 2. |
47 |
| -4. Add this folder to your project `classpath`. |
48 |
| - |
49 |
| -### Base64 Encode and Decode |
50 |
| - |
51 |
| -If you are trying to encode a string to Base64 string or decode a Base64 string to normal string, then you can try this: |
52 |
| - |
53 |
| -```java |
54 |
| -import utils.com.onixbyte.devkit.Base64Util; |
55 |
| - |
56 |
| -// To reduce sample codes, let me use the simplified main method that is upcoming in Java 21 |
57 |
| -void main(String... args) { |
58 |
| - var aString = "The string you need to encode."; |
59 |
| - // Encode it from original text. |
60 |
| - var encodedString = Base64Util.encode(aString); |
61 |
| - // Decode the encoded text. |
62 |
| - var decodedString = Base64Util.decode(encodedString); |
63 |
| -} |
64 |
| -``` |
65 |
| - |
66 |
| -## Reduce `if...else...` |
67 |
| - |
68 |
| -I believe those `if...else...` blocks make you headache, and Java imported lambda since Java 8, why not try to replace those `if...else` with lambda expressions? |
69 |
| - |
70 |
| -```java |
71 |
| -import utils.com.onixbyte.devkit.BranchUtil; |
72 |
| - |
73 |
| -void main(String... args) { |
74 |
| - var a = 1; |
75 |
| - var b = 2; |
76 |
| - |
77 |
| - if (a < b) { |
78 |
| - // do something... |
79 |
| - } else { |
80 |
| - // do something different... |
81 |
| - } |
82 |
| - // The codes above is really annoying, have a try of the lambda version. |
83 |
| - |
84 |
| - BranchUtil.or(a < b) // If multiple logical expressions are combined using the OR operator. |
85 |
| - .handle(() -> { |
86 |
| - // do something if a < b. |
87 |
| - }, () -> { |
88 |
| - // do something if a > b. |
89 |
| - }); |
90 |
| - |
91 |
| - BranchUtil.and(a < b) // If multiple logical expressions are combined using the AND operator |
92 |
| - .handle(() -> { |
93 |
| - // do something if a < b. |
94 |
| - }, () -> { |
95 |
| - // do something if a > b. |
96 |
| - }); |
97 |
| -} |
98 |
| -``` |
99 |
| - |
100 |
| -What if you have to get some data from this branch? Don't worry, those **lambda supports Supplier**. Just add a return in those lambda is fine. |
101 |
| - |
102 |
| -## CHAINED High-precision Calculation |
103 |
| - |
104 |
| -If you have faced high-precision mathematical calculation in Java, you might know it is very troubled to do it in **ANY** programming languages. I'm certain you remember that `0.1 + 0.2 != 0.3` right? |
105 |
| - |
106 |
| -In Java, we usually do high-precision mathematical calculation with `BigDecimal` which is quite tricky when using it. |
107 |
| - |
108 |
| -```java |
109 |
| -import utils.com.onixbyte.devkit.ChainedCalcUtil; |
110 |
| - |
111 |
| -void main(String... args) { |
112 |
| - // If you are trying to calculate the expression of 1 * 2 / 2 - 3 + 4 |
113 |
| - ChainedCalcUtil.startWith(1).multiply(2).divide(2).subtract(3).add(4).getInteger(); // you can also get a double by getDouble([int scale]), get a BigDecimal by getDecimal([int scale]) or get a long by getLong(). |
114 |
| -} |
115 |
| -``` |
116 |
| - |
117 |
| -If you are facing a divide calculation which has an infinite decimal expansion, then DON'T use `divide(dividend)`, use `divideWithScale(dividend, scale, [beforeOperateScale])`. |
118 |
| - |
119 |
| -## Hash a `String` |
120 |
| - |
121 |
| -As is well known, storing plain text passwords in a database is very insecure. Therefore, you need to encrypt the passwords stored in the database, or at least make it difficult for hackers to see the real password at a glance. As a result, hash calculation is often used in database password obfuscation due to its ease of use. |
122 |
| - |
123 |
| -This `HashUtil` supports these following hash or message digest algorithms: |
124 |
| - |
125 |
| -- MD2 |
126 |
| -- MD5 |
127 |
| -- SHA-1 |
128 |
| -- SHA-224 |
129 |
| -- SHA-256 |
130 |
| -- SHA-384 |
131 |
| -- SHA-512 |
132 |
| - |
133 |
| -If you want to run a hash calculation to a string, you can use the following codes: |
134 |
| - |
135 |
| -```java |
136 |
| -import utils.com.onixbyte.devkit.HashUtil; |
137 |
| - |
138 |
| -void main(String... args) { |
139 |
| - var plaintext = "This is a plain text"; |
140 |
| - var hashedText = HashUtil.md2(plaintext); // if you want to use other algorithm, just change the method name such as md5, sha1, sha224 and so on. |
141 |
| -} |
142 |
| -``` |
143 |
| - |
144 |
| -Besides, if you want to use other character set to do the hash calculation, you can add the specified charset after the text to be calculated. |
145 |
| - |
146 |
| -```java |
147 |
| -HashUtil.$algorithm$(String textToCalculate, Charset charset); |
148 |
| -``` |
149 |
| - |
150 |
| -## Convert a Map to Any Object and Vice Versa |
151 |
| - |
152 |
| -A Map is a data structure that allows you to store key-value pairs. The keys can be of any type and the values can be of any type. In this case, the key is the user's name and the value is the user's profile. |
153 |
| - |
154 |
| -Imagine you are developing a website where users can register an account and store their profile. A profile can contain information like their name, age, address, and email address. You store the user's profile in a Map. When the user registers an account, you need to store their profile in a database. A database is a system for storing data. A database can store any type of data, including Maps. |
155 |
| - |
156 |
| -In order to store the Map in a database, you need to convert the Map to an Object. An Object is a generic data type that can store any type of data. |
157 |
| - |
158 |
| -```java |
159 |
| -import utils.com.onixbyte.devkit.MapUtil; |
160 |
| - |
161 |
| -class Data { |
162 |
| - private String name; |
163 |
| - private Long id; |
164 |
| - private Integer age; |
165 |
| - |
166 |
| - // Setters and getters here. |
167 |
| - |
168 |
| - // No-args constructor and all-args constructor here. |
169 |
| -} |
170 |
| - |
171 |
| -void main(String... args) { |
172 |
| - // Create a map. |
173 |
| - var dataMap = new HashMap<String, Object>(); |
174 |
| - dataMap.put("name", "Zihlu Wang"); |
175 |
| - dataMap.put("id", 1L); |
176 |
| - dataMap.put("age", 18); |
177 |
| - |
178 |
| - // Convert this map to an instance of class Data |
179 |
| - var data = MapUtil.mapToObject(dataMap, Data.class); // Then you got an instance of Data("Zihlu Wang", 1L, 18); |
180 |
| - |
181 |
| - // Then you can convert this object to a map. |
182 |
| - var anotherDataMap = MapUtil.objectToMap(data) |
183 |
| -} |
184 |
| -``` |
185 |
| - |
186 |
| -## Contact |
187 |
| - |
188 |
| -If you have any suggestions, ideas, don't hesitate contacting us via [GitHub Issues](https://github.com/CodeCraftersCN/jdevkit/issues/new) or [Discord Community](https://discord.gg/NQK9tjcBB8). |
189 |
| - |
190 |
| -If you face any bugs while using our library and you are able to fix any bugs in our library, we would be happy to accept pull requests from you on [GitHub](https://github.com/CodeCraftersCN/jdevkit/compare). |
0 commit comments