I am struggling to find out a answer,but I failed.so I do this
Enum MyEnum {
A, B, C;
}
public void doSomething(MyEnum enum, List<Object> lists) {
//do something
}
the element type in list is relation to enum, for example:
Object must be Long when enum is A
Object must be String when enum is B...
if the method second argument type is List, call this method will throw error in runtime if the type is not correct!
what can I do to ensure the type is correct, that is, the compiler will reject compiling when calling the method with wrong type argument?
AccountType {
PHONE, EMAIL, USER_ID;
}
List<Object> getRecords(AccountType accountType, List<Object> accounts) {
//phoneService, emailService, userIdService are thrift service
//the method let client
switch(accountType) {
case PHONE:
//method signature is List<PhoneRecord> getPhoneRecords(List<String>)
return phoneService.getPhoneRecords(transString(accounts));
break;
case EMAIL:
//method signature is List<EmailRecord> getEmailRecords(List<String>)
return emailService.getEmailRecords(transString(accounts));
break;
case USER_ID:
//method signature is List<UserIdRecord> getUserIdRecords(List<Long>)
return userIdService.getUserIdRecords(transLong(accounts));
break;
default:
//...
}
}
my project is full of method handling AccountType, such as punish(),selectLogs(),free()...If I wrote method for each AccountType,then would like: punishPhone(String), punishEmail(String), punishUserId(Long), this is too bad:too many methods and when AccountType add or delete a type,I will code hardly.
Sorry for my bad English.I can`t tell more detail or what i really think:C,Wish you could understand