Skip to content

Commit

Permalink
adding content for Sip #16
Browse files Browse the repository at this point in the history
  • Loading branch information
wkorando committed Aug 18, 2021
1 parent f9e1c1d commit 10b2bf7
Show file tree
Hide file tree
Showing 36 changed files with 439 additions and 308 deletions.
5 changes: 5 additions & 0 deletions 016-scanner-tokenize/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:16-oraclelinux7

COPY *.jar /run

CMD ["java", "-jar", "/run/*.jar"]
9 changes: 9 additions & 0 deletions 016-scanner-tokenize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Scanner Tokenize


## Further Reading

* Scanner is a Weird but Useful Beast, by Sutart Marks: [https://stuartmarks.wordpress.com/2020/04/14/scanner-is-a-weird-but-useful-beast/](https://stuartmarks.wordpress.com/2020/04/14/scanner-is-a-weird-but-useful-beast/)
* Scanner JDK 16 JavaDoc - [https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Scanner.html](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Scanner.html)

Happy Coding!
File renamed without changes.
49 changes: 49 additions & 0 deletions 016-scanner-tokenize/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bk.soj</groupId>
<artifactId>sip-of-java-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>016-soj-scanner-tokenize</artifactId>
<name>016-soj-scanner-tokenize</name>
<packaging>jar</packaging>

<description>Demonstrating how to use the token apis in Scanner</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>16</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
37 changes: 37 additions & 0 deletions 016-scanner-tokenize/script.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Hey Java developers,

Need to parse an input source?

Consider using the tokenize API part of the Scanner class

The Scanner class can handle many input sources; String, File, InputStream, and more

To tokenize the input source

A regex can be provided to Scanner as a String, or as a Pattern,

to define the delimiter

Processing the tokens can be done as a stream with, .tokens()

Or done as loop with hasNext and next

Scanner also provides hasNext and next for the types;

Int, Long, and Short, and others

allowing for automatic detection and conversion to those types

Scanner can take a locale,

allowing for easy localization of data

Like here treating the final value in the line

as a String when used with the US Locale

But as a numeric,

when used with the German Locale

Happy coding!
29 changes: 29 additions & 0 deletions 016-scanner-tokenize/src/main/java/ScannerTokensI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class ScannerTokensI {

public static void main(String[] args) {
String advocates = """
Billy,Korando,NA,2600,\
David,Delabasse,EMEA,8522,\
Denys,Makogon,EMEA,233,\
José,Paumard,EMEA,6131,\
Nicolai,Parlog,EMEA,12400\
""";

try (Scanner scanner = new Scanner(advocates).useDelimiter(",")) {
Stream<String> stream = scanner.tokens();

stream.forEach(System.out::println);
}

try (Scanner scanner = new Scanner(advocates).useDelimiter(Pattern.compile(","))) {
Stream<String> stream = scanner.tokens();

stream.forEach(System.out::println);
}
}

}
21 changes: 21 additions & 0 deletions 016-scanner-tokenize/src/main/java/ScannerTokensII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Scanner;

public class ScannerTokensII {

public static void main(String[] args) {
String advocates = """
Billy,Korando,NA,2600
David,Delabasse,EMEA,8522
Denys,Makogon,EMEA,233
José,Paumard,EMEA,6131
Nicolai,Parlog,EMEA,12400
""";

try (Scanner scanner = new Scanner(advocates).useDelimiter("[,\n]+")) {
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
}
}

}
25 changes: 25 additions & 0 deletions 016-scanner-tokenize/src/main/java/ScannerTokensIII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Scanner;

public class ScannerTokensIII {

public static void main(String[] args) {
String advocates = """
Billy,Korando,NA,2600
David,Delabasse,EMEA,8522
Denys,Makogon,EMEA,233
José,Paumard,EMEA,6131
Nicolai,Parlog,EMEA,12400
""";

try (Scanner scanner = new Scanner(advocates).useDelimiter("[,\n]+")) {
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
System.out.println(scanner.nextInt() * 2);
} else {
System.out.println(scanner.next());
}
}
}
}

}
36 changes: 36 additions & 0 deletions 016-scanner-tokenize/src/main/java/ScannerTokensIV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.Locale;
import java.util.Scanner;

public class ScannerTokensIV {

public static void main(String[] args) {
String advocates = """
Billy,Korando,NA,2.600
David,Delabasse,EMEA,8.522
Denys,Makogon,EMEA,233
José,Paumard,EMEA,6.131
Nicolai,Parlog,EMEA,12.400
""";

try (Scanner scanner = new Scanner(advocates).useDelimiter("[,]+").useLocale(Locale.US)) {
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
System.out.println(scanner.nextInt() * 2);
} else {
System.out.println(scanner.next());
}
}
}

try (Scanner scanner = new Scanner(advocates).useDelimiter("[,\n]+").useLocale(Locale.GERMANY)) {
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
System.out.println(scanner.nextInt() * 2);
} else {
System.out.println(scanner.next());
}
}
}
}

}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
<module>013-javadoc</module>
<module>014-scanner</module>
<module>015-record-projections</module>
<module>016-scanner-tokenize</module>
<module>xxx-streams-updates</module>
<module>xxx-new-api-javadoc</module>
<module>xxx-transfer-jmx</module>
<module>xxx-teeing</module>
<module>xxx-intermediate-types</module>
<module>xxx-record-serialization</module>
</modules>

</project>
5 changes: 5 additions & 0 deletions xxx-record-serialization/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:16-oraclelinux7

COPY *.jar /run

CMD ["java", "-jar", "/run/*.jar"]
9 changes: 9 additions & 0 deletions xxx-record-serialization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Scanner Tokenize


## Further Reading

* Scanner is a Weird but Useful Beast, by Sutart Marks: [https://stuartmarks.wordpress.com/2020/04/14/scanner-is-a-weird-but-useful-beast/](https://stuartmarks.wordpress.com/2020/04/14/scanner-is-a-weird-but-useful-beast/)
* Scanner JDK 16 JavaDoc - [https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Scanner.html](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Scanner.html)

Happy Coding!
49 changes: 49 additions & 0 deletions xxx-record-serialization/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bk.soj</groupId>
<artifactId>sip-of-java-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>xxx-soj-record-serialization</artifactId>
<name>xxx-soj-record-serialization</name>
<packaging>jar</packaging>

<description>Demonstrating how to use the token apis in Scanner</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>16</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
45 changes: 45 additions & 0 deletions xxx-record-serialization/script.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Hey Java developers,

Excited for Java Records

but wondering where they fit in

if you are using JPA for data access?

[Comedy bit]

Consider using records as projections!

Added in java 16, Records are transparent carriers of data,

and can be defined in a single line

Because of the constraints on records,

are unsuitable as JPA entities,

but can be used as projections

If using straight JPA,

Records can be used with CriteriaBuilders,

TypedQueries

And Native Queries

Spring Data also supports records

If the record matches the field names of the tracked entity,

Spring data can automatically handle the mapping

Alternatively, a JPA query can be defined

Or a custom repo implementation

can be used to handle the mapping of the query results

to the record

Happy coding!
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class ClassSerializationClient {

public static void main(String[] args) throws Exception {
var address = UnixDomainSocketAddress.of("/mnt/server");
try (var clientChannel = SocketChannel.open(address)) {
ByteBuffer buf = ByteBuffer.wrap("Hello world".getBytes());
clientChannel.write(buf);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.nio.channels.ServerSocketChannel;
import java.nio.file.Files;

public class UnixSocketServer {
public class ClassSerializationServer {

public static void main(String[] args) throws Exception {
var address = UnixDomainSocketAddress.of("/mnt/server");
Expand Down
Loading

0 comments on commit 10b2bf7

Please sign in to comment.