@SuppressWarnings("unchecked")
    public <T> T getVariable(String name, T type) throws InvalidParameterException, TesseractException {
        requireState(State.INITIALIZED);
        
        int rv = 0;
        if (type instanceof Integer) {
            IntBuffer value = IntBuffer.allocate(1);
            rv = api.TessBaseAPIGetIntVariable(handle, name, value);
            checkGetVariableReturnValue(rv, name);
            return (T)new Integer(value.get());
        } else if (type instanceof Boolean) {
            IntBuffer value = IntBuffer.allocate(1);
            rv = api.TessBaseAPIGetBoolVariable(handle, name, value);
            checkGetVariableReturnValue(rv, name);
            
            return (T)Boolean.valueOf(toBoolean(value.get()));
        } else if (type instanceof Double) {
            DoubleBuffer value = DoubleBuffer.allocate(1);
            rv = api.TessBaseAPIGetDoubleVariable(handle, name, value);
            checkGetVariableReturnValue(rv, name);
            
            return (T)new Double(value.get());
        } else if (type instanceof String) {
            String result = api.TessBaseAPIGetStringVariable(handle, name);
            if (result == null) {
                Map<String, String> map = Collections.singletonMap(name, "");
                throw new InvalidParameterException("Could not retrieve the requested parameter", map);
            }
            
            return (T)result;
        } else {
            Map<String, String> map = Collections.singletonMap(name, "");
            throw new InvalidParameterException(
                    "Could not get variable. Unsupported type: " + type.getClass().getName() + 
                    ".  Excepted Integer, Double, Boolean, or String", map);
        }
    }