Skip to content

Commit f7fa91a

Browse files
committed
Adding snippets and testing tools
1 parent 48385a5 commit f7fa91a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+672
-1
lines changed

.github/tools/pom.xml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>tools</artifactId>
7+
<version>1.0.0</version>
8+
<groupId>dev.playground</groupId>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<mainClass>dev.playground.tools.IndexJson</mainClass>
15+
<junit.version>5.9.2</junit.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter</artifactId>
22+
<version>${junit.version}</version>
23+
<scope>compile</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-engine</artifactId>
28+
<version>${junit.version}</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit.jupiter</groupId>
33+
<artifactId>junit-jupiter-api</artifactId>
34+
<version>${junit.version}</version>
35+
<scope>compile</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<artifactId>maven-jar-plugin</artifactId>
43+
<version>3.0.2</version>
44+
<configuration>
45+
<archive>
46+
<manifest>
47+
<addClasspath>true</addClasspath>
48+
<mainClass>${mainClass}</mainClass>
49+
</manifest>
50+
</archive>
51+
</configuration>
52+
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.11.0</version>
57+
<configuration>
58+
<source>${maven.compiler.source}</source>
59+
<target>${maven.compiler.target}</target>
60+
</configuration>
61+
</plugin>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-surefire-plugin</artifactId>
65+
<version>2.22.1</version>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dev.playground.tools;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.util.stream.Collectors;
8+
9+
public class IndexJson {
10+
11+
static final String rawGithubCDNPrefix = "https://raw.githubusercontent.com/java/playground-snippets/main/";
12+
13+
public static void main(String[] args) throws IOException {
14+
if (args.length < 1) {
15+
System.err.println("USAGE:\n\t[program] <path-to-snippets-folder>");
16+
System.exit(1);
17+
}
18+
var strPath = args[0];
19+
var path = Path.of(strPath);
20+
if (!path.toFile().exists()) {
21+
System.err.printf("Directory %s does not exist!\nUSAGE:\n\t[program] <path-to-snippets-folder>\n", path);
22+
System.exit(1);
23+
}
24+
25+
String result;
26+
try(var walker = Files.walk(path)) {
27+
result = walker.filter(f -> !Files.isDirectory(f)).map(key -> {
28+
var strKey = key.toString().replace(strPath + "/", "");
29+
var strValue = rawGithubCDNPrefix + strKey;
30+
31+
return String.format("\t\"%s\": \"%s\"", strKey, strValue);
32+
}).collect(Collectors.joining(",\n"));
33+
}
34+
var indexJsonContent = String.format("{\n%s\n}", result);
35+
var indexJsonFilePath = Path.of(strPath, "index.json");
36+
System.out.println(indexJsonFilePath);
37+
Files.deleteIfExists(indexJsonFilePath);
38+
indexJsonFilePath.toFile().createNewFile();
39+
40+
Files.writeString(indexJsonFilePath, indexJsonContent);
41+
System.out.println(indexJsonContent);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# DISCLAIMER
2+
3+
4+
[snippets](snippets) is a symlink to a [root folder](../../../../../snippets).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var name = "Duke";
2+
3+
System.out.println("Hello, " + name);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"misc/simple-collection.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/misc/simple-collection.snippet",
3+
"misc/func-interface.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/misc/func-interface.snippet",
4+
"misc/functions-chaining.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/misc/functions-chaining.snippet",
5+
"records/composing.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/records/composing.snippet",
6+
"records/simple.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/records/simple.snippet",
7+
"records/method-overriding.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/records/method-overriding.snippet",
8+
"streams/filtering.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/streams/filtering.snippet",
9+
"streams/stream-to-list.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/streams/stream-to-list.snippet",
10+
"streams/text-block-to-stream.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/streams/text-block-to-stream.snippet",
11+
"textblocks/formatting.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/textblocks/formatting.snippet",
12+
"textblocks/simple.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/textblocks/simple.snippet",
13+
"textblocks/record.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/textblocks/record.snippet",
14+
"hello-world.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/hello-world.snippet",
15+
"switch/expression.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/switch/expression.snippet",
16+
"switch/pattern-matching.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/switch/pattern-matching.snippet",
17+
"switch/statement.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/switch/statement.snippet",
18+
"pattern-matching/record.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/pattern-matching/record.snippet",
19+
"pattern-matching/switch.snippet": "https://raw.githubusercontent.com/java/playground-snippets/main/pattern-matching/switch.snippet"
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@FunctionalInterface
2+
interface MyFunctionalInterface {
3+
public int incrementByTwo(int a);
4+
}
5+
6+
MyFunctionalInterface f = (num) -> num + 2;
7+
var result = f.incrementByTwo(40);
8+
9+
System.out.println(result);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var strings = Arrays.asList("one", null, "three", "", "five", "six");
2+
3+
Function<String, String> handleNull = s -> s == null ? "": s;
4+
Function<String, Integer> length = s -> s.length();
5+
Function<String, Integer> handleNullThenLength = handleNull.andThen(length);
6+
7+
for (String string : strings) {
8+
System.out.println(string + " -> length = " + handleNullThenLength.apply(string));
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Collection<String> numbers = new ArrayList<>();
2+
numbers.add("one");
3+
numbers.add("two");
4+
numbers.add("three");
5+
numbers.remove("two");
6+
System.out.println(numbers);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
record Point(int x, int y) {
2+
public boolean equals(Object other) {
3+
return other instanceof Point otherPoint &&
4+
this.x == otherPoint.x && this.y == otherPoint.y;
5+
}
6+
}
7+
8+
Point p1 = new Point(0, 0);
9+
Point p2 = new Point(1, 1);
10+
Point p3 = new Point(0, 0);
11+
System.out.println("p1 equals object? " + p1.equals(new Object()));
12+
System.out.println("p1 equals p2? " + p1.equals(p2));
13+
System.out.println("p1 equals p3? " + p1.equals(p3));
14+
System.out.println("p2 equals p3? " + p2.equals(p3));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
record Point(int x, int y) {}
2+
3+
record Circle(Point center, int radius) {
4+
public Circle {
5+
if (radius < 0) throw new IllegalArgumentException("Radius cant be null");
6+
}
7+
}
8+
9+
ToDoubleFunction<Object> surface = object ->
10+
switch(object) {
11+
case null -> throw new AssertionError("Object is null");
12+
case Circle(Point p, int radius) when radius == 0 -> 0d;
13+
case Circle(Point p, int radius) -> Math.PI*radius*radius;
14+
case Object notACircle -> throw new AssertionError("Object is not a circle");
15+
};
16+
17+
Circle c1 = new Circle(new Point(0, 0), 0);
18+
Circle c2 = new Circle(new Point(1, 1), 1);
19+
20+
System.out.println("Surface of " + c1 + " = " + surface.applyAsDouble(c1));
21+
System.out.println("Surface of " + c2 + " = " + surface.applyAsDouble(c2));
22+
surface.applyAsDouble(null);
23+
surface.applyAsDouble("Not a circle!");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
record Population(int population) {}
2+
record City(String name, Population population) {
3+
// static methods are allowed in records
4+
public static City of(String name, int p) {
5+
var population = new Population(p);
6+
return new City(name, population);
7+
}
8+
}
9+
10+
var paris = City.of("Paris", 2_161);
11+
System.out.println(paris);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
record City(String name) {
2+
3+
public boolean equals(Object other) {
4+
return other instanceof City(String name) &&
5+
this.name.equalsIgnoreCase(name);
6+
}
7+
8+
public int hashCode() {
9+
return name != null ? name.toUpperCase().hashCode() : 0;
10+
}
11+
}
12+
13+
var paris1 = new City("Paris");
14+
var paris2 = new City("paris");
15+
var paris3 = new City("PARIS");
16+
System.out.println("1 == 2 ? " + paris1.equals(paris2));
17+
System.out.println("2 == 3 ? " + paris2.equals(paris3));
18+
System.out.println("1 == 3 ? " + paris1.equals(paris3));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
record Player(String last, String first, int level) {}
2+
var jane = new Player("Doe", "Jane", 42);
3+
System.out.println(jane);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
List<String> cities =
2+
Arrays.asList("Shenzhen", "Brussels", "Taipei", "Buenos Aires", "Sydney", "Bristol");
3+
4+
cities.stream()
5+
.filter(s -> s.startsWith("B"))
6+
.map(String::toUpperCase)
7+
.sorted()
8+
.forEach(System.out::println);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Stream<String> stream = Stream.of("Shenzhen", "Brussels", "Taipei", "Buenos Aires", "Sydney", "Bristol");
2+
3+
List<String> cities = stream.sorted().collect(Collectors.toList());
4+
5+
System.out.println(cities);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var cities = """
2+
San Francisco
3+
Casablanca
4+
Antwerp
5+
New Delhi
6+
Osaka
7+
""";
8+
9+
Stream<String> lines = cities.lines();
10+
11+
System.out.println(lines.toList());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; }
2+
3+
Day day = Day.SUNDAY;
4+
int numLetters = switch (day) {
5+
case MONDAY, FRIDAY, SUNDAY -> 6;
6+
case TUESDAY -> 7;
7+
case THURSDAY, SATURDAY -> 8;
8+
case WEDNESDAY -> 9;
9+
};
10+
11+
System.out.println(numLetters);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var result = switch ("Lorem Ipsum") {
2+
case null -> "Oops";
3+
case "Foo", "Bar" -> "Great";
4+
default -> "Ok!";
5+
};
6+
System.out.println(result);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; }
2+
var numberOfChar = 0;
3+
4+
switch(Day.MONDAY) {
5+
case MONDAY: case FRIDAY: case SUNDAY:
6+
numberOfChar = 6;
7+
break;
8+
case TUESDAY:
9+
numberOfChar = 7;
10+
break;
11+
case WEDNESDAY:
12+
numberOfChar = 9;
13+
break;
14+
case THURSDAY: case SATURDAY:
15+
numberOfChar = 8;
16+
break;
17+
default:
18+
throw new IllegalArgumentException();
19+
}
20+
21+
System.out.println(numberOfChar);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var message = """
2+
{"name" : "%s", "language" : "Java"}
3+
""";
4+
System.out.println(message.formatted("Duke"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
record City(String name, int population) {
2+
3+
public static City of(String line) {
4+
int indexOfLastSpace = line.lastIndexOf(' ');
5+
var name = line.substring(0, indexOfLastSpace);
6+
var population = Integer.parseInt(line.substring(indexOfLastSpace + 1));
7+
return new City(name, population);
8+
}
9+
}
10+
11+
String cities = """
12+
New York 8343
13+
London 8982
14+
Paris 2161
15+
Bangaluru 8474
16+
Singapore 5454
17+
Tokyo 13960
18+
""";
19+
20+
var cityList = cities.lines().map(City::of).toList();
21+
cityList.forEach(System.out::println);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
String cities = """
2+
New York \s
3+
Paris \s
4+
Bangalore \s
5+
Singapore \s
6+
Tokyo \s
7+
""";
8+
9+
cities.lines().map(line -> "|" + line + "|").forEach(System.out::println);

0 commit comments

Comments
 (0)