DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Java Switch on Steroids

Java Switch on Steroids

Mario Fusco user avatar by
Mario Fusco
·
Apr. 12, 10 · Interview
Like (0)
Save
Tweet
Share
10.85K Views

Join the DZone community and get the full member experience.

Join For Free
The native Java switch construct has been often criticized for its limited applicability, since it can operate only on ints, chars and (starting from Java 5) enums. For example Scala has demonstrated how power and flexible its pattern matching construct could be. Getting inspiration from it, the last version of lambdaj introduces a Switcher object that through a smart use of its closures and the matchers provided by hamcrest, greatly enriches the features of the java switch. That allows to implement factory and strategy pattern in a readable and almost declarative way.

For example let's suppose you need a strategy that switchs among three sorting algorithms based on some characteristic of the list to be sorted. In particular let's assume we have an algorithm specialized for Strings:
public List<String> sortStrings(List<String> list) {
// a sort algorithm suitable for Strings
}
another one that works well with small lists having no more than 100 items:
public List<T> sortSmallList(List<T> list) {
// a sort algorithm suitable for no more than 100 items
}
and a more general purpose one:
public List<String> sort(List<String> list) {
// a generic sort algorithm
}
Given these 3 sorting methods it is possible to create a strategy that chooses the most suitable of them in the following declarative way:
Switcher<List<T>> sortStrategy = new Switcher<List<T>>()
.addCase(having(on(List.class).get(0), instanceOf(String.class))),
new Closure() {{ of(this).sortStrings(var(List.class)); }})
.addCase(having(on(List.class).size(), lessThan(100))),
new Closure() {{ of(this).sortSmallList(var(List.class)); }})
.setDefault(new Closure() {{ of(this).sort(var(List.class)); }});
In this way it is possible to sort a list using the best available algorithm by invoking the Switcher as it follows:
List<T> sortedList = sortStrategy.exec(list, list);
In this case, the list must be passed twice to the Switcher: once to decide, through the hamcrest Matchers, which sorting method should be used and once to be passed to the closure that will actually do the sort.

The release 2.2 of lambdaj also allows to define blocks that invoke constructors. By using this last feature, it is easy to define a Switcher implementing a factory pattern, that creates different objects based on different conditions once again defined through hamcrest matchers.
Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • What Are the Different Types of API Testing?
  • OpenVPN With Radius and Multi-Factor Authentication
  • gRPC on the Client Side

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: