Skip to content

Commit

Permalink
- Update README
Browse files Browse the repository at this point in the history
- Organizes MultipleInheritance and update docs

Signed-off-by: Manoel Campos <[email protected]>
  • Loading branch information
manoelcampos committed Jun 1, 2023
1 parent 04c5dc4 commit 344fedb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ https://gitpod.io/#https://github.com/manoelcampos/jdk-new-features[image:https:
== Examples

- link:src/main/java/samples/jdk08/Jdk08.java[JDK &nbsp; 8 Examples]
- link:src/main/java/samples/jdk08/[JDK &nbsp; 8 Examples]. For functional programming examples, https://github.com/manoelcampos/programacao-funcional-java[check this repo] (only in Portuguese).
- link:src/main/java/samples/jdk09/Jdk09.java[JDK &nbsp; 9 Examples]
- link:src/main/java/samples/jdk10/Jdk10.java[JDK 10 Examples]
- link:src/main/java/samples/jdk11/Jdk11.java[JDK 11 Examples]
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/samples/jdk08/MultipleInheritance.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@

import java.util.regex.Pattern;

/**
* Shows how to use a kind of "multiple inheritance" in JDK 8 with
* default methods in interfaces.
* Thoses methods have a default implementation
* that are inherited by implementing classes.
* The {@link UpperLeet} class applies multiple
* inheritance by implementing the {@link Uppercaser}
* and {@link LeetCode} interfaces.
*/
public class MultipleInheritance {
public static void main(String[] args) {
final var str = "a string with all letters lower case, just to try";
System.out.printf("Original String: %s%n", str);
System.out.printf("Camelcase String: %s%n", new Camelcaser().convert(str));
System.out.printf("Upper Leet String: %s%n", new UpperLeet().convert(str));
}
}
/**
* Changes the chars into a String to anoter representation.
*/
interface StringEncoder {
String convert(String text);
}

}
/**
* Replaces some letters by numbers.
* <a href="https://en.wikipedia.org/wiki/Leet">Leet</a>
*/
interface LeetCode extends StringEncoder {
// Partial list of leet codes.

String[][] leetCodes = {
{"A", "4"},
{"B", "8"},
Expand All @@ -28,7 +46,6 @@ interface LeetCode extends StringEncoder {
{"J", "j"},
{"K", "|<"},
{"O", "0"}};

@Override
default String convert(String text) {
for (final String[] row : leetCodes) {
Expand All @@ -37,8 +54,8 @@ default String convert(String text) {

return text;
}
}

}
/**
* Capitalizes every first letter into a word.
*/
Expand All @@ -48,8 +65,8 @@ public String convert(String text) {
final var matcher = Pattern.compile("(^[a-z])|\\s([a-z])").matcher(text);
return matcher.replaceAll(r -> r.group(0).toUpperCase());
}
}

}
/**
* Converts the String to uppercase
*/
Expand All @@ -58,8 +75,8 @@ interface Uppercaser extends StringEncoder {
default String convert(String text) {
return text.toUpperCase();
}
}

}
/**
* Converts a String into uppercase then to Leet code,
* using the interfaces {@link Uppercaser} and {@link LeetCode}.
Expand Down Expand Up @@ -94,22 +111,5 @@ public String convert(final String text) {
final String upperCase = Uppercaser.super.convert(text);
return LeetCode.super.convert(upperCase);
}
}

/**
* Shows how to use multiple inheritance in JDK 8 with
* default methods in interfaces.
* Thoses methods have a default implementation
* that are inherited by implementing classes.
* The {@link UpperLeet} class applies multiple
* inheritance by implementing the {@link Uppercaser}
* and {@link LeetCode} interfaces.
*/
public class MultipleInheritance {
public static void main(String[] args) {
final var str = "a string with all letters lower case, just to try";
System.out.printf("Original String: %s%n", str);
System.out.printf("Camelcase String: %s%n", new Camelcaser().convert(str));
System.out.printf("Upper Leet String: %s%n", new UpperLeet().convert(str));
}
}

0 comments on commit 344fedb

Please sign in to comment.