Skip to content

mtumilowicz/java11-enum-clean-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java11-enum-clean-code

Overview of how to to construct modern enums using lambda.

project description

We have enum JobTitle

public enum JobTitle { PRESIDENT, VICE_PRESIDENT, OFFICER, MANAGER, DEVELOPER, CONSULTANT, IMPLEMENTATION_OFFICER, INTERN; } 

and groups:

  • ADMINISTRATION = PRESIDENT, VICE_PRESIDENT

  • OPERATIONAL = MANAGER, OFFICER

  • it is a good practice to provide static methods that return predicates:

    public static Predicate<JobTitle> administration() { return JobTitle::isAdministration; } public static Predicate<JobTitle> operational() { return JobTitle::isOperational; } 

    where, obviously:

    public boolean isAdministration() { return ADMINISTRATION.contains(this); } public boolean isOperational() { return OPERATIONAL.contains(this); } 
  • and a stream() method:

    public static Stream<JobTitle> stream() { return Arrays.stream(values()); } 

because later in the code it's easy to write very expressive code (like in JobTitleTest):

@Test public void isBusinessShowcase() { JobTitle.stream() .filter(administration().or(operational())) .forEach(title -> System.out.println("Performed action for: " + title)); } 

About

Overview of how to to construct modern enums using lambda.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages