/** * Created by macro on 2020/12/16. */ public class ValExample {
public static void example() { //val代替ArrayList<String>和String类型 val example = new ArrayList<String>(); example.add("Hello World!"); val foo = example.get(0); System.out.println(foo.toLowerCase()); }
public static void example2() { //val代替Map.Entry<Integer,String>类型 val map = new HashMap<Integer, String>(); map.put(0, "zero"); map.put(5, "five"); for (val entry : map.entrySet()) { System.out.printf("%d: %s\n", entry.getKey(), entry.getValue()); } }
public static void main(String[] args) { example(); example2(); } }
/** * 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); } }
编译后会在构造器中添加非空判断,具体代码如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class NonNullExample { private String name;
public NonNullExample(@NonNull String name) { if (name == null) { throw new NullPointerException("name is marked non-null but is null"); } else { this.name = name; } }
public static void main(String[] args) { new NonNullExample("test"); new NonNullExample((String)null); } }
/** * 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); } }
/** * 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()); } }
/** * 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)); } }
public int hashCode() { int PRIME = true; int result = 1; Object $id = this.getId(); int result = result * 59 + ($id == null ? 43 : $id.hashCode()); return result; } }
/** * 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); } }
public class ConstructorExample { @NonNull private Long id; private String name; private Integer age;
public ConstructorExample() { }
private ConstructorExample(@NonNull final Long id) { if (id == null) { throw new NullPointerException("id is marked non-null but is null"); } else { this.id = id; } }
public static ConstructorExample of(@NonNull final Long id) { return new ConstructorExample(id); }
public ConstructorExample(@NonNull final Long id, final String name, final Integer age) { if (id == null) { throw new NullPointerException("id is marked non-null but is null"); } else { this.id = id; this.name = name; this.age = age; } } }
/** * 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)); } }
public class DataExample { @NonNull private Long id; private String name; private Integer age;
public DataExample(@NonNull final Long id) { if (id == null) { throw new NullPointerException("id is marked non-null but is null"); } else { this.id = id; } }
@NonNull public Long getId() { return this.id; }
public String getName() { return this.name; }
public Integer getAge() { return this.age; }
public void setId(@NonNull final Long id) { if (id == null) { throw new NullPointerException("id is marked non-null but is null"); } else { this.id = id; } }
public void setName(final String name) { this.name = name; }
public void setAge(final Integer age) { this.age = age; }
public boolean equals(final Object o) { if (o == this) { return true; } else if (!(o instanceof DataExample)) { return false; } else { DataExample other = (DataExample)o; if (!other.canEqual(this)) { return false; } else { Object this$id = this.getId(); Object other$id = other.getId(); if (this$id == null) { if (other$id != null) { return false; } } else if (!this$id.equals(other$id)) { return false; }
public int hashCode() { int PRIME = true; int result = 1; Object $id = this.getId(); int result = result * 59 + ($id == null ? 43 : $id.hashCode()); return result; }
/** * Created by macro on 2020/12/17. */ @Data public class SynchronizedExample { @NonNull private Integer count;
@Synchronized @SneakyThrows public void reduceCount(Integer id) { if (count > 0) { Thread.sleep(500); count--; System.out.println(String.format("thread-%d count:%d", id, count)); } }
public static void main(String[] args) { //添加@Synchronized三个线程可以同步调用reduceCount方法 SynchronizedExample example = new SynchronizedExample(20); new ReduceThread(1, example).start(); new ReduceThread(2, example).start(); new ReduceThread(3, example).start(); }
/** * 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"); } }
编译后Lombok会生成如下代码。
1 2 3 4 5 6 7 8 9 10 11 12
public class LogExample { private static final Logger log = Logger.getLogger(LogExample.class.getName());
/** * 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"); } }
编译后Lombok会生成如下代码。
1 2 3 4 5 6 7 8 9 10 11 12
public class LogSlf4jExample { private static final Logger log = LoggerFactory.getLogger(LogSlf4jExample.class);