2014-03-02 25 views
11

而是與此基本結構的IF/THEN/ELSEIF/ELSESalesforce中的APEX支持SWITCH和CASE語句嗎?

int month = 8; 
String monthString; 
if (month == 1) { 
    monthString = "January"; 
} else if (month == 2) { 
    monthString = "February"; 
} 
... // and so on 

這將是不錯的

int month = 8; 
    String monthString; 
    switch (month) { 
     case 1: monthString = "January"; 
       break; 
     case 2: monthString = "February"; 
       break; 

     ..... // and so on 

     case 12: monthString = "December"; 
       break; 
     default: monthString = "Invalid month"; 
       break; 
    } 

這將增加可讀性,更容易調試,由於意圖清晰。

回答

13

不在這個時候傷心。自2009年以來,我一直在等待這個功能,並且它是來自下面社區的鏈接高度要求的功能。

Add "Switch" or "Case" Statement to Apex

+2

在我看來 - 不會發生的。 'Case'已經是一個關鍵字(標準對象之一)。我很遺憾那些必須編寫一個編譯器才能找出正確用法的傻瓜:P – eyescream

+0

@eyescream - 就此而言,「case」仍然有星號,表示它保留供將來使用,儘管含糊不清案例sObject會恕我直言太混亂 - 以下是[文檔]的鏈接(https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_reserved_words.htm) – HungryBeagle

+0

@eyescream,'Schema.Case' –

9
monthMap = new Map<Integer,String>(); 
    monthMap.put(1,'JAN'); 
    monthMap.put(2,'FEB'); 
    monthMap.put(3,'MAR'); 
    monthMap.put(4,'APR'); 
    monthMap.put(5,'MAY'); 
    monthMap.put(6,'JUN'); 
    monthMap.put(7,'JUL'); 
    monthMap.put(8,'AUG'); 
    monthMap.put(9,'SEP'); 
    monthMap.put(10,'OCT'); 
    monthMap.put(11,'NOV'); 
    monthMap.put(12,'DEC'); 

然後做一個根據你整月的數值得到。

沒有必要寫一個大的if-else。

+4

+1但是'new Map {1 =>'JAN',2 =''FEB',3 =>'MAR'};' - 浪費較少的腳本語句並且不會將調試日誌作爲垃圾郵件發送許多。 – eyescream

3

我已將我的意見添加到其他答案。

雖然這一個並沒有真正回答這個問題,我仍然認爲把它扔在這裏是一個好主意。我討厭看到這樣的「自制的日期圖書館」 ......

DateTime someDate = System.now(); 
System.debug(someDate.format('MMM')); // Jan, Feb etc. 
System.debug(someDate.format('MMMM')); // January, February etc. 

這將永遠是英語,即使當前用戶的語言偏好是不同的。格式化字符串傳遞給內部Java方法,所以只需快速查看http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

2

同時,SFDC提供本地引擎,因爲解決方法是使用小型實用程序「框架」作爲開關事件面向對象的「語句」:

Switch-Case Utility

實例:

public with sharing class SwitchCaseExample { 

    public String result {get; set;} 

    public static final String MSG_FROM_ACTION_1 = 'invoke action 1'; 
    public static final String MSG_FROM_ACTION_2 = 'invoke action 2'; 
    public static final String MSG_FROM_ACTION_3 = 'invoke action 3'; 
    public static final String MSG_FROM_ACTION_4 = 'invoke action 4'; 

    public void testSwitchCase(String value) { 

     SwitchCaseHelper sch = new SwitchCaseHelper(); 

     sch.switch(value) 
      .case('value1', new Action1(this), SwitchCaseHelper.PUT_BREAK) 
      .case('value2', new Action2(this), SwitchCaseHelper.PUT_CONTINUE) 
      .case('value3', new Action3(this), SwitchCaseHelper.PUT_BREAK) 
      .default(new Action4(this)); 
    } 


    private class Action1 implements ActionContainer { 

     private SwitchCaseExample outerCtx; 

     public Action1(SwitchCaseExample outerCtx) { 

      this.outerCtx = outerCtx; 
     } 

     public String doAction() { 

      outerCtx.result = MSG_FROM_ACTION_1; 
      return null; 
     } 
    } 

    private class Action2 implements ActionContainer { 

     private SwitchCaseExample outerCtx; 

     public Action2(SwitchCaseExample outerCtx) { 

      this.outerCtx = outerCtx; 
     } 

     public String doAction() { 

      outerCtx.result = MSG_FROM_ACTION_2; 
      return null; 
     } 
    } 

    private class Action3 implements ActionContainer { 

     private SwitchCaseExample outerCtx; 

     public Action3(SwitchCaseExample outerCtx) { 

      this.outerCtx = outerCtx; 
     } 

     public String doAction() { 

      outerCtx.result = MSG_FROM_ACTION_3; 
      return null; 
     } 
    } 

    private class Action4 implements ActionContainer { 

     private SwitchCaseExample outerCtx; 

     public Action4(SwitchCaseExample outerCtx) { 

      this.outerCtx = outerCtx; 
     } 

     public String doAction() { 

      outerCtx.result = MSG_FROM_ACTION_4; 
      return null; 
     } 
    } 

} 

接口:

public interface ActionContainer { 

    String doAction(); 

} 

和開關的情況下邏輯實現

public with sharing class SwitchCaseHelper { 

    public static final Boolean PUT_BREAK = true; 
    public static final Boolean PUT_CONTINUE = false; 

    public class SwitchCaseException extends Exception {} 

    public static final String EXCEPTION_MESSAGE = 'Switch-Case construction must have one (and only one) "switch" statement'; 

    @TestVisible 
    private Object switchOperand; 

    @TestVisible 
    private Boolean isCaseAfterBreakStatement; 

    @TestVisible 
    private Boolean isPreviousSwitch; 

    public SwitchCaseHelper() { 

     isCaseAfterBreakStatement = false; 
    } 

    public SwitchCaseHelper switch(Object switchOperand) { 

     if (isPreviousSwitch != null) { 
      throw new SwitchCaseException(EXCEPTION_MESSAGE); 
     } 
     isPreviousSwitch = true; 
     this.switchOperand = switchOperand; 
     return this; 
    } 

    public SwitchCaseHelper case(Object caseOperand, ActionContainer container, Boolean hasBreak) { 

     if (isPreviousSwitch == null) { 
      throw new SwitchCaseException(EXCEPTION_MESSAGE); 
     } 

     if (isPreviousSwitch) { 
      isPreviousSwitch = false; 
     } 

     if (isCaseAfterBreakStatement) { 
      return this; 
     } 

     if (switchOperand.equals(caseOperand)) { 
      container.doAction(); 
      isCaseAfterBreakStatement = hasBreak; 
     } 

     return this; 
    } 

    public SwitchCaseHelper default(ActionContainer container) { 

     if (isPreviousSwitch == null) { 
      throw new SwitchCaseException(EXCEPTION_MESSAGE); 
     } 

     if (!isCaseAfterBreakStatement) { 
      container.doAction(); 
     } 
     return this; 
    } 
}