Git Product home page Git Product logo

kotlinsealedclassesslideless's Introduction

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]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.