Skip to content

Commit 8d6af0b

Browse files
committed
Revert "Ejemplos no usados en clase, borrados"
This reverts commit cb94352.
1 parent 3fa3891 commit 8d6af0b

6 files changed

Lines changed: 337 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.platzi.functional._10_chaining;
2+
3+
import com.platzi.functional._10_chaining.builder.BuilderNoChaining;
4+
import com.platzi.functional._10_chaining.builder.BuilderWithChaining;
5+
import com.platzi.functional._10_chaining.data.Account;
6+
7+
import java.util.function.Function;
8+
9+
public class Chaining {
10+
11+
/**
12+
* Aunque el chaining no es algo exclusivo o a conocer de la programacion funcional,
13+
* vale la pena mencionarlo, puesto que puede ser de ayuda al usar funciones de java o
14+
* en temas futuros…
15+
* <p>
16+
* Chaining es la estrategia de retornar siempre un objeto, tal que puedas invocar
17+
* metodos con cada invocacion.
18+
*/
19+
static void examplesOfChaining() {
20+
//Cuando no usas chaining tu codigo se ve aburrido y repetitivo
21+
BuilderNoChaining builderNoChaining = new BuilderNoChaining("");
22+
builderNoChaining.withBalance(100.00);
23+
builderNoChaining.withFirstName("Sinuhe");
24+
builderNoChaining.withLastName("Jaime");
25+
builderNoChaining.withPhone("+52155555555");
26+
27+
Account sinuheAccount = builderNoChaining.buildAccount();
28+
29+
//Pero con un buen chaining puedes hacer las cosas mas simples.
30+
Account anotherAccount =
31+
new BuilderWithChaining("1")
32+
.withBalance(100.00)
33+
.withFirstName("Sinuhe")
34+
.withLastName("Jaime")
35+
.withPhone("+52155555555")
36+
.buildAccount();
37+
38+
//Es relativamente comun verlo en Strings:
39+
char[] saludo = "Bye Platzi!".replaceAll("Bye", "Hola")
40+
.toLowerCase()
41+
.toCharArray();
42+
43+
//O como en el primer ejemplo, con builders…
44+
StringBuilder builderJava = new StringBuilder()
45+
.append("E").append("s").append("t").append("o").append("y")
46+
.append(" ")
47+
.append("A")
48+
.append("p")
49+
.append("r")
50+
.append("e")
51+
.append("n")
52+
.append("d")
53+
.append("i")
54+
.append("e")
55+
.append("n")
56+
.append("d")
57+
.append("o")
58+
.append(" Java ")
59+
.append(8);
60+
61+
System.out.println(builderJava.toString());
62+
63+
64+
//La reelevancia en este curso, de esta estrategia, viene al crear funciones mas complejas
65+
//o que se crean a partir de otras funciones. Posteriormente veremos que es de mucha utilidad
66+
//con ciertos tipos de datos.
67+
68+
//Por ejemplo, creamos una funcion que reemplace las "f" por asteriscos
69+
Function<String, String> replacer = s -> s.replaceAll("f", "*");
70+
71+
72+
Function<Function<String, String>, Function<String, String>> replacerPlus =
73+
f -> f.andThen(s -> s.replaceAll("a", "/"));
74+
75+
System.out.println(
76+
replacerPlus
77+
.apply(replacer)
78+
.andThen(s -> s.replaceFirst("Ho", "Mo"))
79+
.apply("Hablando de chaining, esto Hola, fucho")
80+
);
81+
}
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.platzi.functional._10_chaining.builder;
2+
3+
import com.platzi.functional._10_chaining.data.Account;
4+
import com.platzi.functional._10_chaining.data.Phone;
5+
6+
public class BuilderNoChaining {
7+
private String id;
8+
9+
private String firstName;
10+
private String lastName;
11+
12+
private double currentBalance;
13+
14+
private Phone phone;
15+
16+
public BuilderNoChaining(String id) {
17+
checkStringField(id, "ID");
18+
this.id = id;
19+
firstName = "";
20+
lastName = "";
21+
currentBalance = 0.0;
22+
phone = null;
23+
}
24+
25+
public void withFirstName(String firstName) {
26+
checkStringField(firstName, "First Name");
27+
this.firstName = firstName;
28+
}
29+
30+
public void withLastName(String lastName) {
31+
checkStringField(lastName, "First Name");
32+
this.lastName = lastName;
33+
}
34+
35+
public void withBalance(double balance) {
36+
currentBalance = balance;
37+
}
38+
39+
public void withPhone(String phone){
40+
checkStringField(phone, "Phone");
41+
this.phone = new Phone(phone);
42+
}
43+
44+
public Account buildAccount() {
45+
return new Account(
46+
id,
47+
firstName,
48+
lastName,
49+
currentBalance,
50+
phone
51+
);
52+
}
53+
54+
private void checkStringField(String field, String fieldName) {
55+
if (field == null || field.length() == 0) {
56+
throw new IllegalArgumentException(fieldName + " is not valid");
57+
}
58+
}
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.platzi.functional._10_chaining.builder;
2+
3+
import com.platzi.functional._10_chaining.data.Account;
4+
import com.platzi.functional._10_chaining.data.Phone;
5+
6+
public class BuilderWithChaining {
7+
private String id;
8+
9+
private String firstName;
10+
private String lastName;
11+
12+
private double currentBalance;
13+
14+
private Phone phone;
15+
16+
public BuilderWithChaining(String id) {
17+
checkStringField(id, "ID");
18+
this.id = id;
19+
firstName = "";
20+
lastName = "";
21+
currentBalance = 0.0;
22+
phone = null;
23+
}
24+
25+
public BuilderWithChaining withFirstName(String firstName) {
26+
checkStringField(firstName, "First Name");
27+
this.firstName = firstName;
28+
return this;
29+
}
30+
31+
public BuilderWithChaining withLastName(String lastName) {
32+
checkStringField(lastName, "First Name");
33+
this.lastName = lastName;
34+
return this;
35+
}
36+
37+
public BuilderWithChaining withBalance(double balance) {
38+
currentBalance = balance;
39+
return this;
40+
}
41+
42+
public BuilderWithChaining withPhone(String phone) {
43+
checkStringField(phone, "Phone");
44+
this.phone = new Phone(phone);
45+
return this;
46+
}
47+
48+
public Account buildAccount() {
49+
return new Account(
50+
id,
51+
firstName,
52+
lastName,
53+
currentBalance,
54+
phone
55+
);
56+
}
57+
58+
private void checkStringField(String field, String fieldName) {
59+
if (field == null || field.length() == 0) {
60+
throw new IllegalArgumentException(fieldName + " is not valid");
61+
}
62+
}
63+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.platzi.functional._10_chaining.data;
2+
3+
public class Account {
4+
private String id;
5+
6+
private String firstName;
7+
private String lastName;
8+
9+
private double currentBalance;
10+
11+
private Phone phone;
12+
13+
public Account(String id, String firstName, String lastName, double currentBalance, Phone phone) {
14+
this.id = id;
15+
this.firstName = firstName;
16+
this.lastName = lastName;
17+
this.currentBalance = currentBalance;
18+
this.phone = phone;
19+
}
20+
21+
public String getId() {
22+
return id;
23+
}
24+
25+
public String getFirstName() {
26+
return firstName;
27+
}
28+
29+
public String getLastName() {
30+
return lastName;
31+
}
32+
33+
public double getCurrentBalance() {
34+
return currentBalance;
35+
}
36+
37+
public Phone getPhone() {
38+
return phone;
39+
}
40+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.platzi.functional._10_chaining.data;
2+
3+
public class Address {
4+
private String street;
5+
6+
private String interiorNumber;
7+
8+
private String exteriorNumber;
9+
10+
private String state;
11+
12+
private String zip;
13+
14+
public String getStreet() {
15+
return street;
16+
}
17+
18+
public void setStreet(String street) {
19+
this.street = street;
20+
}
21+
22+
public String getInteriorNumber() {
23+
return interiorNumber;
24+
}
25+
26+
public void setInteriorNumber(String interiorNumber) {
27+
this.interiorNumber = interiorNumber;
28+
}
29+
30+
public String getExteriorNumber() {
31+
return exteriorNumber;
32+
}
33+
34+
public void setExteriorNumber(String exteriorNumber) {
35+
this.exteriorNumber = exteriorNumber;
36+
}
37+
38+
public String getState() {
39+
return state;
40+
}
41+
42+
public void setState(String state) {
43+
this.state = state;
44+
}
45+
46+
public String getZip() {
47+
return zip;
48+
}
49+
50+
public void setZip(String zip) {
51+
this.zip = zip;
52+
}
53+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.platzi.functional._10_chaining.data;
2+
3+
public class Phone {
4+
private String country;
5+
6+
private char[] countryCode;
7+
8+
private char[] digits;
9+
10+
public Phone() {
11+
}
12+
13+
public Phone(String phoneString) {
14+
//TODO: Descomponer los valores de un string
15+
}
16+
17+
public String getCountry() {
18+
return country;
19+
}
20+
21+
public void setCountry(String country) {
22+
this.country = country;
23+
}
24+
25+
public char[] getCountryCode() {
26+
return countryCode;
27+
}
28+
29+
public void setCountryCode(char[] countryCode) {
30+
this.countryCode = countryCode;
31+
}
32+
33+
public char[] getDigits() {
34+
return digits;
35+
}
36+
37+
public void setDigits(char[] digits) {
38+
this.digits = digits;
39+
}
40+
}

0 commit comments

Comments
 (0)