-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
439 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
15 changes: 15 additions & 0 deletions
15
xxx-record-serialization/src/main/java/ClassSerializationClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.