Skip to content

Commit

Permalink
add mall-tiny-lombok
Browse files Browse the repository at this point in the history
  • Loading branch information
macrozheng committed Dec 22, 2020
1 parent dd88fc2 commit b62f04a
Show file tree
Hide file tree
Showing 21 changed files with 523 additions and 0 deletions.
29 changes: 29 additions & 0 deletions mall-tiny-lombok/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

### VS Code ###
.vscode/
60 changes: 60 additions & 0 deletions mall-tiny-lombok/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>com.macro.mall</groupId>
<artifactId>mall-tiny-lombok</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mall-tiny-lombok</name>
<description>Demo project for Spring Boot</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--SpringBoot通用依赖模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.macro.mall.tiny;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MallTinyApplication {

public static void main(String[] args) {
SpringApplication.run(MallTinyApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.macro.mall.tiny.example;

import lombok.Builder;
import lombok.ToString;

/**
* Created by macro on 2020/12/17.
*/
@Builder
@ToString
public class BuilderExample {
private Long id;
private String name;
private Integer age;

public static void main(String[] args) {
BuilderExample example = BuilderExample.builder()
.id(1L)
.name("test")
.age(20)
.build();
System.out.println(example);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.macro.mall.tiny.example;

import lombok.Cleanup;

import java.io.*;

/**
* Created by macro on 2020/12/16.
*/
public class CleanupExample {
public static void main(String[] args) throws IOException {
String inStr = "Hello World!";
//使用输入输出流自动关闭,无需编写try catch和调用close()方法
@Cleanup ByteArrayInputStream in = new ByteArrayInputStream(inStr.getBytes("UTF-8"));
@Cleanup ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
String outStr = out.toString("UTF-8");
System.out.println(outStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.macro.mall.tiny.example;

import lombok.*;

/**
* Created by macro on 2020/12/17.
*/
@NoArgsConstructor
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor
public class ConstructorExample {
@NonNull
private Long id;
private String name;
private Integer age;

public static void main(String[] args) {
//无参构造器
ConstructorExample example1 = new ConstructorExample();
//全部参数构造器
ConstructorExample example2 = new ConstructorExample(1L,"test",20);
//@NonNull注解的必须参数构造器
ConstructorExample example3 = ConstructorExample.of(1L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.macro.mall.tiny.example;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

/**
* Created by macro on 2020/12/17.
*/
@Data
public class DataExample {
@NonNull
private Long id;
@EqualsAndHashCode.Exclude
private String name;
@EqualsAndHashCode.Exclude
private Integer age;

public static void main(String[] args) {
//@RequiredArgsConstructor已生效
DataExample example1 = new DataExample(1L);
//@Getter @Setter已生效
example1.setName("test");
example1.setAge(20);
//@ToString已生效
System.out.println(example1);
DataExample example2 = new DataExample(1L);
//@EqualsAndHashCode已生效
System.out.println(example1.equals(example2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.macro.mall.tiny.example;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
* Created by macro on 2020/12/17.
*/
@Getter
@Setter
@EqualsAndHashCode
public class EqualsAndHashCodeExample {
private Long id;
@EqualsAndHashCode.Exclude
private String name;
@EqualsAndHashCode.Exclude
private Integer age;

public static void main(String[] args) {
EqualsAndHashCodeExample example1 = new EqualsAndHashCodeExample();
example1.setId(1L);
example1.setName("test");
example1.setAge(20);
EqualsAndHashCodeExample example2 = new EqualsAndHashCodeExample();
example2.setId(1L);
//equals方法只对比id,返回true
System.out.println(example1.equals(example2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.macro.mall.tiny.example;

import lombok.Getter;

/**
* Created by macro on 2020/12/17.
*/
public class GetterLazyExample {
@Getter(lazy = true)
private final double[] cached = expensive();

private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}

public static void main(String[] args) {
//使用Double Check Lock 样板代码对属性进行懒加载
GetterLazyExample example = new GetterLazyExample();
System.out.println(example.getCached().length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.macro.mall.tiny.example;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

/**
* Created by macro on 2020/12/17.
*/
public class GetterSetterExample {
@Getter
@Setter
private String name;
@Getter
@Setter(AccessLevel.PROTECTED)
private Integer age;

public static void main(String[] args) {
GetterSetterExample example = new GetterSetterExample();
example.setName("test");
example.setAge(20);
System.out.printf("name:%s age:%d",example.getName(),example.getAge());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.macro.mall.tiny.example;

import lombok.extern.java.Log;

/**
* Created by macro on 2020/12/17.
*/
@Log
public class LogExample {
public static void main(String[] args) {
log.info("level info");
log.warning("level warning");
log.severe("level severe");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.macro.mall.tiny.example;

import lombok.extern.slf4j.Slf4j;

/**
* Created by macro on 2020/12/17.
*/
@Slf4j
public class LogSlf4jExample {
public static void main(String[] args) {
log.info("level:{}","info");
log.warn("level:{}","warn");
log.error("level:{}", "error");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.macro.mall.tiny.example;

import lombok.NonNull;

/**
* Created by macro on 2020/12/16.
*/
public class NonNullExample {
private String name;
public NonNullExample(@NonNull String name){
this.name = name;
}

public static void main(String[] args) {
new NonNullExample("test");
//会抛出NullPointerException
new NonNullExample(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.macro.mall.tiny.example;

import lombok.SneakyThrows;

import java.io.UnsupportedEncodingException;

/**
* Created by macro on 2020/12/17.
*/
public class SneakyThrowsExample {

//自动抛出异常,无需处理
@SneakyThrows(UnsupportedEncodingException.class)
public static byte[] str2byte(String str){
return str.getBytes("UTF-8");
}

public static void main(String[] args) {
String str = "Hello World!";
System.out.println(str2byte(str).length);
}
}
Loading

0 comments on commit b62f04a

Please sign in to comment.