Dans la continuité de Dernière fois, Lombok a introduit la fonction de plaque de chaudière.
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"
$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
Tout le monde aime IntelliJ IDEA
Toutes les sources sont ↓ https://github.com/sky0621/try-java-lombok
@Builder
Générer la source pour les modèles de générateur (https://sourcemaking.com/design_patterns/builder)
@lombok.Builder
public class Builder {
private int id;
private String name;
private int price;
private String publishDate;
}
public class Builder {
private int id;
private String name;
private int price;
private String publishDate;
@java.lang.SuppressWarnings("all")
Builder(final int id, final String name, final int price, final String publishDate) {
this.id = id;
this.name = name;
this.price = price;
this.publishDate = publishDate;
}
@java.lang.SuppressWarnings("all")
public static class BuilderBuilder {
@java.lang.SuppressWarnings("all")
private int id;
@java.lang.SuppressWarnings("all")
private String name;
@java.lang.SuppressWarnings("all")
private int price;
@java.lang.SuppressWarnings("all")
private String publishDate;
@java.lang.SuppressWarnings("all")
BuilderBuilder() {
}
@java.lang.SuppressWarnings("all")
public BuilderBuilder id(final int id) {
this.id = id;
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderBuilder name(final String name) {
this.name = name;
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderBuilder price(final int price) {
this.price = price;
return this;
}
@java.lang.SuppressWarnings("all")
public BuilderBuilder publishDate(final String publishDate) {
this.publishDate = publishDate;
return this;
}
@java.lang.SuppressWarnings("all")
public Builder build() {
return new Builder(id, name, price, publishDate);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "Builder.BuilderBuilder(id=" + this.id + ", name=" + this.name + ", price=" + this.price + ", publishDate=" + this.publishDate + ")";
}
}
@java.lang.SuppressWarnings("all")
public static BuilderBuilder builder() {
return new BuilderBuilder();
}
}
public class Main {
public static void main(String... args){
System.out.println(
Builder.builder()
.id(100)
.name("Nom")
.price(937)
.publishDate("2018-11-09")
.build().toString());
}
}
@Value
Générer une classe immuable
@lombok.Value
public class Value {
private int id;
private String name;
private int price;
private String publishDate;
}
public final class Value {
private final int id;
private final String name;
private final int price;
private final String publishDate;
@java.lang.SuppressWarnings("all")
public Value(final int id, final String name, final int price, final String publishDate) {
this.id = id;
this.name = name;
this.price = price;
this.publishDate = publishDate;
}
@java.lang.SuppressWarnings("all")
public int getId() {
return this.id;
}
@java.lang.SuppressWarnings("all")
public String getName() {
return this.name;
}
@java.lang.SuppressWarnings("all")
public int getPrice() {
return this.price;
}
@java.lang.SuppressWarnings("all")
public String getPublishDate() {
return this.publishDate;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof Value)) return false;
final Value other = (Value) o;
if (this.getId() != other.getId()) return false;
final java.lang.Object this$name = this.getName();
final java.lang.Object other$name = other.getName();
if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
if (this.getPrice() != other.getPrice()) return false;
final java.lang.Object this$publishDate = this.getPublishDate();
final java.lang.Object other$publishDate = other.getPublishDate();
if (this$publishDate == null ? other$publishDate != null : !this$publishDate.equals(other$publishDate)) return false;
return true;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getId();
final java.lang.Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
result = result * PRIME + this.getPrice();
final java.lang.Object $publishDate = this.getPublishDate();
result = result * PRIME + ($publishDate == null ? 43 : $publishDate.hashCode());
return result;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "Value(id=" + this.getId() + ", name=" + this.getName() + ", price=" + this.getPrice() + ", publishDate=" + this.getPublishDate() + ")";
}
}
@Cleanup
Il ajoute une logique de fermeture de post-traitement telle que l'ouverture de fichier. Cependant, si vous utilisez Java 7 ou une version ultérieure, vous devez utiliser try-with-resources, donc si vous l'utilisez, peut-être jusqu'à Java 6.
public class Cleanup {
public static void main(String[] args) throws IOException {
@lombok.Cleanup InputStream in = new FileInputStream(args[0]);
@lombok.Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}
import java.io.*;
public class Cleanup {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(args[0]);
try {
OutputStream out = new FileOutputStream(args[1]);
try {
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
} finally {
if (java.util.Collections.singletonList(out).get(0) != null) {
out.close();
}
}
} finally {
if (java.util.Collections.singletonList(in).get(0) != null) {
in.close();
}
}
}
}
@Log
(java.util.logger.Logger)
@lombok.extern.java.Log
public class LogExample {
public static void main(String... args) {
log.severe("Something's wrong here");
}
}
public class LogExample {
@java.lang.SuppressWarnings("all")
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
public static void main(String... args) {
log.severe("Something\'s wrong here");
}
}
@NonNull
Insérez une vérification nulle pour l'argument de méthode. Cependant, par défaut, «NullPointerException» est levé. Il est possible de changer en ʻIllegalArgumentException`.
public class NonNull {
private int id;
@lombok.NonNull
private String name;
public void setNonNull(@lombok.NonNull Book book) {
this.name = book.getName();
}
}
public class NonNull {
private int id;
@lombok.NonNull
private String name;
public void setNonNull(@lombok.NonNull Book book) {
if (book == null) {
throw new java.lang.NullPointerException("book is marked @NonNull but is null");
}
this.name = book.getName();
}
}
Pour le moment, j'ai repris et introduit certaines des fonctions de base. Au fait, la famille principale est ↓ https://projectlombok.org/features/all
D'autres fonctionnalités expérimentales sont présentées ci-dessous. https://projectlombok.org/features/experimental/all
Recommended Posts