package de.mrbesen.telegram.objects; import org.json.JSONObject; public class JSONBased { protected JSONObject base_json = null; protected Member[] allowedmembers = {}; public JSONBased() {} public JSONBased(JSONObject json) { base_json = json; } public boolean has(Member mem) { if(base_json == null) return false; if(!isAllowed(mem)) throw new UnallowedMemberException(mem, this); return base_json.has(mem.name()); } public T get(Member mem) { if(base_json == null) return null; return (T) base_json.get(mem.name()); } public boolean isAllowed(Member mem) { for(Member allowedmem : allowedmembers) { if(allowedmem == mem) { return true; } } return false; } public Member[] getAllowed() { return allowedmembers; } public enum Member { // ===== MESSAGE ==== chat,//Chat forward_from_message_id,//int forward_signature,//String forward_date,//long reply_to_message,//Message edit_date,//long media_group_id,//String author_signature, //String audio, //Audio document, //Document game, //Game photo, //Array of PhotoSize sticker, //Sticker video, //Video voice, //voice video_note, //videoNote caption, // String contact, //Contact location, //Location venue, //Venue new_chat_members, // Array of TUser left_chat_members, // Array of TUser new_chat_title, //String new_chat_photo, //Array of Photosize delete_chat_photo, // true group_chat_created, // true supergroup_chat_created, // true channel_chat_created, //true migrate_to_chat_id, // int migrate_from_chat_id, // int pinned_message, //TMessage invoice, //Invoice successful_payment, //SuccessfulPayment connected_website, // String //==== END MESSAGE ==== //==== AUDIO ====== performer, //String title,//String mime_type, // String - also used for Documents, PhotoSize & video file_size, // int - also used for Documents, File & video //===== END AUDIO ==== //=====Document===== thumb, //PhotoSize - also used in Video file_name,//String //=====END Document===== //===== VENUE ==== foursquare_id, //String //===== END VENUE ==== //===== FILE ===== file_path, // String //===== END FILE ===== } protected class UnallowedMemberException extends RuntimeException { private static final long serialVersionUID = -253424139568749269L; public UnallowedMemberException(Member mem, JSONBased b) { super(b.getClass().getSimpleName() + " does not allow member " + mem.name()); } } }