forked from hellokaton/write-readable-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4.java
More file actions
50 lines (41 loc) · 1.17 KB
/
Copy pathExample4.java
File metadata and controls
50 lines (41 loc) · 1.17 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
package chapter_11;
import java.time.LocalDate;
/**
* 项目专有的功能
*
* @author biezhi
* @date 2018/8/13
*/
public class Example4 {
class Request {
String param(String key) {
return "";
}
}
static class Business {
String name;
String url;
LocalDate created;
public void save() {
}
}
public void part1(Request request) {
Business business = new Business();
business.name = request.param("name");
business.url = "/biz/" + makePrettyURL(business);
business.created = LocalDate.now();
business.save();
}
private String makePrettyURL(Business business) {
String urlPathName = business.name.toLowerCase();
urlPathName = urlPathName.replaceAll("[\\.,\\']", "");
urlPathName = urlPathName.replaceAll("[^a-z0-9]+", "-");
if (urlPathName.charAt(0) == '-') {
urlPathName = urlPathName.substring(1);
}
if (urlPathName.charAt(urlPathName.length() - 1) == '-') {
urlPathName = urlPathName.substring(0, urlPathName.length() - 1);
}
return urlPathName;
}
}