Skip to content

Instantly share code, notes, and snippets.

View adibfara's full-sized avatar

Adib Faramarzi adibfara

View GitHub Profile
public sealed interface Item permits Box, Container { }
@adibfara
adibfara / Main.java
Created October 18, 2023 09:17
Visitor Pattern Usage
public static void main(String[] args) {
var ensemble = new Container(
new Item[]{
new Box(2),
new Container(new Item[]{
new Box(3),
new Box(4)}
)});
ensemble.visit(new PrinterVisitor());
@adibfara
adibfara / 1ItemVisitor.java
Last active October 18, 2023 09:17
VisitorPattern
public interface ItemVisitor<T> {
T visit(Box box);
T visit(Container container);
}
public interface Item {
<T> T visit(ItemVisitor<T> itemVisitor);
}
@adibfara
adibfara / WeightCalculator.java
Last active October 18, 2023 09:08
Fixing the double dispatch with default branch
public class WeightCalculator {
public int calculate(Item item) {
return switch (item) {
case Box box -> calculate(box);
case Container container -> calculate(container);
default -> throw new IllegalStateException("Unexpected value: " + item);
};
}
public int calculate(Box box) {
return box.size();
@adibfara
adibfara / 1.java
Created October 18, 2023 08:52
No Visitor
public record Box(int size) implements Item {}
public record Container(Item[] items) implements Item { }
public interface Item {
void printStructure();
int weight();
}
@adibfara
adibfara / ListedProcessor.kt
Created May 5, 2022 14:06
Completing the processor
internal class ListedProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
private fun Resolver.findAnnotations(
kClass: KClass<*>,
) = getSymbolsWithAnnotation(
kClass.qualifiedName.toString())
.filterIsInstance<KSFunctionDeclaration>().filter {
@adibfara
adibfara / ListedProcessor.kt
Created May 5, 2022 13:52
Simple Code Generator
internal class ListedProcessor(
private val environment: SymbolProcessorEnvironment,
) : SymbolProcessor {
private fun Resolver.findAnnotations(
kClass: KClass<*>,
) = getSymbolsWithAnnotation(
kClass.qualifiedName.toString())
.filterIsInstance<KSFunctionDeclaration>().filter {
it.parameters.isEmpty()
@adibfara
adibfara / build.gradle
Created May 5, 2022 08:31
Adding the generator to the application's build.gradle
// Inside your app's module, declare a dependency to your ksp module
dependencies {
implementation project(":ksp") // since you want to use your @Listed annotation
ksp project(":ksp") // to make KSP work
}
@adibfara
adibfara / SymbolProcessorProvider.kt
Created May 5, 2022 08:11
Adding a Service Loader
// Create the directories: yourmodule/src/main/resources/META-INF/services/
// Create a file named com.google.devtools.ksp.processing.SymbolProcessorProvider
// Inside the file, add the following line (and nothing more):
com.snaky.ksp.processor.provider.ListedProcessorProvider
// For a reference, you can check out here:
// https://github.com/adibfara/ListGen/blob/main/ksp/src/main/resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider