Java tutorial
/* * Copyright 2014 Ronald Hoffman. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.ScripterRon.BitcoinMonitor; import org.json.simple.JSONAware; import org.json.simple.JSONObject; import java.util.Collections; import java.util.List; /** * Response is used for the JSON-encoded responses returned by the Bitcoin node */ public class Response extends JSONObject { /** Empty string list */ private static final List<String> emptyStringList = Collections.emptyList(); /** Empty long list */ private static final List<Long> emptyLongList = Collections.emptyList(); /** Empty object list */ private static final List<Response> emptyObjectList = Collections.emptyList(); /** * Create the response object */ public Response() { super(); } /** * Return a boolean value * * @param key JSON key * @return Boolean value (FALSE if key not found) */ public boolean getBoolean(String key) { Object value = get(key); return (value != null && (value instanceof Boolean) ? (Boolean) value : false); } /** * Return a byte value * * @param key JSON key * @return Byte value (0 if key not found) */ public byte getByte(String key) { Object value = get(key); return (value != null && (value instanceof Long) ? (byte) ((Long) value).intValue() : 0); } /** * Return a short value * * @param key JSON key * @return Short value (0 if key not found) */ public short getShort(String key) { Object value = get(key); return (value != null && (value instanceof Long) ? (short) ((Long) value).intValue() : 0); } /** * Return an integer value * * @param key JSON key * @return Integer value (0 if key not found) */ public int getInt(String key) { Object value = get(key); return (value != null && (value instanceof Long) ? ((Long) value).intValue() : 0); } /** * Return a long value * * @param key JSON key * @return Long value (0 if key not found) */ public long getLong(String key) { Object value = get(key); return (value != null && (value instanceof Long) ? (Long) value : 0); } /** * Return a list of long values * * @param key JSON key * @return List of long values (empty list if key not found) */ public List<Long> getLongList(String key) { Object value = get(key); return (value != null && (value instanceof List) && !((List) value).isEmpty() && (((List) value).get(0) instanceof Long) ? (List<Long>) value : emptyLongList); } /** * Return a double value * * @param key JSON key * @return Double value (zero if key not found) */ public double getDouble(String key) { Object value = get(key); return (value != null && (value instanceof Double) ? (Double) value : 0.0); } /** * Return a string value * * @param key JSON key * @return String value (empty string if key not found) */ public String getString(String key) { Object value = get(key); return (value != null && (value instanceof String) ? (String) value : ""); } /** * Return a hexadecimal byte value * * @param key JSON key * @return Hexadecimal byte array (null if key not found) * @throws NumberFormatException Invalid hexadecimal string */ public byte[] getHexString(String key) { Object value = get(key); return (value != null && (value instanceof String) ? Utils.parseHexString((String) value) : null); } /** * Return a string list value * * @param key JSON key * @return String list (empty list if key not found) */ public List<String> getStringList(String key) { Object value = get(key); return (value != null && (value instanceof List) && !((List) value).isEmpty() && (((List) value).get(0) instanceof String) ? (List<String>) value : emptyStringList); } /** * Return a structured object * * @param key JSON key * @return Object or null if key not found */ public JSONAware getObject(String key) { Object value = get(key); return (value != null && (value instanceof JSONAware) ? (JSONAware) value : null); } /** * Return a list of structured objects * * @param key JSON key * @return Object list (empty list if key not found) */ public List<Response> getObjectList(String key) { Object value = get(key); return (value != null && (value instanceof List) && !((List) value).isEmpty() && (((List) value).get(0) instanceof Response) ? (List<Response>) value : emptyObjectList); } }