SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
03/09/2012

Milou fait un régime
Guava - Lombok

SPONSORS

Cours

@thierryleriche

Thierry Leriche-Dessirier





Consultant JEE freelance
Professeur de Génie Logiciel à l’ESIEA
Rédacteur pour Programmez
Rédacteur pour Developpez.com

icauda.com

Forums

FAQ

News

Interviews

Critiques

Articles / tutoriels

Agendas

Magazine

 Milou est trop lourd (intro)

Agenda

13 OOO OOO pages vues par mois
5 500 000 visites par mois
2 500 000 visites uniques par mois
5 000 messages forum par jour

 Lombok (et Lombok-pg) en action
 Tour d’horizon de Guava

1
DOG

03/09/2012

Milou est trop lourd (et doit faire un régime)

I am Milou

public class Dog {
private
private
private
private
private
private
private
private
private
private

Integer
String
String
SexeEnum
Date
String
Boolean
Double
Double
List<String>

id ;
name ;
fullName ;
sex ;
birthday ;
race ;
lof ;
weight ;
size ;
colors ;

5

...

























Constructeurs
Getters / setters
toString ()
equals ()
hashCode ()
compareTo ()











id
name
fullName
sex
birthday
race
lof
weight
size
Colors

id
name
fullName
sex
birthday
race
lof
weight
size
Colors

...
@Override
public int compareTo(Dog other) {
int result = 0;
result = name.compareTo(other.name);
if (result != 0) {
return result;
}
...
}

Code très limite… NPE ?...

clic clic
démo








public class Dog
implements Comparable<Dog> {

Constructeurs
Getters / setters
toString ()
equals ()
hashCode ()
compareTo ()

10 attributs











Constructeurs
Getters / setters
toString ()
equals ()
hashCode ()
compareTo ()

 210 lignes

id
name
fullName
sex
birthday
race
lof
weight
size
Colors

2
Java

Va chercher Guava 

03/09/2012

10 attributs
210 lignes de code






toString ()
equals ()
hashCode ()
compareTo ()

Commons  FAQ 1-2

Java 5

5

public String toString() {
return "Dog [id=" + id
+ ", name=" + name
+ ", fullName=" + fullName
+ ", sex=" + sex
+ ", birthday=" + birthday
+ ", race=" + race
+ ", lof=" + lof
+ ", weight=" + weight
+ ", size=" + size
+ ", colors=" + colors + "]";
}

Base

Simple, efficace et bien pourri 
Objects

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Dog.class.getSimpleName())
.append("[id=").append(id)
.append(", name=").append(name)
.append(", fullName=").append(fullName)
...
.append(", colors=").append(colors);

5

return sb.toString();

Guava

Java 5

 toString ()

public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("name", name)
.add("fullName", fullName)
...
.add("colors", colors)
.toString();
}

G
Builder

}
Mieux mais sans plus 

 toString ()

 toString ()

3
03/09/2012

Java 5

5

Dog other = (Dog) obj;
if (birthday == null) {
if (other.birthday != null) return false;
} else if (!birthday.equals(other.birthday)) return false;
if (fullName == null) {
if (other.fullName != null) return false;
...
} else if (!race.equals(other.race)) return false;
if (sex != other.sex) return false;

birthday, fullname, name, race et sex

Guava

birthday, fullname, name, race et sex
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;

public boolean equals(Object obj) {
if (!(obj instanceof Dog)) return false;
Dog other = (Dog) obj;

G

return Objects.equal(birthday, other.birthday)
&& Objects.equal(fullname, other.fullname)
&& Objects.equal(name, other.name)
&& Objects.equal(race, other.race)
&& sex == other.sex;
}

return true;
}
Carrément illisible 

 equals ()

 equals ()

result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
result = prime * result
+ ((fullName == null) ? 0 : fullName.hashCode());
result = prime * result
+ ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((race == null) ? 0 : race.hashCode());
result = prime * result
+ ((sex == null) ? 0 : sex.hashCode());

5

birthday, fullname, name, race et sex

Guava

Java 5

birthday, fullname, name, race et sex
public int hashCode() {
final int prime = 31;
int result = 1;

public int hashCode() {
return Objects.hashCode(birthday, fullName,
name, race, sex);

}

G

return result;
}

 hashCode ()

 hasCode()

Java 5

name, fullname, birthday, weight, size, race et sex
public int compareTo(Dog other) {
int result = 0;
result = name.compareTo(other.name);
if (result != 0) {
return result;
}
result = fullname.compareTo(other.fullname);
if (result != 0) {
return result;
}

5
name.compareTo(other.name)

Vs

other.name.compareTo(name)

...
}

 compareTo ()

 compareTo ()

4
03/09/2012

name, fullname, birthday, weight, size, race et sex
public int compareTo(Dog other) {
return ComparisonChain.start()
.compare(name, other.name)
.compare(fullName, other.fullName)
.compare(birthday, other.birthday)
.compare(weight, other.weight)
.compare(size, other.size)
.compare(race, other.race)
.compare(sex, other.sex)
.result();
}

Lombok

Guava

G

 compareTo ()

Régime

en action

Régime

 @NoArgsConstructor /
@RequiredArgsConstructor /
@AllArgsConstructor  constructeurs
 @Getter / @Setter
 @ToString
 @EqualsAndHashCode
 @Data  @Getter + @Setter + @ToString +
@EqualsAndHashCode + @RequiredArgsConstructor

Régime

Annotations de base

clic clic
démo

 @NoArgsConstructor /
@RequiredArgsConstructor /
@AllArgsConstructor
 @Getter / @Setter
 @ToString
 @EqualsAndHashCode
 @Data

Java
Lombok

10 attributs
210 lignes de code
12 lignes + 2-3 @ + compareTo()

5
@RequiredArgsConstructor(staticName = "of")
public class Dog {
private Integer id;
@NonNull private String name; 
private String fullName;
@NonNull private SexeEnum sex; 
private Date birthday;
...

 Dog dog = Dog.of("Milou", MALE);

L
name, sex

Factory

Factory

03/09/2012

L

@RequiredArgsConstructor(staticName = "of")
public class Dog {
private Integer id;
@NonNull private String name; 
private String fullName;
@NonNull private SexeEnum sex; 
private Date birthday;
...

 Dog dog = Dog.of("Milou", MALE);

name, sex

private Dog(@NonNull final String name,
@NonNull final SexeEnum sex) {
if (name == null) throw new NullPointerException("name");
if (sex == null) throw new NullPointerException("sex");
this.name = name;
this.sex = sex;
}

Pratique

public static Dog of(@NonNull final String name,
@NonNull final SexeEnum sex) {
return new Dog(name, sex);
}

Trucs pratiques







@Cleanup
@Synchronized
@SneakyThrows
@Log
@Delegate

 val

public void lireJava6(String from) {
InputStream in = null;
try {
in = new FileInputStream(from);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1)
break;
...
}
} catch (Exception e) {
...
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
...
}
}
...

5

Ressources

Ressources

Se simplifier la vie

public void lire(String from) throws IOException {
@Cleanup
InputStream in = new FileInputStream(from);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1)
break;
...
}

L

}

6
03/09/2012

java -jar lombok.jar delombok src -d src-delomboked

Délombok

XML

<properties>
<lombok.sourceDirectory>
${project.basedir}/src/main/java
</lombok.sourceDirectory>
...

<build>
<groupId>org.projectlombok</groupId>
<artifactId>maven-lombok-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
</execution>
</executions>
...

Délombok

Revenir sans la lib

mvn lombok:delombok

Délombok

5

@Data
public class Dog {
private Integer id;
public class Dog {
private String name;
private Integer id;
private String fullName;
private String name;
private SexeEnum sex;
private String fullName;
private Date birthday;
...
private String race;
private List<String> colors;
private Boolean lof;
private Double weight;
public DogLombok() {
private Double size;
}
private List<String> colors;
@java.lang.Override
}
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (o == null) return false;
mvn
lombok:delombok
if (o.getClass() != this.getClass()) return false;
final Dog other = (Dog)o;
if (this.getId() == null ? other.getId() != null : !this.getId().equals(other.getId())) return false;
...

5

Lombok-pg

Annotations supplémentaires


















 @Builder
@Action
 @LazyGetter
@Function
 @FluentSetter
@EnumId
 @Predicate
@Rethrow / @Rethrows
@Singleton
@AutoGenMethodStub
@BoundPropertySupport / @BoundSetter
@DoPrivileged
@ExtensionMethod
@ListenerSupport
@WriteLock / @ReadLock
@Await / @Signal / @AwaitBeforeAndSignalAfter
@Sanitize.Normalize / .With
@SwingInvokeLater / @SwingInvokeAndWait
@Validate.NotEmpty / .NotNull / .With
@VisibleForTesting

Fluent

Annotations Pg

@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = result * PRIME + (this.getId() == null ? 0 : this.getId().hashCode());
result = result * PRIME + (this.getName() == null ? 0 : this.getName().hashCode());

@FluentSetter
@Getter
public class Dog {
private Integer id ;
private String name ;
private String fullName ;

L

...
 DogLombok dog = new DogLombok();
dog.name("Milou").sex(MALE);

println( dog.getName());
println( dog.getSex());

//  Milou
//  MALE

7
L

@Builder
@Getter
public class Dog {
private Integer id ;
private String name ;
private String fullName ;

extension

Builder

03/09/2012

L

@ExtensionMethod({ Dog.class, MyDogExtensionClass.class })
public class DogWithExtension {
public void foo() {
Dog milou = new Dog("Milou", 12.5, ...);
boolean isTooFat = milou.isTooFat();
}

}

...

class MyDogExtensionClass {

 Dog dog = Dog.dog().name("Milou").sex(MALE).build();

println( dog.getName());
println( dog.getSex());

public static boolean isTooFat(final Dog dog) {
double imc = dog.getWeight() / pow(dog.getSize(), 2);
return 25 < imc;
}

}

//  Milou
//  MALE

Avec paramètres  FAQ 9

Pro / Cons

 http://blog.developpez.com/todaystip/p11165/dev/java/extensionmethod-de-lombok-pg/

Lombok or not Lombok ?






Byte code modifié 
Version en 0.x
Documentation des prog ?
Magique ?






Compacité
Lecture simplifiée
Code normée
Délombok (pour essayer)

Avantages et inconvénients

Guava

Factory Methods

Les choses que j’aime

tour d’horizon

8
03/09/2012

< new Vs static factories >

Set<String> colors = new TreeSet<String>();

5

5

List<Integer> primeNumbers = new ArrayList<Integer>();
Set<String> colors = new TreeSet<String>();

Map<String, Integer> ages = new HashMap<String, Integer>();

dur dur ?

Map<String, Integer> ages = new HashMap<String, Integer>();

Java 5

Java 5

< new Vs static factories >
List<Integer> primeNumbers = new ArrayList<Integer>();

5

Map<String, List<String>> trucs =
new HashMap<String, List<String>>();

Map<? extends Person, Map<String, List<String>>> trucs =
new TreeMap<? extends Person, Map<String, List<String>>>();
Map<? extends Wrapper<String, Sex, Person>, Map<String,
List<Set<Adress<String, Integer, Country>>>>> trucs = ...

5
7

Java 7

Map<String, Integer> ages = new HashMap<String, Integer>();
List<Integer> primeNumbers = new ArrayList<>();
Set<String> colors = new TreeSet<>();
Map<String, Integer> ages = new HashMap<>();

List<Integer> primeNumbers = new ArrayList<Integer>();
Set<String> colors = new TreeSet<String>();

5
7
G

Map<String, Integer> ages = new HashMap<String, Integer>();

Java 7

Set<String> colors = new TreeSet<String>();

Java 5

< new Vs static factories >

Java 5

< new Vs static factories >
List<Integer> primeNumbers = new ArrayList<Integer>();

List<Integer> primeNumbers = new ArrayList<>();
Set<String> colors = new TreeSet<>();
Map<String, Integer> ages = new HashMap<>();

Qui utilise Java 7 en prod ?

Guava

Qui utilise Java 7 en prod ?

List<Integer> primeNumbers = newArrayList();
Set<String> colors = newTreeSet();
Map<String, Integer> ages = newHashMap();

Dès maintenant !

Set<String> colors = new TreeSet<String>();

5
7
G

Java 7

Map<String, Integer> ages = new HashMap<String, Integer>();
List<Integer> primeNumbers = new ArrayList<>();
Set<String> colors = new TreeSet<>();
Map<String, Integer> ages = new HashMap<>();

Guava

Qui utilise Java 7 en prod ?
List<Integer> primeNumbers = newArrayList();
Set<String> colors = newTreeSet();
Map<String, Integer> ages = newHashMap();

Lombok

Dès maintenant !
var primeNumbers = new ArrayList<Integer>();
-> primeNumbers.size();
...

L

List of dogs

< new Vs static factories >

Java 5

< new Vs static factories >
List<Integer> primeNumbers = new ArrayList<Integer>();

List<Dog> dogs = newArrayList(
new Dog("Milou", 12.5, MALE, ...),
new Dog("Rintintin", 45.0, MALE, ...),

G

new Dog("Volt", 10.3, MALE, ...),
new Dog("Lassie", 45.0, FEMALE, ...),
new Dog("Pluto", 22.0, MALE, ...),
new Dog("Medor", 35.6, MALE, ...));

[Milou,
Rintintin,
Volt,
Lassie,
Pluto,
Medor]

9
03/09/2012

immutable

Quand ?

Immutables

Pour que ça ne change pas

Java 5

< unmodifiables Vs immutables >
Set<Integer> temp =
new LinkedHashSet<Integer>(Arrays.asList(1, 2, 3, 5, 7));
Set<Integer> primes = Collections.unmodifiableSet(temp);

immutable

Quand ?
mutable

On se demande souvent si une liste doit être immutable mais
c’est prendre le problème dans le mauvais sens. La plupart du
temps, ce qu’il faut vraiment se demander, c’est si la liste a
besoin d’être mutable.

Set<Integer> primes = Collections.unmodifiableSet(temp);

Of

< unmodifiables Vs immutables >

Java 5

< unmodifiables Vs immutables >
Set<Integer> temp =
new LinkedHashSet<Integer>(Arrays.asList(1, 2, 3, 5, 7));

ImmutableSet.of(E
ImmutableSet.of(E
ImmutableSet.of(E
ImmutableSet.of(E
ImmutableSet.of(E
ImmutableSet.of(E

e1)
e1,
e1,
e1,
e1,
e1,

E
E
E
E
E

e2)
e2,
e2,
e2,
e2,

E
E
E
E

G

e3)
e3, E e4)
e3, E e4, E e5)
e3, E e4, E e5, E e6, E...)

Set<Integer> primes = ImmutableSet.of(1, 2, 3, 5, 7);

G

Of

Guava

Ne prend pas de null

ImmutableSet.of()
Vide

Map et List  FAQ 4
 http://blog.developpez.com/guava/p10589/collection/title-212/

10
03/09/2012

< Copie de défense >

5

private String name;
...
private List<String> colors;

Guava

Java 5

< Copie de défense >
public class Dog {

public class Dog {
private String name;
...
private ImmutableList<String> colors;

G

public Dog(String name, List<String> colors) {
this.name = name;
...
this.colors = ImmutableList.copyOf(colors);
}

public Dog(String name, List<String> colors) {
this.name = name;
...
this.colors = Collections.unmodifiableList(
new ArrayList<String>(colors));
}

public ImmutableList<String> getColors() {
return colors;
}

public List<String> getColors() {
return colors;
}

Message clair

Java 5

< Multi Map >
Map<String, List<String>> dogFavoriteColors =
new HashMap<String, List<String>>();

5
5

List<String> milouColors = dogFavoriteColors.get("Milou");
if(milouColors == null) {
milouColors = new ArrayList<String>();
dogFavoriteColors.put("Milou",milouColors);
}
milouColors.add("Vert");

Collections en plus

Multimap, Bipap, Multiset

5
5

Guava

List<String> milouColors = dogFavoriteColors.get("Milou");
if(milouColors == null) {
milouColors = new ArrayList<String>();
dogFavoriteColors.put("Milou",milouColors);
}
milouColors.add("Vert");

Multimap<String, String> dogFavoriteColors =
HashMultimap.create();
dogFvoriteColors2.put("Milou", "Jaune");
dogFavoriteColors2.put("Milou", "Rouge");

G

< Bi Map >

Guava

Java 5

< Multi Map >
Map<String, List<String>> dogFavoriteColors =
new HashMap<String, List<String>>();

BiMap<String, Dog> tatouages = HashBiMap.create();
tatouages.put("ABC123", new Dog("Milou") );
tatouages.put("MBD324", new Dog("Volt") );
tatouages.put("JFT672", new Dog("Lassie") );

G

println( tatouages );

{ABC123=Milou,
MBD324=Volt,
JFT672=Lassie}

ABC123=Milou
MDB324=Volt
JFT672=Lassie

println( tatouages.inverse() );
Une map bijective

{Milou=ABC123,
Volt=MBD324,
Lassie=JFT672}

Il est possible de changer la valeur associée à une clé mais pas
d'avoir deux clés avec la même valeur (IllegalArgumentException).

11
03/09/2012

Guava

< Multi Set >
Multiset<Integer> ages = HashMultiset.create();
ages.add(2);
ages.add(3);
ages.add(7);
ages.add(11);
ages.add(3);
ages.add(5);
println( ages );

G

[2, 3 x 2, 5, 7, 11]

Base
println( ages.count(3) )

2
Préconditions, joiner, splitter, optional

DOG

< Preconditions >






5

public class Dog {
private Integer
private String
private String
...

id ;
name ;
fullName ;

public Dog(String name, String fullName, ...) {
if(name == null) {
throw new NPE("Le nom bla bla");
}
if(fullName == null) {
throw new NPE("bla bla");
}
...

toString ()
equals ()
hashCode ()
compareTo ()

this.name = name;
this.fullName = fullName;
...

DOG

< Preconditions >
import static com.google.common.base.Preconditions.*;

public class Dog {
private Integer
private String
private String
...

id ;
name ;
fullName ;

G

Joiner

}

String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", "Milou"));

"Lassie, Volt, Milou"

G

public Dog(String name, String fullName, ...) {
this.name = checkNotNull(name, "bla bla");
this.fullName = checkNotNull(fullName, "bla bla");
...
}

checkNotNull()  NPE
checkArgument()  IAE
checkState()
 ISE

Java classique
List<String> dogNames = newArrayList("Lassie", "Volt", "Milou");
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String name : dogNames) {
if(name != null || name.trim().isEmpty()) {
continue;
}
if (!first) {
sb.append(", ");
}
sb.append(name);
first = false;
}
String names = sb.toString();

12
String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", "Milou"));

"Lassie, Volt, Milou"

G

Joiner

Joiner

03/09/2012

String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", "Milou"));

String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", null, "Milou"));

"Lassie, Volt, Milou"

G

String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", null, "Milou"));

 NPE 

 NPE 

G

String names = Joiner.on(", ")
.skipNulls()
.join(newArrayList("Lassie", "Volt", null, "Milou"));

"Lassie, Volt, Milou"

G

Splitter

String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", "Milou"));

[ "Lassie",
"Volt",
"Milou" ]

Splitter

Joiner

"Lassie, Volt, Milou"

[ "Lassie",
"Volt",
"Milou" ]

Iterable<String> dogNames =
Splitter.on(",")
.split("Lassie, Volt, Milou");

String names = Joiner.on(", ")
.join(newArrayList("Lassie", "Volt", null, "Milou"));

 NPE 

G
G

String names = Joiner.on(", ")
.skipNulls()
.join(newArrayList("Lassie", "Volt", null, "Milou"));

"Lassie, Volt, Milou"

String names = Joiner.on(", ")
.useForNull("Anonymous"))
.join(newArrayList("Lassie", "Volt", null, "Milou"));

"Lassie, Volt, Anonymous, Milou"

Splitter

 http://blog.developpez.com/guava/p11054/base/joiner-pour-assembler-des-items/

Iterable<String> dogNames =
Splitter.on(",")
.split("Lassie, Volt, Milou");

[ "Lassie",
"Volt",
"Milou" ]

Iterable<String> dogNames =
Splitter.on(",")
.split("Lassie, Volt, Milou");

Iterable<String> dogNames =
Splitter.on(",")
[ "Lassie",
.split("Lassie, Volt, ,Milou"); "Volt",

Iterable<String> dogNames =
Splitter.on(",")
[ "Lassie",
.split("Lassie, Volt, ,Milou"); "Volt",

" ",
"Milou" ]

" ",
"Milou" ]
Iterable<String> dogNames =
Splitter.on(",")
[ "Lassie",
.trimResults()
"Volt",
.split("Lassie, Volt, ,Milou"); "",

"Milou" ]

13
Iterable<String> dogNames =
Splitter.on(",")
.split("Lassie, Volt, Milou");

Optional

Splitter

03/09/2012

[ "Lassie",
"Volt",
"Milou" ]

Iterable<String> dogNames =
Splitter.on(",")
[ "Lassie",
.split("Lassie, Volt, ,Milou"); "Volt",

Wrapper Optional <T>
Dog dog = new Dog("Milou", ...);
Optional<Dog> opt = Optional.of(dog);
assertTrue( opt.isPresent() );
assertEquals( "Milou", opt.get().getName() );

" ",
"Milou" ]
Iterable<String> dogNames =
Splitter.on(",")
[ "Lassie",
.trimResults()
"Volt",
.split("Lassie, Volt, ,Milou"); "",

"Milou" ]
Iterable<String> dogNames =
Splitter.on(",")
.trimResults()
[ "Lassie",
.omitEmptyStrings()
.split("Lassie, Volt, ,Milou"); "Volt",

G

"Milou" ]

Wrapper Optional <T>
Dog dog = new Dog("Milou", ...);
Optional<Dog> opt = Optional.of(dog);
assertTrue( opt.isPresent() );
assertEquals( "Milou", opt.get().getName() );

Optional<Dog> opt = Optional.absent();
assertFalse( opt.isPresent() );
opt.get();  ISE

Optional

Optional

 http://blog.developpez.com/guava/p11045/annotation/splitter-pour-separer-des-items/

Wrapper Optional <T>
Dog dog = new Dog("Milou", ...);
Optional<Dog> opt = Optional.of(dog);
assertTrue( opt.isPresent() );
assertEquals( "Milou", opt.get().getName() );

Optional<Dog> opt = Optional.absent();
assertFalse( opt.isPresent() );
opt.get();  ISE
Dog dog = null;
Optional<Dog> opt = Optional.of(dog);  NPE

Optional

Optional<Dog> opt = Optional.fromNullable(dog);

Wrapper Optional <T>
Dog dog = new Dog("Milou", ...);
Optional<Dog> opt = Optional.of(dog);
assertTrue( opt.isPresent() );
assertEquals( "Milou", opt.get().getName() );

Optional<Dog> opt = Optional.absent();
assertFalse( opt.isPresent() );
opt.get();  ISE
Dog dog = null;
Optional<Dog> opt = Optional.of(dog);  NPE

Functional Programming

Optional<Dog> opt = Optional.fromNullable(dog);
Dog dog = null;
Optional<Dog> opt = Optional.fromNullable(dog);

todo

Dog dog2 = opt.or( new Dog("noname", ...) );
assertEquals( "noname", dog2.getName() );
 http://blog.developpez.com/guava/p11163/base/le-wrapper-optional-de-guava/

14
Super Chien

03/09/2012

Super chien

public class SuperChien implements SuperHero {
private
private
private
private

String
double
Set<String>
Set<String>

surnom ;
poids ;
couleursCostume ;
pouvoirs ;

5

...
Les héros peuvent avoir plusieurs costumes donc je
n’utilise pas un ImmutableSet. Idem pour les pouvoirs
dont la liste augmente avec l’expérience.

Dog

< Transformation >

G

chien.setSurnom("Super " + dog.getName());
chien.setPoids(dog.getWeight());
chien.setCouleursCostume(newHashSet(dog.getColors()))
chien.setPouvoirs(newHashSet("Code en Java", "Vole"))
...

Transformation

Transformation

< Transformation >
List<SuperChien> superChiens =
Lists.transform(dogs,
new Function<Dog, SuperChien>() {
@Override
public SuperChien apply(Dog dog) {
SuperChien chien = new SuperChien();

List<SuperChien> superChiens =
Lists.transform(dogs, new Function<Dog, SuperChien>() {
@Override
public SuperChien apply(Dog dog) {
SuperChien chien = new SuperChien();
...
return chien;
}
});

return chien;
}
});

[Super Milou,
Super Rintintin,
Super Volt,
Super Lassie,
Super Pluto,
Super Medor]

G

 Vue (lazy)
 size / isEmpty dispo 
 Pas pour traitements répétés   FAQ 3

List<SuperChien> chiens = newArrayList(Lists.transform(...
ImmutableList<SuperChien> chiens =
ImmutableList.copyOf(Lists.transform(...

< Filtre >

Filtre

G

Iterable<Dog> maleDogs =
Iterables.filter(dogs, malePredicate);

[Milou,
Rintintin,
Volt,
Pluto,
Medor]

G

Filtre

< Filtre >
Predicate<Dog> malePredicate =
new Predicate<Dog>() {
public boolean apply(Dog dog) {
return dog.getSex() == MALE;
}
}

Predicate<Dog> malePredicate =
new Predicate<Dog>() {
public boolean apply(Dog dog) {
return dog.getSex() == MALE;
}
}
Iterable<Dog> maleDogs =
Iterables.filter(dogs, malePredicate);

[Milou,
Rintintin,
Volt,
Pluto,
Medor]

Dog firstMaleDog =
Iterables.find(dogs, malePredicate);

Milou

G
G
G

15
03/09/2012

Iterable<Dog> maleDogs =
Iterables.filter(dogs, malePredicate);

[Milou,
Rintintin,
Volt,
Pluto,
Medor]

Dog firstMaleDog =
Iterables.find(dogs, malePredicate);

Milou

G
G
G

Seconde liste

Filtre

< Filtre >
Predicate<Dog> malePredicate =
new Predicate<Dog>() {
public boolean apply(Dog dog) {
return dog.getSex() == MALE;
}
}

new Dog("Pluto", 22.0, MALE, ...),
new Dog("Lulu", 35.6, MALE, ...));

[Rintintin,
Pluto,
Lulu]

Dog firstMaleDog =
Iterables.find(femaleDogs, malePredicate, DEFAULT_DOG );

G

List<Dog> dogs2 = newArrayList(
new Dog("Rintintin", 45.0, MALE, ...),

[Milou,
Rintintin,
Volt,
Lassie,
Pluto,
Medor]

Liste 2 : dogs2

Default dog

Function<FullDog, String> nameFunction =
new Function<FullDog, String>() {
public String apply(FullDog dog) {
return dog.getName();
}};
Iterable<FullDog> maleDogs =
Iterables.filter(dogs, malePredicate);
Iterable<String> maleNames =
Iterables.transform(maleDogs, nameFunction);

G
G
G

< Fluent or not fluent ? >

Iterable<FullDog> maleDogs =
Iterables.filter(dogs, malePredicate);
Iterable<String> maleNames =
Iterables.transform(maleDogs, nameFunction);

Fluent

Pas fluent

< Fluent or not fluent ? >
Predicate<Dog> malePredicate =
new Predicate<Dog>() {
public boolean apply(Dog dog) {
return dog.getSex() == MALE;
}};

Pas fluent

Liste 1 : dogs

List<String> maleNames2 =
FluentIterable.from(dogs)
.filter(malePredicate)
.transform(nameFunction)
.toImmutableList();

G
G
Guava 12.0

[Milou,
Rintintin,
Volt,
Pluto,
Medor]

Web Service

 http://blog.developpez.com/guava/p11092/annotation/fluentiterable-surmon-chien-guava/

< Memoization >

public class DogService {
@Inject
private PetShopWebService service;
public Integer getNumberDogsSoldYesterday() {
return service.checkSales( "dog" );
}

[Milou,
Rintintin,
Volt,
Pluto,
Medor]

5

Cache

todo

16
5

Cache manuel
Guava

@Inject
private PetShopWebService service;
public Integer getNumberDogsSoldYesterday() {
return service.checkSales( "dog" );
}

public Integer getNumberDogsSoldYesterday() {
if (nbOfDogsSold == null) {
nbOfDogsSold = service.checkSales( "dog" );
}
return nbOfDogsSold;
}

< Memoization >

G

public class DogService {
@Inject
private PetShopWebService service;
private Supplier<Integer> nbOfDogsSoldSupplier =
Suppliers.memoize(
new Supplier<Integer>() {
public Integer get() {
return service.checkSales( "dog" );
}
});

5

private Integer nbOfDogsSold;

public Integer getNumberDogsSoldYesterday() {
return nbOfDogsSoldSupplier.get();
}

Double check null…

< Memoization >

public class DogService {
@Inject
private PetShopWebService service;
private Supplier<Integer> nbOfDogsSoldSupplier =
Suppliers.memoizeWithExpiration(
new Supplier<Integer>() {
public Integer get() {
return service.checkSales( "dog" );
}
}, 1, TimeUnit.DAYS );

G

public Integer getNumberDogsSoldYesterday() {
return nbOfDogsSoldSupplier.get();
}

Guava

Guava

< Memoization >

public class DogService {

< Cache >

public class DogService {
@Inject
private PetShopWebService service;

Web Service

Web Service

03/09/2012

< Cache >

public class DogService {
@Inject
private PetShopWebService service;
private Map<String, Dog> dogMap = Maps.newHashMap();

5

public Integer getDog(String name) {
Dog dog = dogMap.get(name);
if(dog == null) {
dog = service.getAnimal( "dog", name ); // type-name
dogMap.put( name, dog );
}
return dog;
}
Quid du timeout ? Max ?

G

private LoadingCache<String, Dog> dogCache =
CacheBuilder.newBuilder()
.maximumSize(2000)
.expireAfterWrite(30, TimeUnit.MINUTES)
.build(new CacheLoader<String, Dog>() {
public Dog load(String key) {
return service.getAnimal( "dog", key );
}
});

Hash

public Integer getDog(String name) {
return dogCache.get( name ); // + try-catch
}
Fantôme

17
Hash

G

HashFunction hf = Hashing.md5();
HashCode hc = hf.newHasher()
.putInt(123)
.putString("Milou")
.hash();
byte[] bytes = hc.asBytes();

md5
Murmur3 128 bits
Murmur3 32 bits
Sha1
Sha256
Sha512
goodFastHash

< Is in list ? >

5

int NB_OF_DOGS = 100000;
List<Dog> dogs = newArrayList();
Random rand = new Random();
for (int i = 0; i < NB_OF_DOGS; i++) {
Dog dog = new Dog();
dog.setName("abc" + rand.nextInt(999));
...
dogs.add(dog);
}

contains









Préparation

03/09/2012

5

final Dog milou = new Dog();
milou.setName("Milou");
...
boolean isInList = dogs.contains(milou);

false (14 ms)

dogs.add(milou);
boolean isInList = dogs.contains(milou);

Bloom filter

< Is in list ? >
Funnel<Dog> dogFunnel = new Funnel<Dog>() {
public void funnel(Dogdog, PrimitiveSink sink) {
sink.putString(dog.getName())
.putString(dog.getFullName())
.putString(dog.getRace());
}};

true (14 ms)

G

BloomFilter<Dog> bloom =
BloomFilter.create(dogFunnel, NB_OF_DOGS, 0.01);
for (int i = 0; i < NB_OF_DOGS; i++) {
...
bloom.put(dog);

Guava or not Guava ?

boolean isInList = bloom.mightContain(milou);

false (0 ms)
bloom.put(milou);
boolean isInList = bloom.mightContain(milou);
Guava 13

Avantages et inconvénients

true (0 ms)

 Ne pas en abuser…

 Utile
 Bonnes pratiques

LIENS

Pro / Cons

 http://blog.developpez.com/guava/p11149/collection/bloom-filter-de-guava-13/

@thierryleriche

18
03/09/2012

ICAUDA

Guava

 http://icauda.com
 http://icauda.com/articles.html
 http://icauda.com/cours.html

 http://code.google.com/p/guava-libraries

Lombok

(articles)
(slides)

Blog Guava

 http://projectlombok.org

 http://blog.developpez.com/guava

Lombok-pg

« Simplifier le code de vos beans Java à l'aide
de Commons Lang, Guava et Lombok »

 https://github.com/peichhorn/lombok-pg

 http://thierry-leriche-dessirier.developpez.com/tutoriels/
java/simplifier-code-guava-lombok
(article)

MERCI

FAQ / Bonus

1. Guava Vs Commons
2. Régime façon Apache
3. Fonctionnal prog Vs boucle for
4. Créer des Maps
5. Charsets
6. Converter Spring
7. Orderings
8. Licences
9. Extension avec valeurs
10. Chrono
11. CharMatcher
12. Impl
13. And, or, in…
14. Partition, limit

Régime : la méthode Commons ?
toString

Guava Vs Commons ?

public String toString() {
return new ToStringBuilder(this)
.append("id", id)
.append("name", name)
.append("fullName", fullName)
...
.append("colors", colors)
.toString();

http://tinyurl.com/guava-vs-apache

L

}

1

2

19
03/09/2012

Régime : la méthode Commons ?

L

Dog other = (Dog) obj;

hashCode

equals

Régime : la méthode Commons ?
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Dog)) return false;
return new EqualsBuilder()
.append(birthday, other.birthday)
.append(fullname, other.fullname)
.append(name, other.name)
.append(race, other.race)
.append(sex, other.sex)
.isEquals();

2

return new HashCodeBuilder(17, 37)
.append(birthday)
.append(fullname)
.append(name)
.append(race)
.append(sex)
.toHashCode();
}

}

2

Fonctionnal prog Vs boucle for : quand ?

L

public int compareTo(Dog other) {
return new CompareToBuilder()
.append(name, other.name)
.append(fullname, other.fullname)
.append(birthday, other.birthday)
...
.append(sex, other.sex)
.toComparison();
}

Transformation

Régime : la méthode Commons ?
compareTo

L

public int hashCode() {

List<SuperChien> superChiens = newArrayList(
Lists.transform(dogs, new Function<Dog, SuperChien>() {
@Override
public SuperChien apply(Dog dog) {
SuperChien chien = new SuperChien();
...
return chien;
}
}));

3

2

G

Vs

List<SuperChien> superChiens = newArrayList();
for(Dog dog : dogs) {
SuperChien chien = new SuperChien();
...
superChiens.add(chien);
}

G

http://code.google.com/p/guava-libraries/wiki/FunctionalExplained

Builder

G

4

public static final ImmutableMap<String, Integer>
AGES = new ImmutableMap.Builder<String, Integer>()
.put("Milou", 32)
.put("Volt", 7)
.put("Pluto", 37)
.put("Lassie", 17)
.put("Medor", 5)
.put("Croquette", 8)
.put("Loulou", 2)
...
.build();

G

Java 5

Charsets
String name = "Milou";
try {
byte[] bytes = name.getBytes("UTF-8");

5

} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
Ca n’arrive jamais cette exception 

Guava

of

Créer des Maps
public static final ImmutableMap<String, Integer>
AGES = ImmutableMap.of("Milou", 32,
"Volt", 7,
"Pluto", 37,
"Lassie", 17);

5

String name = "Milou";
byte[] bytes = name.getBytes(Charsets.UTF_8);

G

20
03/09/2012

Converter Spring

Orderings

G

import org.springframework...Converter;

@Component("dogToChienConverter")
public class DogToChienConverter
implements Converter<List<Dog>,
List<SuperChien>> {

public List<SuperChien> convert(List<Dog> dogs) {
List<SuperChien> chiens = newArrayList(transform(dogs,
new Function<Dog, SuperChien>() {
public SuperChien apply(Dog dog) {
...
return chien;
}
}));
return chiens;
}

6

7

Lazy or not lazy ?

Extension avec valeurs

Licences
 Lombok :
 Lombok-pg :
 Guava :

@ExtensionMethod({ Object.class, MyOtherClass.class })
public class DogWithExtension {

MIT License
MIT License

public void foo() {
String s1 = "toto";
s1.print();
String s2 = null;
s2.print();

L

//  toto
//  null

Apache License 2.0
}

 Commons :

}

Apache License 2.0

class MyOtherClass {
public static <T> void print(final T value) {
System.out.println(value);
}
}

8

9

On peut mettre autre chose qu’un "Object", par exemple un
"Arrays" , un "Dog" , etc.

< Chrono >

Extension avec valeurs
public void foo() {
String s1 = "toto";
s1.print();

//  toto

String s2 = null;
s2.print();
s2.print("vide");

//  null
//  vide

foo(); // traitement long (ou pas)
public static <T extends String> void print(final T value, final T otherValue) {
long end = new Date().getTime();
if (value == null || value.isEmpty()) {
long
System.out.println(otherValue); duration = end - start; // 11 ms
}
}

}
}

9

L

Guava Java 5

@ExtensionMethod({ Object.class, MyOtherClass.class })
public class DogWithExtension {

long start = new Date().getTime();

Stopwatch sw = new Stopwatch();
sw.start();
foo();

class MyOtherClass {
public static <T> void print(final T value) {
System.out.println(value);
}
public static void print(String value, String other) {
if (value == null || value.isEmpty()) {
System.out.println(other);
} else {
System.out.println(value);
}

sw.stop();
long duration = sw.elapsedMillis(); // 11 ms

10

5
G

long nano = sw.elapsedTime(NANOSECONDS); // 11179739 ns
long micro = sw.elapsedTime(MICROSECONDS); // 11179 us
long millis = sw.elapsedTime(MILLISECONDS); // 11 ms

 http://blog.developpez.com/guava/p11160/base/le-stop-watch-de-guava/

21
03/09/2012

CharMatcher

Impl
public class Dog implements Comparable<Dog> {
private Integer id ;
private String name ;
private String fullName ;

5

...
@Override
public int compareTo(Dog dog) {
return ...;
}

11

implements List ? aie aie aie

12

And, or, in…

@AutoGenMethodStub
public class Dog implements Comparable<Dog> {
private Integer id ;
private String name ;
private String fullName ;

L

...

And, or, in...

Impl

boolean isRintintinInBoth =
and( in(dogs), in(dogs2) )
.apply(new Dog("Rintintin"));

Partition

Partition, limit
List<List<FullDog>> partition =
Lists.partition(dogs, 4);

[Milou,
Rintintin,
Volt,
Lassie]

[Pluto,
Medor]

[Milou,
Rintintin,
Volt,
Lassie,
Pluto,
Medor]

Liste 2 : dogs2

13

Liste 1 : dogs

12

Limit

true

G

import static com.google.common.base.Predicates.or;

boolean isTintinInOne =
or( in(dogs), in(dogs2) )
.apply(new Dog("Tintin");

false

[Rintintin,
Pluto,
Lulu]

G
Slides en préparation

List<FullDog> first4Dogs =
newArrayList(Iterables.limit(dogs, 4));

14

G

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.in;

[Milou,
Rintintin,
Volt,
Lassie]

G

22
03/09/2012

Ressources

< I/O >

I/O

todo

public void lireJava6(String from) {
InputStream in = null;
try {
in = new FileInputStream(from);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1)
break;
...
}
} catch (Exception e) {
...
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
...
}
}
...

< I/O >

7

Guava

Java 7

< I/O >
public void lireJava7(String from) {
try(InputStream in = new FileInputStream(from)) {
...
while (true) {
...
}
}
}

5

Todo

G

Todo

Concurrent / future

todo

23

Mais conteúdo relacionado

Mais procurados

Javascript, Do you speak it!
Javascript, Do you speak it!Javascript, Do you speak it!
Javascript, Do you speak it!
Victor Porof
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
Pierre de Lacaze
 

Mais procurados (20)

Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
 
Monads asking the right question
Monads  asking the right questionMonads  asking the right question
Monads asking the right question
 
Prolog
PrologProlog
Prolog
 
Zhifu Ge - How To Be Weird In Ruby - With Notes
Zhifu Ge - How To Be Weird In Ruby - With NotesZhifu Ge - How To Be Weird In Ruby - With Notes
Zhifu Ge - How To Be Weird In Ruby - With Notes
 
Kotlin for Android Developers - 3
Kotlin for Android Developers - 3Kotlin for Android Developers - 3
Kotlin for Android Developers - 3
 
Real world gobbledygook
Real world gobbledygookReal world gobbledygook
Real world gobbledygook
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of Flattery
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of Flattery
 
Introduction to Prolog
Introduction to PrologIntroduction to Prolog
Introduction to Prolog
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java Developers
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
 
Prolog 01
Prolog 01Prolog 01
Prolog 01
 
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018 Meetup di GDG Italia - Leonardo Pirro -  Codemotion Rome 2018
Meetup di GDG Italia - Leonardo Pirro - Codemotion Rome 2018
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
 
Javascript, Do you speak it!
Javascript, Do you speak it!Javascript, Do you speak it!
Javascript, Do you speak it!
 
Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prolog
 
Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full story
 
F bound-types
F bound-typesF bound-types
F bound-types
 
Basic inheritance in JavaScript
Basic inheritance in JavaScriptBasic inheritance in JavaScript
Basic inheritance in JavaScript
 

Destaque

Enquête chauffeurs vtc 02-14 - bilan réseaux
Enquête chauffeurs vtc   02-14 - bilan réseauxEnquête chauffeurs vtc   02-14 - bilan réseaux
Enquête chauffeurs vtc 02-14 - bilan réseaux
Etudiant EGE
 
Store manager intuiko
Store manager intuikoStore manager intuiko
Store manager intuiko
INTUIKOFRANCE
 
América Latina: Flujos de información y poder
América Latina: Flujos de información y poder América Latina: Flujos de información y poder
América Latina: Flujos de información y poder
Ximena Ponce León
 
Enquête chauffeurs vtc 02-14 - bilan réseaux
Enquête chauffeurs vtc   02-14 - bilan réseauxEnquête chauffeurs vtc   02-14 - bilan réseaux
Enquête chauffeurs vtc 02-14 - bilan réseaux
Etudiant EGE
 
Question 5
Question 5Question 5
Question 5
wilsonnx
 
Lettre d'information de ...n bui n°13 octobre 2014
Lettre d'information de ...n bui n°13 octobre 2014Lettre d'information de ...n bui n°13 octobre 2014
Lettre d'information de ...n bui n°13 octobre 2014
graves146
 
Cnc 2009 mp_physique_2
Cnc 2009 mp_physique_2Cnc 2009 mp_physique_2
Cnc 2009 mp_physique_2
elmanssouri
 
François Martin, Pixels Award 2014 - Pixels Festival S01E01
François Martin, Pixels Award 2014 - Pixels Festival S01E01François Martin, Pixels Award 2014 - Pixels Festival S01E01
François Martin, Pixels Award 2014 - Pixels Festival S01E01
Pixelsfestival
 

Destaque (20)

FR Webinar Sept 2014 - Comment optimiser votre site
FR Webinar Sept 2014 - Comment optimiser votre site FR Webinar Sept 2014 - Comment optimiser votre site
FR Webinar Sept 2014 - Comment optimiser votre site
 
Enquête chauffeurs vtc 02-14 - bilan réseaux
Enquête chauffeurs vtc   02-14 - bilan réseauxEnquête chauffeurs vtc   02-14 - bilan réseaux
Enquête chauffeurs vtc 02-14 - bilan réseaux
 
Store manager intuiko
Store manager intuikoStore manager intuiko
Store manager intuiko
 
2231
22312231
2231
 
Un Tsung vaut mieux que 2 "croisons les doigts"
Un Tsung vaut mieux que 2 "croisons les doigts"Un Tsung vaut mieux que 2 "croisons les doigts"
Un Tsung vaut mieux que 2 "croisons les doigts"
 
Colloque rapport
Colloque rapportColloque rapport
Colloque rapport
 
América Latina: Flujos de información y poder
América Latina: Flujos de información y poder América Latina: Flujos de información y poder
América Latina: Flujos de información y poder
 
Enquête chauffeurs vtc 02-14 - bilan réseaux
Enquête chauffeurs vtc   02-14 - bilan réseauxEnquête chauffeurs vtc   02-14 - bilan réseaux
Enquête chauffeurs vtc 02-14 - bilan réseaux
 
Marketing : étude du marché et commercialisation
Marketing : étude du marché et commercialisationMarketing : étude du marché et commercialisation
Marketing : étude du marché et commercialisation
 
Information metier hotellerie restauration personnel polyvalent d'hôtellerie ...
Information metier hotellerie restauration personnel polyvalent d'hôtellerie ...Information metier hotellerie restauration personnel polyvalent d'hôtellerie ...
Information metier hotellerie restauration personnel polyvalent d'hôtellerie ...
 
Rendre votre dossier de financement irresistible !
Rendre votre dossier de financement irresistible !Rendre votre dossier de financement irresistible !
Rendre votre dossier de financement irresistible !
 
Barometre emploi jobtransport_juin2014
Barometre emploi jobtransport_juin2014Barometre emploi jobtransport_juin2014
Barometre emploi jobtransport_juin2014
 
Transats Numériques - Atelier 2 "J'utilise du contenu photo video en ligne po...
Transats Numériques - Atelier 2 "J'utilise du contenu photo video en ligne po...Transats Numériques - Atelier 2 "J'utilise du contenu photo video en ligne po...
Transats Numériques - Atelier 2 "J'utilise du contenu photo video en ligne po...
 
Question 5
Question 5Question 5
Question 5
 
Lettre d'information de ...n bui n°13 octobre 2014
Lettre d'information de ...n bui n°13 octobre 2014Lettre d'information de ...n bui n°13 octobre 2014
Lettre d'information de ...n bui n°13 octobre 2014
 
Canal Santé Magazine - Février 2014 - Spécial Nutrition
Canal Santé Magazine - Février 2014 - Spécial NutritionCanal Santé Magazine - Février 2014 - Spécial Nutrition
Canal Santé Magazine - Février 2014 - Spécial Nutrition
 
Cnc 2009 mp_physique_2
Cnc 2009 mp_physique_2Cnc 2009 mp_physique_2
Cnc 2009 mp_physique_2
 
Rapport sur les aides au logement
Rapport sur les aides au logement Rapport sur les aides au logement
Rapport sur les aides au logement
 
François Martin, Pixels Award 2014 - Pixels Festival S01E01
François Martin, Pixels Award 2014 - Pixels Festival S01E01François Martin, Pixels Award 2014 - Pixels Festival S01E01
François Martin, Pixels Award 2014 - Pixels Festival S01E01
 
Bysoft - L’e-Commerce OpenSource est il prêt pour le cross-commerce ?
Bysoft - L’e-Commerce OpenSource est il prêt pour le cross-commerce ?Bysoft - L’e-Commerce OpenSource est il prêt pour le cross-commerce ?
Bysoft - L’e-Commerce OpenSource est il prêt pour le cross-commerce ?
 

Semelhante a Guava et Lombok au Lyon JUG

201209 Lombok & Guava
201209 Lombok & Guava201209 Lombok & Guava
201209 Lombok & Guava
lyonjug
 
Milou fait un régime Guava Lombok
Milou fait un régime Guava LombokMilou fait un régime Guava Lombok
Milou fait un régime Guava Lombok
Lorraine JUG
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
Andres Almiray
 

Semelhante a Guava et Lombok au Lyon JUG (20)

201209 Lombok & Guava
201209 Lombok & Guava201209 Lombok & Guava
201209 Lombok & Guava
 
Milou fait un régime Guava Lombok
Milou fait un régime Guava LombokMilou fait un régime Guava Lombok
Milou fait un régime Guava Lombok
 
Kotlin intro
Kotlin introKotlin intro
Kotlin intro
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Ceylon idioms by Gavin King
Ceylon idioms by Gavin KingCeylon idioms by Gavin King
Ceylon idioms by Gavin King
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
 
Kotlin, a modern language for modern times
Kotlin, a modern language for modern timesKotlin, a modern language for modern times
Kotlin, a modern language for modern times
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
Unleashing kotlin's power
Unleashing kotlin's powerUnleashing kotlin's power
Unleashing kotlin's power
 
Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03Android & Kotlin - The code awakens #03
Android & Kotlin - The code awakens #03
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Code generating beans in Java
Code generating beans in JavaCode generating beans in Java
Code generating beans in Java
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
No excuses, switch to kotlin
No excuses, switch to kotlinNo excuses, switch to kotlin
No excuses, switch to kotlin
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystem
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
 
Kotlin L → ∞
Kotlin L → ∞Kotlin L → ∞
Kotlin L → ∞
 

Mais de Thierry Leriche-Dessirier

Disc l'essentiel
Disc l'essentielDisc l'essentiel
Disc l'essentiel
Thierry Leriche-Dessirier
 
Memento DISC Stable
Memento DISC StableMemento DISC Stable
Memento DISC Stable
Thierry Leriche-Dessirier
 
Memento DISC Influent
Memento DISC InfluentMemento DISC Influent
Memento DISC Influent
Thierry Leriche-Dessirier
 
Memento DISC Dominant
Memento DISC DominantMemento DISC Dominant
Memento DISC Dominant
Thierry Leriche-Dessirier
 
Memento Disc Consciencieux
Memento Disc ConsciencieuxMemento Disc Consciencieux
Memento Disc Consciencieux
Thierry Leriche-Dessirier
 
Management en couleur avec DISC à Agile Tour Paris 2015
Management en couleur avec DISC à Agile Tour Paris 2015Management en couleur avec DISC à Agile Tour Paris 2015
Management en couleur avec DISC à Agile Tour Paris 2015
Thierry Leriche-Dessirier
 
Management en couleur avec DISC
Management en couleur avec DISCManagement en couleur avec DISC
Management en couleur avec DISC
Thierry Leriche-Dessirier
 

Mais de Thierry Leriche-Dessirier (18)

Disc l'essentiel
Disc l'essentielDisc l'essentiel
Disc l'essentiel
 
Rapport DISC Pro de Lucie Durand
Rapport DISC Pro de Lucie DurandRapport DISC Pro de Lucie Durand
Rapport DISC Pro de Lucie Durand
 
Rapport de test DISC de Groupe (Laurent Duval)
Rapport de test DISC de Groupe (Laurent Duval)Rapport de test DISC de Groupe (Laurent Duval)
Rapport de test DISC de Groupe (Laurent Duval)
 
Memento DISC Influent (English)
Memento DISC Influent (English)Memento DISC Influent (English)
Memento DISC Influent (English)
 
Le management en couleurs avec le DISC
Le management en couleurs avec le DISCLe management en couleurs avec le DISC
Le management en couleurs avec le DISC
 
Memento DISC Stable
Memento DISC StableMemento DISC Stable
Memento DISC Stable
 
Memento DISC Influent
Memento DISC InfluentMemento DISC Influent
Memento DISC Influent
 
Memento DISC Dominant
Memento DISC DominantMemento DISC Dominant
Memento DISC Dominant
 
Memento Disc Consciencieux
Memento Disc ConsciencieuxMemento Disc Consciencieux
Memento Disc Consciencieux
 
Management en couleur avec DISC à Agile Tour Paris 2015
Management en couleur avec DISC à Agile Tour Paris 2015Management en couleur avec DISC à Agile Tour Paris 2015
Management en couleur avec DISC à Agile Tour Paris 2015
 
Management en couleur avec DISC
Management en couleur avec DISCManagement en couleur avec DISC
Management en couleur avec DISC
 
Les algorithmes de tri
Les algorithmes de triLes algorithmes de tri
Les algorithmes de tri
 
Cours de Génie Logiciel / ESIEA 2016-17
Cours de Génie Logiciel / ESIEA 2016-17Cours de Génie Logiciel / ESIEA 2016-17
Cours de Génie Logiciel / ESIEA 2016-17
 
Puzzle 2 (4x4)
Puzzle 2 (4x4)Puzzle 2 (4x4)
Puzzle 2 (4x4)
 
Guava au Paris JUG
Guava au Paris JUGGuava au Paris JUG
Guava au Paris JUG
 
Memento scrum-equipe
Memento scrum-equipeMemento scrum-equipe
Memento scrum-equipe
 
Memento java
Memento javaMemento java
Memento java
 
Cours de Génie Logiciel / ESIEA 2013-2014
Cours de Génie Logiciel / ESIEA 2013-2014 Cours de Génie Logiciel / ESIEA 2013-2014
Cours de Génie Logiciel / ESIEA 2013-2014
 

Último

young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
anilsa9823
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
tbatkhuu1
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
home
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
suhanimunjal27
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
amitlee9823
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
amitlee9823
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
University of Wisconsin-Milwaukee
 

Último (20)

VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 

Guava et Lombok au Lyon JUG

  • 1. 03/09/2012 Milou fait un régime Guava - Lombok SPONSORS Cours @thierryleriche Thierry Leriche-Dessirier     Consultant JEE freelance Professeur de Génie Logiciel à l’ESIEA Rédacteur pour Programmez Rédacteur pour Developpez.com icauda.com Forums FAQ News Interviews Critiques Articles / tutoriels Agendas Magazine  Milou est trop lourd (intro) Agenda 13 OOO OOO pages vues par mois 5 500 000 visites par mois 2 500 000 visites uniques par mois 5 000 messages forum par jour  Lombok (et Lombok-pg) en action  Tour d’horizon de Guava 1
  • 2. DOG 03/09/2012 Milou est trop lourd (et doit faire un régime) I am Milou public class Dog { private private private private private private private private private private Integer String String SexeEnum Date String Boolean Double Double List<String> id ; name ; fullName ; sex ; birthday ; race ; lof ; weight ; size ; colors ; 5 ...                       Constructeurs Getters / setters toString () equals () hashCode () compareTo ()           id name fullName sex birthday race lof weight size Colors id name fullName sex birthday race lof weight size Colors ... @Override public int compareTo(Dog other) { int result = 0; result = name.compareTo(other.name); if (result != 0) { return result; } ... } Code très limite… NPE ?... clic clic démo       public class Dog implements Comparable<Dog> { Constructeurs Getters / setters toString () equals () hashCode () compareTo () 10 attributs           Constructeurs Getters / setters toString () equals () hashCode () compareTo ()  210 lignes id name fullName sex birthday race lof weight size Colors 2
  • 3. Java Va chercher Guava  03/09/2012 10 attributs 210 lignes de code     toString () equals () hashCode () compareTo () Commons  FAQ 1-2 Java 5 5 public String toString() { return "Dog [id=" + id + ", name=" + name + ", fullName=" + fullName + ", sex=" + sex + ", birthday=" + birthday + ", race=" + race + ", lof=" + lof + ", weight=" + weight + ", size=" + size + ", colors=" + colors + "]"; } Base Simple, efficace et bien pourri  Objects public String toString() { StringBuilder sb = new StringBuilder(); sb.append(Dog.class.getSimpleName()) .append("[id=").append(id) .append(", name=").append(name) .append(", fullName=").append(fullName) ... .append(", colors=").append(colors); 5 return sb.toString(); Guava Java 5  toString () public String toString() { return Objects.toStringHelper(this) .add("id", id) .add("name", name) .add("fullName", fullName) ... .add("colors", colors) .toString(); } G Builder } Mieux mais sans plus   toString ()  toString () 3
  • 4. 03/09/2012 Java 5 5 Dog other = (Dog) obj; if (birthday == null) { if (other.birthday != null) return false; } else if (!birthday.equals(other.birthday)) return false; if (fullName == null) { if (other.fullName != null) return false; ... } else if (!race.equals(other.race)) return false; if (sex != other.sex) return false; birthday, fullname, name, race et sex Guava birthday, fullname, name, race et sex public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; public boolean equals(Object obj) { if (!(obj instanceof Dog)) return false; Dog other = (Dog) obj; G return Objects.equal(birthday, other.birthday) && Objects.equal(fullname, other.fullname) && Objects.equal(name, other.name) && Objects.equal(race, other.race) && sex == other.sex; } return true; } Carrément illisible   equals ()  equals () result = prime * result + ((birthday == null) ? 0 : birthday.hashCode()); result = prime * result + ((fullName == null) ? 0 : fullName.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((race == null) ? 0 : race.hashCode()); result = prime * result + ((sex == null) ? 0 : sex.hashCode()); 5 birthday, fullname, name, race et sex Guava Java 5 birthday, fullname, name, race et sex public int hashCode() { final int prime = 31; int result = 1; public int hashCode() { return Objects.hashCode(birthday, fullName, name, race, sex); } G return result; }  hashCode ()  hasCode() Java 5 name, fullname, birthday, weight, size, race et sex public int compareTo(Dog other) { int result = 0; result = name.compareTo(other.name); if (result != 0) { return result; } result = fullname.compareTo(other.fullname); if (result != 0) { return result; } 5 name.compareTo(other.name) Vs other.name.compareTo(name) ... }  compareTo ()  compareTo () 4
  • 5. 03/09/2012 name, fullname, birthday, weight, size, race et sex public int compareTo(Dog other) { return ComparisonChain.start() .compare(name, other.name) .compare(fullName, other.fullName) .compare(birthday, other.birthday) .compare(weight, other.weight) .compare(size, other.size) .compare(race, other.race) .compare(sex, other.sex) .result(); } Lombok Guava G  compareTo () Régime en action Régime  @NoArgsConstructor / @RequiredArgsConstructor / @AllArgsConstructor  constructeurs  @Getter / @Setter  @ToString  @EqualsAndHashCode  @Data  @Getter + @Setter + @ToString + @EqualsAndHashCode + @RequiredArgsConstructor Régime Annotations de base clic clic démo  @NoArgsConstructor / @RequiredArgsConstructor / @AllArgsConstructor  @Getter / @Setter  @ToString  @EqualsAndHashCode  @Data Java Lombok 10 attributs 210 lignes de code 12 lignes + 2-3 @ + compareTo() 5
  • 6. @RequiredArgsConstructor(staticName = "of") public class Dog { private Integer id; @NonNull private String name;  private String fullName; @NonNull private SexeEnum sex;  private Date birthday; ...  Dog dog = Dog.of("Milou", MALE); L name, sex Factory Factory 03/09/2012 L @RequiredArgsConstructor(staticName = "of") public class Dog { private Integer id; @NonNull private String name;  private String fullName; @NonNull private SexeEnum sex;  private Date birthday; ...  Dog dog = Dog.of("Milou", MALE); name, sex private Dog(@NonNull final String name, @NonNull final SexeEnum sex) { if (name == null) throw new NullPointerException("name"); if (sex == null) throw new NullPointerException("sex"); this.name = name; this.sex = sex; } Pratique public static Dog of(@NonNull final String name, @NonNull final SexeEnum sex) { return new Dog(name, sex); } Trucs pratiques      @Cleanup @Synchronized @SneakyThrows @Log @Delegate  val public void lireJava6(String from) { InputStream in = null; try { in = new FileInputStream(from); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; ... } } catch (Exception e) { ... } finally { if (in != null) { try { in.close(); } catch (Exception e) { ... } } ... 5 Ressources Ressources Se simplifier la vie public void lire(String from) throws IOException { @Cleanup InputStream in = new FileInputStream(from); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; ... } L } 6
  • 7. 03/09/2012 java -jar lombok.jar delombok src -d src-delomboked Délombok XML <properties> <lombok.sourceDirectory> ${project.basedir}/src/main/java </lombok.sourceDirectory> ... <build> <groupId>org.projectlombok</groupId> <artifactId>maven-lombok-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> </execution> </executions> ... Délombok Revenir sans la lib mvn lombok:delombok Délombok 5 @Data public class Dog { private Integer id; public class Dog { private String name; private Integer id; private String fullName; private String name; private SexeEnum sex; private String fullName; private Date birthday; ... private String race; private List<String> colors; private Boolean lof; private Double weight; public DogLombok() { private Double size; } private List<String> colors; @java.lang.Override } @java.lang.SuppressWarnings("all") public boolean equals(final java.lang.Object o) { if (o == this) return true; if (o == null) return false; mvn lombok:delombok if (o.getClass() != this.getClass()) return false; final Dog other = (Dog)o; if (this.getId() == null ? other.getId() != null : !this.getId().equals(other.getId())) return false; ... 5 Lombok-pg Annotations supplémentaires                  @Builder @Action  @LazyGetter @Function  @FluentSetter @EnumId  @Predicate @Rethrow / @Rethrows @Singleton @AutoGenMethodStub @BoundPropertySupport / @BoundSetter @DoPrivileged @ExtensionMethod @ListenerSupport @WriteLock / @ReadLock @Await / @Signal / @AwaitBeforeAndSignalAfter @Sanitize.Normalize / .With @SwingInvokeLater / @SwingInvokeAndWait @Validate.NotEmpty / .NotNull / .With @VisibleForTesting Fluent Annotations Pg @java.lang.Override @java.lang.SuppressWarnings("all") public int hashCode() { final int PRIME = 31; int result = 1; result = result * PRIME + (this.getId() == null ? 0 : this.getId().hashCode()); result = result * PRIME + (this.getName() == null ? 0 : this.getName().hashCode()); @FluentSetter @Getter public class Dog { private Integer id ; private String name ; private String fullName ; L ...  DogLombok dog = new DogLombok(); dog.name("Milou").sex(MALE); println( dog.getName()); println( dog.getSex()); //  Milou //  MALE 7
  • 8. L @Builder @Getter public class Dog { private Integer id ; private String name ; private String fullName ; extension Builder 03/09/2012 L @ExtensionMethod({ Dog.class, MyDogExtensionClass.class }) public class DogWithExtension { public void foo() { Dog milou = new Dog("Milou", 12.5, ...); boolean isTooFat = milou.isTooFat(); } } ... class MyDogExtensionClass {  Dog dog = Dog.dog().name("Milou").sex(MALE).build(); println( dog.getName()); println( dog.getSex()); public static boolean isTooFat(final Dog dog) { double imc = dog.getWeight() / pow(dog.getSize(), 2); return 25 < imc; } } //  Milou //  MALE Avec paramètres  FAQ 9 Pro / Cons  http://blog.developpez.com/todaystip/p11165/dev/java/extensionmethod-de-lombok-pg/ Lombok or not Lombok ?     Byte code modifié  Version en 0.x Documentation des prog ? Magique ?     Compacité Lecture simplifiée Code normée Délombok (pour essayer) Avantages et inconvénients Guava Factory Methods Les choses que j’aime tour d’horizon 8
  • 9. 03/09/2012 < new Vs static factories > Set<String> colors = new TreeSet<String>(); 5 5 List<Integer> primeNumbers = new ArrayList<Integer>(); Set<String> colors = new TreeSet<String>(); Map<String, Integer> ages = new HashMap<String, Integer>(); dur dur ? Map<String, Integer> ages = new HashMap<String, Integer>(); Java 5 Java 5 < new Vs static factories > List<Integer> primeNumbers = new ArrayList<Integer>(); 5 Map<String, List<String>> trucs = new HashMap<String, List<String>>(); Map<? extends Person, Map<String, List<String>>> trucs = new TreeMap<? extends Person, Map<String, List<String>>>(); Map<? extends Wrapper<String, Sex, Person>, Map<String, List<Set<Adress<String, Integer, Country>>>>> trucs = ... 5 7 Java 7 Map<String, Integer> ages = new HashMap<String, Integer>(); List<Integer> primeNumbers = new ArrayList<>(); Set<String> colors = new TreeSet<>(); Map<String, Integer> ages = new HashMap<>(); List<Integer> primeNumbers = new ArrayList<Integer>(); Set<String> colors = new TreeSet<String>(); 5 7 G Map<String, Integer> ages = new HashMap<String, Integer>(); Java 7 Set<String> colors = new TreeSet<String>(); Java 5 < new Vs static factories > Java 5 < new Vs static factories > List<Integer> primeNumbers = new ArrayList<Integer>(); List<Integer> primeNumbers = new ArrayList<>(); Set<String> colors = new TreeSet<>(); Map<String, Integer> ages = new HashMap<>(); Qui utilise Java 7 en prod ? Guava Qui utilise Java 7 en prod ? List<Integer> primeNumbers = newArrayList(); Set<String> colors = newTreeSet(); Map<String, Integer> ages = newHashMap(); Dès maintenant ! Set<String> colors = new TreeSet<String>(); 5 7 G Java 7 Map<String, Integer> ages = new HashMap<String, Integer>(); List<Integer> primeNumbers = new ArrayList<>(); Set<String> colors = new TreeSet<>(); Map<String, Integer> ages = new HashMap<>(); Guava Qui utilise Java 7 en prod ? List<Integer> primeNumbers = newArrayList(); Set<String> colors = newTreeSet(); Map<String, Integer> ages = newHashMap(); Lombok Dès maintenant ! var primeNumbers = new ArrayList<Integer>(); -> primeNumbers.size(); ... L List of dogs < new Vs static factories > Java 5 < new Vs static factories > List<Integer> primeNumbers = new ArrayList<Integer>(); List<Dog> dogs = newArrayList( new Dog("Milou", 12.5, MALE, ...), new Dog("Rintintin", 45.0, MALE, ...), G new Dog("Volt", 10.3, MALE, ...), new Dog("Lassie", 45.0, FEMALE, ...), new Dog("Pluto", 22.0, MALE, ...), new Dog("Medor", 35.6, MALE, ...)); [Milou, Rintintin, Volt, Lassie, Pluto, Medor] 9
  • 10. 03/09/2012 immutable Quand ? Immutables Pour que ça ne change pas Java 5 < unmodifiables Vs immutables > Set<Integer> temp = new LinkedHashSet<Integer>(Arrays.asList(1, 2, 3, 5, 7)); Set<Integer> primes = Collections.unmodifiableSet(temp); immutable Quand ? mutable On se demande souvent si une liste doit être immutable mais c’est prendre le problème dans le mauvais sens. La plupart du temps, ce qu’il faut vraiment se demander, c’est si la liste a besoin d’être mutable. Set<Integer> primes = Collections.unmodifiableSet(temp); Of < unmodifiables Vs immutables > Java 5 < unmodifiables Vs immutables > Set<Integer> temp = new LinkedHashSet<Integer>(Arrays.asList(1, 2, 3, 5, 7)); ImmutableSet.of(E ImmutableSet.of(E ImmutableSet.of(E ImmutableSet.of(E ImmutableSet.of(E ImmutableSet.of(E e1) e1, e1, e1, e1, e1, E E E E E e2) e2, e2, e2, e2, E E E E G e3) e3, E e4) e3, E e4, E e5) e3, E e4, E e5, E e6, E...) Set<Integer> primes = ImmutableSet.of(1, 2, 3, 5, 7); G Of Guava Ne prend pas de null ImmutableSet.of() Vide Map et List  FAQ 4  http://blog.developpez.com/guava/p10589/collection/title-212/ 10
  • 11. 03/09/2012 < Copie de défense > 5 private String name; ... private List<String> colors; Guava Java 5 < Copie de défense > public class Dog { public class Dog { private String name; ... private ImmutableList<String> colors; G public Dog(String name, List<String> colors) { this.name = name; ... this.colors = ImmutableList.copyOf(colors); } public Dog(String name, List<String> colors) { this.name = name; ... this.colors = Collections.unmodifiableList( new ArrayList<String>(colors)); } public ImmutableList<String> getColors() { return colors; } public List<String> getColors() { return colors; } Message clair Java 5 < Multi Map > Map<String, List<String>> dogFavoriteColors = new HashMap<String, List<String>>(); 5 5 List<String> milouColors = dogFavoriteColors.get("Milou"); if(milouColors == null) { milouColors = new ArrayList<String>(); dogFavoriteColors.put("Milou",milouColors); } milouColors.add("Vert"); Collections en plus Multimap, Bipap, Multiset 5 5 Guava List<String> milouColors = dogFavoriteColors.get("Milou"); if(milouColors == null) { milouColors = new ArrayList<String>(); dogFavoriteColors.put("Milou",milouColors); } milouColors.add("Vert"); Multimap<String, String> dogFavoriteColors = HashMultimap.create(); dogFvoriteColors2.put("Milou", "Jaune"); dogFavoriteColors2.put("Milou", "Rouge"); G < Bi Map > Guava Java 5 < Multi Map > Map<String, List<String>> dogFavoriteColors = new HashMap<String, List<String>>(); BiMap<String, Dog> tatouages = HashBiMap.create(); tatouages.put("ABC123", new Dog("Milou") ); tatouages.put("MBD324", new Dog("Volt") ); tatouages.put("JFT672", new Dog("Lassie") ); G println( tatouages ); {ABC123=Milou, MBD324=Volt, JFT672=Lassie} ABC123=Milou MDB324=Volt JFT672=Lassie println( tatouages.inverse() ); Une map bijective {Milou=ABC123, Volt=MBD324, Lassie=JFT672} Il est possible de changer la valeur associée à une clé mais pas d'avoir deux clés avec la même valeur (IllegalArgumentException). 11
  • 12. 03/09/2012 Guava < Multi Set > Multiset<Integer> ages = HashMultiset.create(); ages.add(2); ages.add(3); ages.add(7); ages.add(11); ages.add(3); ages.add(5); println( ages ); G [2, 3 x 2, 5, 7, 11] Base println( ages.count(3) ) 2 Préconditions, joiner, splitter, optional DOG < Preconditions >     5 public class Dog { private Integer private String private String ... id ; name ; fullName ; public Dog(String name, String fullName, ...) { if(name == null) { throw new NPE("Le nom bla bla"); } if(fullName == null) { throw new NPE("bla bla"); } ... toString () equals () hashCode () compareTo () this.name = name; this.fullName = fullName; ... DOG < Preconditions > import static com.google.common.base.Preconditions.*; public class Dog { private Integer private String private String ... id ; name ; fullName ; G Joiner } String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", "Milou")); "Lassie, Volt, Milou" G public Dog(String name, String fullName, ...) { this.name = checkNotNull(name, "bla bla"); this.fullName = checkNotNull(fullName, "bla bla"); ... } checkNotNull()  NPE checkArgument()  IAE checkState()  ISE Java classique List<String> dogNames = newArrayList("Lassie", "Volt", "Milou"); StringBuilder sb = new StringBuilder(); boolean first = true; for (String name : dogNames) { if(name != null || name.trim().isEmpty()) { continue; } if (!first) { sb.append(", "); } sb.append(name); first = false; } String names = sb.toString(); 12
  • 13. String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", "Milou")); "Lassie, Volt, Milou" G Joiner Joiner 03/09/2012 String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", "Milou")); String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", null, "Milou")); "Lassie, Volt, Milou" G String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", null, "Milou"));  NPE   NPE  G String names = Joiner.on(", ") .skipNulls() .join(newArrayList("Lassie", "Volt", null, "Milou")); "Lassie, Volt, Milou" G Splitter String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", "Milou")); [ "Lassie", "Volt", "Milou" ] Splitter Joiner "Lassie, Volt, Milou" [ "Lassie", "Volt", "Milou" ] Iterable<String> dogNames = Splitter.on(",") .split("Lassie, Volt, Milou"); String names = Joiner.on(", ") .join(newArrayList("Lassie", "Volt", null, "Milou"));  NPE  G G String names = Joiner.on(", ") .skipNulls() .join(newArrayList("Lassie", "Volt", null, "Milou")); "Lassie, Volt, Milou" String names = Joiner.on(", ") .useForNull("Anonymous")) .join(newArrayList("Lassie", "Volt", null, "Milou")); "Lassie, Volt, Anonymous, Milou" Splitter  http://blog.developpez.com/guava/p11054/base/joiner-pour-assembler-des-items/ Iterable<String> dogNames = Splitter.on(",") .split("Lassie, Volt, Milou"); [ "Lassie", "Volt", "Milou" ] Iterable<String> dogNames = Splitter.on(",") .split("Lassie, Volt, Milou"); Iterable<String> dogNames = Splitter.on(",") [ "Lassie", .split("Lassie, Volt, ,Milou"); "Volt", Iterable<String> dogNames = Splitter.on(",") [ "Lassie", .split("Lassie, Volt, ,Milou"); "Volt", " ", "Milou" ] " ", "Milou" ] Iterable<String> dogNames = Splitter.on(",") [ "Lassie", .trimResults() "Volt", .split("Lassie, Volt, ,Milou"); "", "Milou" ] 13
  • 14. Iterable<String> dogNames = Splitter.on(",") .split("Lassie, Volt, Milou"); Optional Splitter 03/09/2012 [ "Lassie", "Volt", "Milou" ] Iterable<String> dogNames = Splitter.on(",") [ "Lassie", .split("Lassie, Volt, ,Milou"); "Volt", Wrapper Optional <T> Dog dog = new Dog("Milou", ...); Optional<Dog> opt = Optional.of(dog); assertTrue( opt.isPresent() ); assertEquals( "Milou", opt.get().getName() ); " ", "Milou" ] Iterable<String> dogNames = Splitter.on(",") [ "Lassie", .trimResults() "Volt", .split("Lassie, Volt, ,Milou"); "", "Milou" ] Iterable<String> dogNames = Splitter.on(",") .trimResults() [ "Lassie", .omitEmptyStrings() .split("Lassie, Volt, ,Milou"); "Volt", G "Milou" ] Wrapper Optional <T> Dog dog = new Dog("Milou", ...); Optional<Dog> opt = Optional.of(dog); assertTrue( opt.isPresent() ); assertEquals( "Milou", opt.get().getName() ); Optional<Dog> opt = Optional.absent(); assertFalse( opt.isPresent() ); opt.get();  ISE Optional Optional  http://blog.developpez.com/guava/p11045/annotation/splitter-pour-separer-des-items/ Wrapper Optional <T> Dog dog = new Dog("Milou", ...); Optional<Dog> opt = Optional.of(dog); assertTrue( opt.isPresent() ); assertEquals( "Milou", opt.get().getName() ); Optional<Dog> opt = Optional.absent(); assertFalse( opt.isPresent() ); opt.get();  ISE Dog dog = null; Optional<Dog> opt = Optional.of(dog);  NPE Optional Optional<Dog> opt = Optional.fromNullable(dog); Wrapper Optional <T> Dog dog = new Dog("Milou", ...); Optional<Dog> opt = Optional.of(dog); assertTrue( opt.isPresent() ); assertEquals( "Milou", opt.get().getName() ); Optional<Dog> opt = Optional.absent(); assertFalse( opt.isPresent() ); opt.get();  ISE Dog dog = null; Optional<Dog> opt = Optional.of(dog);  NPE Functional Programming Optional<Dog> opt = Optional.fromNullable(dog); Dog dog = null; Optional<Dog> opt = Optional.fromNullable(dog); todo Dog dog2 = opt.or( new Dog("noname", ...) ); assertEquals( "noname", dog2.getName() );  http://blog.developpez.com/guava/p11163/base/le-wrapper-optional-de-guava/ 14
  • 15. Super Chien 03/09/2012 Super chien public class SuperChien implements SuperHero { private private private private String double Set<String> Set<String> surnom ; poids ; couleursCostume ; pouvoirs ; 5 ... Les héros peuvent avoir plusieurs costumes donc je n’utilise pas un ImmutableSet. Idem pour les pouvoirs dont la liste augmente avec l’expérience. Dog < Transformation > G chien.setSurnom("Super " + dog.getName()); chien.setPoids(dog.getWeight()); chien.setCouleursCostume(newHashSet(dog.getColors())) chien.setPouvoirs(newHashSet("Code en Java", "Vole")) ... Transformation Transformation < Transformation > List<SuperChien> superChiens = Lists.transform(dogs, new Function<Dog, SuperChien>() { @Override public SuperChien apply(Dog dog) { SuperChien chien = new SuperChien(); List<SuperChien> superChiens = Lists.transform(dogs, new Function<Dog, SuperChien>() { @Override public SuperChien apply(Dog dog) { SuperChien chien = new SuperChien(); ... return chien; } }); return chien; } }); [Super Milou, Super Rintintin, Super Volt, Super Lassie, Super Pluto, Super Medor] G  Vue (lazy)  size / isEmpty dispo   Pas pour traitements répétés   FAQ 3 List<SuperChien> chiens = newArrayList(Lists.transform(... ImmutableList<SuperChien> chiens = ImmutableList.copyOf(Lists.transform(... < Filtre > Filtre G Iterable<Dog> maleDogs = Iterables.filter(dogs, malePredicate); [Milou, Rintintin, Volt, Pluto, Medor] G Filtre < Filtre > Predicate<Dog> malePredicate = new Predicate<Dog>() { public boolean apply(Dog dog) { return dog.getSex() == MALE; } } Predicate<Dog> malePredicate = new Predicate<Dog>() { public boolean apply(Dog dog) { return dog.getSex() == MALE; } } Iterable<Dog> maleDogs = Iterables.filter(dogs, malePredicate); [Milou, Rintintin, Volt, Pluto, Medor] Dog firstMaleDog = Iterables.find(dogs, malePredicate); Milou G G G 15
  • 16. 03/09/2012 Iterable<Dog> maleDogs = Iterables.filter(dogs, malePredicate); [Milou, Rintintin, Volt, Pluto, Medor] Dog firstMaleDog = Iterables.find(dogs, malePredicate); Milou G G G Seconde liste Filtre < Filtre > Predicate<Dog> malePredicate = new Predicate<Dog>() { public boolean apply(Dog dog) { return dog.getSex() == MALE; } } new Dog("Pluto", 22.0, MALE, ...), new Dog("Lulu", 35.6, MALE, ...)); [Rintintin, Pluto, Lulu] Dog firstMaleDog = Iterables.find(femaleDogs, malePredicate, DEFAULT_DOG ); G List<Dog> dogs2 = newArrayList( new Dog("Rintintin", 45.0, MALE, ...), [Milou, Rintintin, Volt, Lassie, Pluto, Medor] Liste 2 : dogs2 Default dog Function<FullDog, String> nameFunction = new Function<FullDog, String>() { public String apply(FullDog dog) { return dog.getName(); }}; Iterable<FullDog> maleDogs = Iterables.filter(dogs, malePredicate); Iterable<String> maleNames = Iterables.transform(maleDogs, nameFunction); G G G < Fluent or not fluent ? > Iterable<FullDog> maleDogs = Iterables.filter(dogs, malePredicate); Iterable<String> maleNames = Iterables.transform(maleDogs, nameFunction); Fluent Pas fluent < Fluent or not fluent ? > Predicate<Dog> malePredicate = new Predicate<Dog>() { public boolean apply(Dog dog) { return dog.getSex() == MALE; }}; Pas fluent Liste 1 : dogs List<String> maleNames2 = FluentIterable.from(dogs) .filter(malePredicate) .transform(nameFunction) .toImmutableList(); G G Guava 12.0 [Milou, Rintintin, Volt, Pluto, Medor] Web Service  http://blog.developpez.com/guava/p11092/annotation/fluentiterable-surmon-chien-guava/ < Memoization > public class DogService { @Inject private PetShopWebService service; public Integer getNumberDogsSoldYesterday() { return service.checkSales( "dog" ); } [Milou, Rintintin, Volt, Pluto, Medor] 5 Cache todo 16
  • 17. 5 Cache manuel Guava @Inject private PetShopWebService service; public Integer getNumberDogsSoldYesterday() { return service.checkSales( "dog" ); } public Integer getNumberDogsSoldYesterday() { if (nbOfDogsSold == null) { nbOfDogsSold = service.checkSales( "dog" ); } return nbOfDogsSold; } < Memoization > G public class DogService { @Inject private PetShopWebService service; private Supplier<Integer> nbOfDogsSoldSupplier = Suppliers.memoize( new Supplier<Integer>() { public Integer get() { return service.checkSales( "dog" ); } }); 5 private Integer nbOfDogsSold; public Integer getNumberDogsSoldYesterday() { return nbOfDogsSoldSupplier.get(); } Double check null… < Memoization > public class DogService { @Inject private PetShopWebService service; private Supplier<Integer> nbOfDogsSoldSupplier = Suppliers.memoizeWithExpiration( new Supplier<Integer>() { public Integer get() { return service.checkSales( "dog" ); } }, 1, TimeUnit.DAYS ); G public Integer getNumberDogsSoldYesterday() { return nbOfDogsSoldSupplier.get(); } Guava Guava < Memoization > public class DogService { < Cache > public class DogService { @Inject private PetShopWebService service; Web Service Web Service 03/09/2012 < Cache > public class DogService { @Inject private PetShopWebService service; private Map<String, Dog> dogMap = Maps.newHashMap(); 5 public Integer getDog(String name) { Dog dog = dogMap.get(name); if(dog == null) { dog = service.getAnimal( "dog", name ); // type-name dogMap.put( name, dog ); } return dog; } Quid du timeout ? Max ? G private LoadingCache<String, Dog> dogCache = CacheBuilder.newBuilder() .maximumSize(2000) .expireAfterWrite(30, TimeUnit.MINUTES) .build(new CacheLoader<String, Dog>() { public Dog load(String key) { return service.getAnimal( "dog", key ); } }); Hash public Integer getDog(String name) { return dogCache.get( name ); // + try-catch } Fantôme 17
  • 18. Hash G HashFunction hf = Hashing.md5(); HashCode hc = hf.newHasher() .putInt(123) .putString("Milou") .hash(); byte[] bytes = hc.asBytes(); md5 Murmur3 128 bits Murmur3 32 bits Sha1 Sha256 Sha512 goodFastHash < Is in list ? > 5 int NB_OF_DOGS = 100000; List<Dog> dogs = newArrayList(); Random rand = new Random(); for (int i = 0; i < NB_OF_DOGS; i++) { Dog dog = new Dog(); dog.setName("abc" + rand.nextInt(999)); ... dogs.add(dog); } contains        Préparation 03/09/2012 5 final Dog milou = new Dog(); milou.setName("Milou"); ... boolean isInList = dogs.contains(milou); false (14 ms) dogs.add(milou); boolean isInList = dogs.contains(milou); Bloom filter < Is in list ? > Funnel<Dog> dogFunnel = new Funnel<Dog>() { public void funnel(Dogdog, PrimitiveSink sink) { sink.putString(dog.getName()) .putString(dog.getFullName()) .putString(dog.getRace()); }}; true (14 ms) G BloomFilter<Dog> bloom = BloomFilter.create(dogFunnel, NB_OF_DOGS, 0.01); for (int i = 0; i < NB_OF_DOGS; i++) { ... bloom.put(dog); Guava or not Guava ? boolean isInList = bloom.mightContain(milou); false (0 ms) bloom.put(milou); boolean isInList = bloom.mightContain(milou); Guava 13 Avantages et inconvénients true (0 ms)  Ne pas en abuser…  Utile  Bonnes pratiques LIENS Pro / Cons  http://blog.developpez.com/guava/p11149/collection/bloom-filter-de-guava-13/ @thierryleriche 18
  • 19. 03/09/2012 ICAUDA Guava  http://icauda.com  http://icauda.com/articles.html  http://icauda.com/cours.html  http://code.google.com/p/guava-libraries Lombok (articles) (slides) Blog Guava  http://projectlombok.org  http://blog.developpez.com/guava Lombok-pg « Simplifier le code de vos beans Java à l'aide de Commons Lang, Guava et Lombok »  https://github.com/peichhorn/lombok-pg  http://thierry-leriche-dessirier.developpez.com/tutoriels/ java/simplifier-code-guava-lombok (article) MERCI FAQ / Bonus 1. Guava Vs Commons 2. Régime façon Apache 3. Fonctionnal prog Vs boucle for 4. Créer des Maps 5. Charsets 6. Converter Spring 7. Orderings 8. Licences 9. Extension avec valeurs 10. Chrono 11. CharMatcher 12. Impl 13. And, or, in… 14. Partition, limit Régime : la méthode Commons ? toString Guava Vs Commons ? public String toString() { return new ToStringBuilder(this) .append("id", id) .append("name", name) .append("fullName", fullName) ... .append("colors", colors) .toString(); http://tinyurl.com/guava-vs-apache L } 1 2 19
  • 20. 03/09/2012 Régime : la méthode Commons ? L Dog other = (Dog) obj; hashCode equals Régime : la méthode Commons ? public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Dog)) return false; return new EqualsBuilder() .append(birthday, other.birthday) .append(fullname, other.fullname) .append(name, other.name) .append(race, other.race) .append(sex, other.sex) .isEquals(); 2 return new HashCodeBuilder(17, 37) .append(birthday) .append(fullname) .append(name) .append(race) .append(sex) .toHashCode(); } } 2 Fonctionnal prog Vs boucle for : quand ? L public int compareTo(Dog other) { return new CompareToBuilder() .append(name, other.name) .append(fullname, other.fullname) .append(birthday, other.birthday) ... .append(sex, other.sex) .toComparison(); } Transformation Régime : la méthode Commons ? compareTo L public int hashCode() { List<SuperChien> superChiens = newArrayList( Lists.transform(dogs, new Function<Dog, SuperChien>() { @Override public SuperChien apply(Dog dog) { SuperChien chien = new SuperChien(); ... return chien; } })); 3 2 G Vs List<SuperChien> superChiens = newArrayList(); for(Dog dog : dogs) { SuperChien chien = new SuperChien(); ... superChiens.add(chien); } G http://code.google.com/p/guava-libraries/wiki/FunctionalExplained Builder G 4 public static final ImmutableMap<String, Integer> AGES = new ImmutableMap.Builder<String, Integer>() .put("Milou", 32) .put("Volt", 7) .put("Pluto", 37) .put("Lassie", 17) .put("Medor", 5) .put("Croquette", 8) .put("Loulou", 2) ... .build(); G Java 5 Charsets String name = "Milou"; try { byte[] bytes = name.getBytes("UTF-8"); 5 } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } Ca n’arrive jamais cette exception  Guava of Créer des Maps public static final ImmutableMap<String, Integer> AGES = ImmutableMap.of("Milou", 32, "Volt", 7, "Pluto", 37, "Lassie", 17); 5 String name = "Milou"; byte[] bytes = name.getBytes(Charsets.UTF_8); G 20
  • 21. 03/09/2012 Converter Spring Orderings G import org.springframework...Converter; @Component("dogToChienConverter") public class DogToChienConverter implements Converter<List<Dog>, List<SuperChien>> { public List<SuperChien> convert(List<Dog> dogs) { List<SuperChien> chiens = newArrayList(transform(dogs, new Function<Dog, SuperChien>() { public SuperChien apply(Dog dog) { ... return chien; } })); return chiens; } 6 7 Lazy or not lazy ? Extension avec valeurs Licences  Lombok :  Lombok-pg :  Guava : @ExtensionMethod({ Object.class, MyOtherClass.class }) public class DogWithExtension { MIT License MIT License public void foo() { String s1 = "toto"; s1.print(); String s2 = null; s2.print(); L //  toto //  null Apache License 2.0 }  Commons : } Apache License 2.0 class MyOtherClass { public static <T> void print(final T value) { System.out.println(value); } } 8 9 On peut mettre autre chose qu’un "Object", par exemple un "Arrays" , un "Dog" , etc. < Chrono > Extension avec valeurs public void foo() { String s1 = "toto"; s1.print(); //  toto String s2 = null; s2.print(); s2.print("vide"); //  null //  vide foo(); // traitement long (ou pas) public static <T extends String> void print(final T value, final T otherValue) { long end = new Date().getTime(); if (value == null || value.isEmpty()) { long System.out.println(otherValue); duration = end - start; // 11 ms } } } } 9 L Guava Java 5 @ExtensionMethod({ Object.class, MyOtherClass.class }) public class DogWithExtension { long start = new Date().getTime(); Stopwatch sw = new Stopwatch(); sw.start(); foo(); class MyOtherClass { public static <T> void print(final T value) { System.out.println(value); } public static void print(String value, String other) { if (value == null || value.isEmpty()) { System.out.println(other); } else { System.out.println(value); } sw.stop(); long duration = sw.elapsedMillis(); // 11 ms 10 5 G long nano = sw.elapsedTime(NANOSECONDS); // 11179739 ns long micro = sw.elapsedTime(MICROSECONDS); // 11179 us long millis = sw.elapsedTime(MILLISECONDS); // 11 ms  http://blog.developpez.com/guava/p11160/base/le-stop-watch-de-guava/ 21
  • 22. 03/09/2012 CharMatcher Impl public class Dog implements Comparable<Dog> { private Integer id ; private String name ; private String fullName ; 5 ... @Override public int compareTo(Dog dog) { return ...; } 11 implements List ? aie aie aie 12 And, or, in… @AutoGenMethodStub public class Dog implements Comparable<Dog> { private Integer id ; private String name ; private String fullName ; L ... And, or, in... Impl boolean isRintintinInBoth = and( in(dogs), in(dogs2) ) .apply(new Dog("Rintintin")); Partition Partition, limit List<List<FullDog>> partition = Lists.partition(dogs, 4); [Milou, Rintintin, Volt, Lassie] [Pluto, Medor] [Milou, Rintintin, Volt, Lassie, Pluto, Medor] Liste 2 : dogs2 13 Liste 1 : dogs 12 Limit true G import static com.google.common.base.Predicates.or; boolean isTintinInOne = or( in(dogs), in(dogs2) ) .apply(new Dog("Tintin"); false [Rintintin, Pluto, Lulu] G Slides en préparation List<FullDog> first4Dogs = newArrayList(Iterables.limit(dogs, 4)); 14 G import static com.google.common.base.Predicates.and; import static com.google.common.base.Predicates.in; [Milou, Rintintin, Volt, Lassie] G 22
  • 23. 03/09/2012 Ressources < I/O > I/O todo public void lireJava6(String from) { InputStream in = null; try { in = new FileInputStream(from); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; ... } } catch (Exception e) { ... } finally { if (in != null) { try { in.close(); } catch (Exception e) { ... } } ... < I/O > 7 Guava Java 7 < I/O > public void lireJava7(String from) { try(InputStream in = new FileInputStream(from)) { ... while (true) { ... } } } 5 Todo G Todo Concurrent / future todo 23