Kotlin vs Java Sealed Classes & Interfaces

Sample code explaining differences between sealed classes and interfaces in Java and Kotlin.

Kotlin

code without sealed classes (source):

fun verifyGithubOrganization(gitHub: GitHub, org: String): GHOrganization? {
    return try {
        gitHub.getOrganization(org)
    } catch (ioe: IOException) {
        null
    }
}

usage (source):

  val githubOrganization = Utils.verifyGithubOrganization(github, organizationName)
  githubOrganization?.let {
      val repositories = Utils.listOrgRepos(it, limit = -1)
      repositories.forEach { println("repo: ${it.name}") }
  }

similar code with sealed classes (source):

fun verifyGithubOrganization(gitHub: GitHub, org: String): Organization {
    return try {
        Organization.Success(gitHub.getOrganization(org))
    } catch (ioe: IOException) {
        Organization.Failure(ioe.localizedMessage)
    }
}

usage (source):

  when (org) {
      is Organization.Success -> listOrgRepos(org.githubOrg, limit)
      is Organization.Failure -> emptyList()
  }

Java

code without sealed class (source):

public static List<GHRepository> listRepositories(GHOrganization gitHubOrganization){
    try {
        return gitHubOrganization.listRepositories().toList();
    } catch (IOException ioe) {
        return null;
    }
}

usage (source):

  List<GHRepository> repos = Utils.listRepositories(gitHubOrganizationSuccess.ghOrganization());
  if (repos != null) {
      repos.forEach(repo -> System.out.println(repo.getName().toLowerCase()));
  }

similar code with sealed classes (source):

public static GitHubRepository listRepos(GHOrganization gitHubOrganization){
    try {
        return new GitHubRepositorySuccess(gitHubOrganization.listRepositories().toList());
    } catch (IOException ioe) {
        return new GitHubRepositoryFailure(ioe.getLocalizedMessage());
    }
}

similar code with sealed interfaces and records (source):

public static GitHubRepository listGitHubRepositories(GHOrganization gitHubOrganization){
    try {
        return new GitHubRepositorySuccess(gitHubOrganization.listRepositories().toList());
    } catch (IOException ioe) {
        return new GitHubRepositoryFailure(ioe.getLocalizedMessage());
    }
}

usage with Java 17 (Preview) language level (source)

GitHubOrganization githubOrganization = Utils.verifyGithubOrganization(gitHub, organizationName);
switch (githubOrganization) {
    case GitHubOrganizationSuccess gitHubOrganizationSuccess -> {
        List<GHRepository> repos = Utils.listRepositories(gitHubOrganizationSuccess.ghOrganization());
        repos.forEach(repo -> System.out.println(repo.getName().toLowerCase()));
    }
    case GitHubOrganizationFailure gitHubOrganizationFailure -> {
        System.out.println(gitHubOrganizationFailure.error());
    }
}

usage with plain java 17:

if (gitHubRepository instanceof GitHubRepositorySuccess gitHubRepositorySuccess){
    return gitHubRepositorySuccess.ghRepositories();
} else if (gitHubConnectionResult instanceof GitHubConnectionFailure gitHubConnectionFailure) {
    System.out.println("gitHubConnectionFailure.getError(): " + gitHubConnectionFailure.getError());
} else {
    return 1;
}

Setup

Create a github personal access token with at least the following scopes:

personal access token

How to run the code

Java and Kotlin code reads GitHub organization data provided by the -o flag

Kotlin

cd kotlin
export GITHUB_OAUTH=<your github personal access token>
./gradlew clean build && java -jar app/build/dist/app.jar -o [GitHub organization to read repositories from]

Java

cd java
export GITHUB_OAUTH=<your github personal access token>
./gradlew clean build && java --enable-preview -jar app/build/dist/app.jar -o [GitHub organization to read repositories from]