Skip to content

Instantly share code, notes, and snippets.

@mokamoto
Last active August 29, 2015 13:57
Show Gist options
  • Save mokamoto/9909259 to your computer and use it in GitHub Desktop.
Save mokamoto/9909259 to your computer and use it in GitHub Desktop.
public class Governor1{
private static final integer ONE = 1;
private static final String ONE_MESSAGE = 'ええぃ!セールスフォースのガバナは化け物か!';
private Governor1(){}
public static void check(){
Governor1.check(false);
}
private static string buildMessage(string limitTitle,Integer currentUsage,Integer maxLimit,boolean onemode){
String message =limitTitle +' - 現在:' + currentUsage + ' 最大:'+ maxLimit;
if(onemode && currentUsage > maxLimit){
message += ' Governor1:' + ONE_MESSAGE;
}
return message;
}
public static void check(boolean onemode){
List<String> messageList = new List<String>();
messageList.add(buildMessage('集計クエリ数',Limits.getAggregateQueries(),onemode ? ONE : Limits.getLimitAggregateQueries(),onemode));
messageList.add(buildMessage('コールアウト数',Limits.getCallouts(),onemode ? ONE : Limits.getLimitCallouts(),onemode));
messageList.add(buildMessage('子リレーション数',Limits.getChildRelationshipsDescribes(),onemode ? ONE : Limits.getLimitChildRelationshipsDescribes(),onemode));
messageList.add(buildMessage('CPU時間数',Limits.getCpuTime(),onemode ? ONE : Limits.getLimitCpuTime(),onemode));
messageList.add(buildMessage('DML実行行数',Limits.getDMLRows(),onemode ? ONE : Limits.getLimitDMLRows(),onemode));
messageList.add(buildMessage('DMLステートメント数',Limits.getDMLStatements(),onemode ? ONE : Limits.getLimitDMLStatements(),onemode));
messageList.add(buildMessage('メール送信数',Limits.getEmailInvocations(),onemode ? ONE : Limits.getLimitEmailInvocations(),onemode));
messageList.add(buildMessage('項目ディスクライブ数',Limits.getFieldsDescribes(),onemode ? ONE : Limits.getLimitFieldsDescribes(),onemode));
messageList.add(buildMessage('項目セットディスクライブ数',Limits.getFieldSetsDescribes(),onemode ? ONE : Limits.getLimitFieldSetsDescribes(),onemode));
messageList.add(buildMessage('非同期コール数',Limits.getFutureCalls(),onemode ? ONE : Limits.getLimitFutureCalls(),onemode));
messageList.add(buildMessage('ヒープサイズ数',Limits.getHeapSize(),onemode ? ONE : Limits.getLimitHeapSize(),onemode));
messageList.add(buildMessage('SOQLクエリ数',Limits.getQueries(),onemode ? ONE : Limits.getLimitQueries(),onemode));
messageList.add(buildMessage('選択リスト数',Limits.getPicklistDescribes(),onemode ? ONE : Limits.getLimitPicklistDescribes(),onemode));
messageList.add(buildMessage('クエリロケーター行数',Limits.getQueryLocatorRows(),onemode ? ONE : Limits.getLimitQueryLocatorRows(),onemode));
messageList.add(buildMessage('SOQLクエリ行数',Limits.getQueryRows(),onemode ? ONE : Limits.getLimitQueryRows(),onemode));
messageList.add(buildMessage('レコードタイプディスクドライブ数',Limits.getRecordTypesDescribes(),onemode ? ONE : Limits.getLimitRecordTypesDescribes(),onemode));
messageList.add(buildMessage('実行コード行数',Limits.getScriptStatements(),onemode ? ONE : Limits.getLimitScriptStatements(),onemode));
messageList.add(buildMessage('SOSLクエリ実行数',Limits.getSoslQueries(),onemode ? ONE : Limits.getLimitSoslQueries(),onemode));
String errorMessage;
Integer errorCount = 0;
for(String message : messageList){
System.debug(message);
if(oneMode && message.endsWith(ONE_MESSAGE)){
if(errorCount !=0){
errorMessage += ',';
}
errorMessage += message.removeEnd(ONE_MESSAGE);
errorCount++;
}
}
if(onemode){
if(errorCount ==0){
return;
}
if(errorCount == 2){
errorMessage = 'おい…、見てるか?谷沢、お前を超えるガバナが、ここにいるのだ。しかも、2つも同時にだ、谷沢… :' + errorMessage;
}
throw new Governer1Exception(errorMessage);
}
}
public class Governer1Exception extends Exception{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment