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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Power of Guava Switcher

Upender Chinthala user avatar by
Upender Chinthala
·
May. 06, 14 · · Code Snippet
Like (0)
Save
Tweet
621 Views

Join the DZone community and get the full member experience.

Join For Free

In Java Switch has limitation i.e you can check case values integer , character, or string, etc.but you can't combine all these cases in single switch case. If you try to combine then it will show compilation error. So overcome this problem using Google Lamdaj Switcher class .  You case stick with one type value comparison or multiple type value comparison. The first example show to use multiple type cases and the second example show regular scenario to get salary based on job type. 

//Example-1
public class SwitcherDemo
{
	public static void main(String args[])
	{
		Switcher generic = new Switcher()
				.addCase(1, "Integer")
				.addCase(1.1, "Double")
				.addCase(1.1f,"Float")
				.addCase("S","String")
				.setDefault("Others");
		System.out.println("Switch type "+generic.exec(1.1f));
		System.out.println("Switch type "+generic.exec("S"));
		System.out.println("Switch type "+generic.exec(1));
		System.out.println("Switch type "+generic.exec(1.1));
		System.out.println("Switch type "+generic.exec("SS"));
	}
}

//Example-2
import ch.lambdaj.function.closure.Switcher;

import java.util.Arrays;
import java.util.List;

public class SwitcherDemo {



	public static void main(String args[])
	{
		Employee fulltime = new Employee(JOB_TYPE.FULL_TIME);
		Employee partime = new Employee(JOB_TYPE.PART_TIME);
		Employee contract = new Employee(JOB_TYPE.CONTRACT);
		Employee intern = new Employee(JOB_TYPE.INTERN);
		List  employeeList = Arrays.asList(fulltime,partime,contract,intern);

		Switcher salarySwitcher = new Switcher();
		salarySwitcher.addCase(JOB_TYPE.FULL_TIME, new Salary(100000));
		salarySwitcher.addCase(JOB_TYPE.PART_TIME, new Salary(50000));
		salarySwitcher.addCase(JOB_TYPE.CONTRACT, new Salary(120000));
		salarySwitcher.addCase(JOB_TYPE.INTERN, new Salary(45000));
		salarySwitcher.setDefault(new Salary(0));

		for(Employee e: employeeList)
		{
                     Salary sal = salarySwitcher.exec(e.getJobtype());
			e.setSalary(sal);
			System.out.println("Salary "+e.getSalary().getSalary()+" Offered Job type "+e.getJobtype());
		}

	}
}
enum JOB_TYPE
{
	FULL_TIME,CONTRACT,INTERN,PART_TIME
}

class Employee
{

	private final JOB_TYPE jobtype;
	private Salary salary;

	Employee(JOB_TYPE job_type)
	{
		this.jobtype=job_type;
	}
	public JOB_TYPE getJobtype()
	{
		return jobtype;
	}

	Salary getSalary()
	{
		return salary;
	}

	void setSalary(Salary salary)
	{
		this.salary = salary;
	}
}
class Salary
{
	private final double sal;

	public Salary(double sal)
	{
		this.sal=sal;
	}
	public double getSalary()
	{
		return sal;
	}
}
Google Guava Comparison (grammar) Data Types career Java (programming language) Strings Google (verb)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • Use Lambda Function URL To Write a Serverless App Backed by DynamoDB
  • Why Is Software Integration Important for Business?
  • How Database B-Tree Indexing Works

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo