Add val and var examples, unit tests and include flagUsage in lombok.config (#11419)

This commit is contained in:
Carlos Cavero 2021-11-21 05:19:03 +01:00 committed by GitHub
parent 13348bab48
commit 156dc8cb2c
5 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package com.baeldung.lombok.valvar;
import lombok.val;
import lombok.var;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class ValExample {
public Class name() {
val name = "name";
System.out.println("Name: " + name);
return name.getClass();
}
public Class age() {
val age = Integer.valueOf(30);
System.out.println("Age: " + age);
return age.getClass();
}
public Class listOf() {
val agenda = new ArrayList<String>();
agenda.add("Day 1");
System.out.println("Agenda: " + agenda);
return agenda.getClass();
}
public Class mapOf() {
val books = new HashMap<Integer, String>();
books.put(1, "Book 1");
books.put(2, "Book 2");
System.out.println("Books:");
for (val entry : books.entrySet()) {
System.out.printf("- %d. %s\n", entry.getKey(), entry.getValue());
}
return books.getClass();
}
public Class compoundTypes(boolean isArray) {
val compound = isArray ? new ArrayList<String>() : new HashSet<String>();
return compound.getClass();
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.lombok.valvar;
import lombok.var;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class VarExample {
public String name() {
var name = "name";
name = "newName";
System.out.println("Name: " + name);
return name;
}
public Integer age() {
var age = Integer.valueOf(30);
age = 35;
System.out.println("Age: " + age);
return age;
}
public ArrayList<String> listOf() {
var agenda = new ArrayList<String>();
agenda.add("Day 1");
agenda = new ArrayList<String>(Arrays.asList("Day 2"));
System.out.println("Agenda: " + agenda);
return agenda;
}
public Map<Integer, String> mapOf() {
var books = new HashMap<Integer, String>();
books.put(1, "Book 1");
books.put(2, "Book 2");
books = new HashMap<Integer, String>();
books.put(3, "Book 3");
books.put(4, "Book 4");
System.out.println("Books:");
for (var entry : books.entrySet()) {
System.out.printf("- %d. %s\n", entry.getKey(), entry.getValue());
}
return books;
}
}

View File

@ -0,0 +1,2 @@
lombok.var.flagUsage = warning
lombok.val.flagUsage = warning

View File

@ -0,0 +1,24 @@
package com.baeldung.lombok.valvar;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import static org.assertj.core.api.Assertions.assertThat;
class ValExampleUnitTest {
@Test
void whenUsingValWithString_thenTheAssignedClassIsCorrect() {
ValExample val = new ValExample();
assertThat(val.name()).isEqualTo(String.class);
assertThat(val.age()).isEqualTo(Integer.class);
assertThat(val.listOf()).isEqualTo(ArrayList.class);
assertThat(val.mapOf()).isEqualTo(HashMap.class);
assertThat(val.compoundTypes(true)).isEqualTo(ArrayList.class);
assertThat(val.compoundTypes(false)).isEqualTo(HashSet.class);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.lombok.valvar;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import static org.assertj.core.api.Assertions.assertThat;
class VarExampleUnitTest {
@Test
void whenUsingVarWithString_thenTheAssignedClassIsCorrect() {
VarExample varExample = new VarExample();
assertThat(varExample.name()).isEqualTo("newName");
assertThat(varExample.age()).isEqualTo(35);
assertThat("Day 2").isIn(varExample.listOf());
assertThat(varExample.mapOf()).containsValue("Book 3");
}
}