Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
artem-smotrakov / SwitchExpressionWithInteger.java
Created March 7, 2020 20:04
An example of a switch expression with integers in Java 14
static String print(int n) {
return switch (n) {
case 0 -> "zero";
case 1 -> "one";
case 2 -> "two";
default -> "many";
};
}
@artem-smotrakov
artem-smotrakov / InvalidSwitchExpressionWithoutDefault.java
Created March 7, 2020 20:02
An example of an invalid switch expression without a default clause in Java 14
public class InvalidSwitchExpressionWithoutDefault {
public static void main(String[] args) {
System.out.println(print(1));
}
static String print(int n) {
return switch (n) {
case 0 -> "zero";
case 1 -> "one";
@artem-smotrakov
artem-smotrakov / WhoIsWho.java
Created March 7, 2020 18:56
An example of a switch expression with a default block and yield in Java 14
public class WhoIsWho {
enum Person {
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali,
Gaudi, Bach, Einstein
}
public static void main(String[] args) {
print(Person.Mozart);
@artem-smotrakov
artem-smotrakov / Factorial.java
Created March 7, 2020 18:41
Factorial implementation with switch expression in Java
static int factorial(int n) {
return switch (n) {
case 0, 1 -> 1;
case 2 -> 2;
default -> factorial(n - 1) * n;
};
}
@artem-smotrakov
artem-smotrakov / SwitchExpressionWithDefault.java
Created March 7, 2020 17:26
An example of a switch expression with a default block
String title = switch (person) {
case Dali, Picasso -> "painter";
case Mozart, Prokofiev -> "composer";
case Goethe, Dostoevsky -> "writer";
default -> "...";
};
@artem-smotrakov
artem-smotrakov / InvalidWhoIsWho.java
Created March 7, 2020 17:13
This code doesn't compile because there is an element in the enum which is not covered by any case in the switch expression
public class InvalidWhoIsWho {
enum Person {
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali,
// this element is not covered
// by any case in the switch expression below
// which results to compilation failure
Gaudi
}
@artem-smotrakov
artem-smotrakov / WhoIsWho.java
Created March 7, 2020 16:26
An example of the new switch expressions in Java 14
public class WhoIsWho {
enum Person {
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali
}
public static void main(String[] args) {
print(Person.Mozart);
print(Person.Dali);
print(Person.Dostoevsky);
@artem-smotrakov
artem-smotrakov / ClassicSwitchStatement.java
Last active March 7, 2020 16:06
An example of a switch statement with enum in Java
public class ClassicSwitchStatement {
enum Person {
Mozart, Picasso, Goethe, Dostoevsky, Prokofiev, Dali
}
public static void main(String[] args) {
print(Person.Mozart);
print(Person.Dali);
print(Person.Dostoevsky);
package com.gypsyengineer.jackson.unsafe.one;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.io.Serializable;
public class Person {
public String name;
public int age;
package com.gypsyengineer.jackson.unsafe.one;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
public class SaferPersonDeserialization {
private static final String bad =