Skip to content

Instantly share code, notes, and snippets.

View benhlal's full-sized avatar
😀
I may be slow to respond.

benhlal benhlal

😀
I may be slow to respond.
View GitHub Profile
@benhlal
benhlal / sealedClass
Created August 24, 2021 18:16
Sealed Class to restrict inheritance
public abstract sealed class Animal
permits Cat,Dog {....}
//
public class Cat extends Animal{...} //Complies fine
public class Dog extends Animal{...} //Complies fine
public class Lion extends Animal{...}//Compiler will complain
// Your DTO
record Person (String firstName, String lastName, int age){}
//Usage in a TestClass (psvm)
var p = new Person('youness','ben',99);
@benhlal
benhlal / instanceof.java
Created August 24, 2021 18:01
pattern matching instanceof
//Before
if (person instanceof Student) {
Student student = (Student) person;
student.subscribeToCourse();
}
//After
if(obj instanceof Student student) student.subscribeToCourse();
var mediaJSON = { "categories" : [ { "name" : "Movies",
"videos" : [
{ "description" : "Big Buck Bunny tells the story of a giant rabbit with a heart bigger than himself. When one sunny day three rodents rudely harass him, something snaps... and the rabbit ain't no bunny anymore! In the typical cartoon tradition he prepares the nasty rodents a comical revenge.\n\nLicensed under the Creative Commons Attribution license\nhttp://www.bigbuckbunny.org",
"sources" : [ "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" ],
"subtitle" : "By Blender Foundation",
"thumb" : "images/BigBuckBunny.jpg",
"title" : "Big Buck Bunny"
},
{ "description" : "The first Blender Open Movie from 2006",
"sources" : [ "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" ],