Use enum for Actions types

This commit is contained in:
Rubenlagus 2016-06-20 21:33:14 +02:00
parent 4fc3cc34cf
commit 046d918574
2 changed files with 78 additions and 4 deletions

View File

@ -0,0 +1,57 @@
package org.telegram.telegrambots.api.methods;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief Types of actions for SendChatAction method.
* @date 20 of June of 2016
*/
public enum ActionType {
TYPING("typing"),
RECORDVIDEO("record_video"),
RECORDAUDIO("record_audio"),
UPLOADPHOTO("upload_photo"),
UPLOADVIDEO("upload_video"),
UPLOADAUDIO("upload_audio"),
UPLOADDOCUMENT("upload_document"),
FINDLOCATION("find_location");
private String text;
ActionType(String text) {
this.text = text;
}
/**
* @deprecated Added for backward compatibility, will be dropped in next mayor release
* @param text text of the action
* @return ActionType
*/
@Deprecated
public static ActionType GetActionType(String text) throws IllegalArgumentException {
switch (text) {
case "typing":
return TYPING;
case "record_video":
return RECORDVIDEO;
case "record_audio":
return RECORDAUDIO;
case "upload_photo":
return UPLOADPHOTO;
case "upload_video":
return UPLOADVIDEO;
case "upload_audio":
return UPLOADAUDIO;
case "upload_document":
return UPLOADDOCUMENT;
case "find_location":
return FINDLOCATION;
}
throw new IllegalArgumentException(text + " doesn't match any know ActionType");
}
@Override
public String toString() {
return text;
}
}

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject; import org.json.JSONObject;
import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.ActionType;
import org.telegram.telegrambots.api.methods.BotApiMethod; import org.telegram.telegrambots.api.methods.BotApiMethod;
import java.io.IOException; import java.io.IOException;
@ -31,7 +32,7 @@ public class SendChatAction extends BotApiMethod<Boolean> {
* videos 'record_audio' or 'upload_audio' for audio files 'upload_document' for general files, * videos 'record_audio' or 'upload_audio' for audio files 'upload_document' for general files,
* 'find_location' for location data. * 'find_location' for location data.
*/ */
private String action; private ActionType action;
public String getChatId() { public String getChatId() {
return chatId; return chatId;
@ -42,12 +43,28 @@ public class SendChatAction extends BotApiMethod<Boolean> {
return this; return this;
} }
/**
* @deprecated
* @return Action type text
*/
@Deprecated
public String getAction() { public String getAction() {
return action; return action.toString();
} }
public SendChatAction setAction(String action) { public void setAction(ActionType action) {
this.action = action; this.action = action;
}
/**
* @deprecated Use {@link #setAction(ActionType)} instead
* @param action Text of the action to create
* @return Reference to this same instance
* @throws IllegalArgumentException if action is not valid
*/
@Deprecated
public SendChatAction setAction(String action) throws IllegalArgumentException {
this.action = ActionType.GetActionType(action);
return this; return this;
} }
@ -76,7 +93,7 @@ public class SendChatAction extends BotApiMethod<Boolean> {
public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException { public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartObject(); gen.writeStartObject();
gen.writeStringField(CHATID_FIELD, chatId); gen.writeStringField(CHATID_FIELD, chatId);
gen.writeStringField(ACTION_FIELD, action); gen.writeStringField(ACTION_FIELD, action.toString());
gen.writeEndObject(); gen.writeEndObject();
} }