重构强制map.get java代码

时间:2013-09-18 15:17:29

标签: java

请关于如何重构这个java代码的任何想法,所以我不必重复这些代码块3次(或更多次)?

Integer id = null;
try {
    id = (Integer)extradata.get("id");
}
catch(Exception e) {
    logger.error(e);
}

if (id == null) {
    logger.error("no id set");
    task.setStatus(Status.ERROR);
    DBService.updateTaskStatus(conn, false, task);
    conn.commit();
    return;
}

String name = null;
try {
    name = (String)extradata.get("name");
}
catch(Exception e) {
    logger.error(e);
}

if (name == null) {
    logger.error("no name set");
    task.setStatus(Status.ERROR);
    DBService.updateTaskStatus(conn, false, task);
    conn.commit();
    return;
}

String city = null;
try {
    city = (String)extradata.get("city");
}
catch(Exception e) {
    logger.error(e);
}

if (city == null) {
    logger.error("no city set");
    task.setStatus(Status.ERROR);
    DBService.updateTaskStatus(conn, false, task);
    conn.commit();
    return;
}

6 个答案:

答案 0 :(得分:3)

您可以使用静态通用方法:

private static final <T> T getValue(Map<String, Object> extradata, String key, Class<T> clazz) {
    Object val = extradata.get(key);
    if (val == null) {
        handleNullVariable("name");
        return null;
    }        
    return clazz.cast(val);
}

然后你可以用:

来调用它
Integer id = getValue(extradata, "id", Integer.class);
String name = getValue(extradata, "name", String.class);

答案 1 :(得分:1)

您可以定义检查null字符串的方法,并更新状态,如下所示:

private static boolean isValid(
    Object obj
,   String msg
,   Connection conn
,   Task task) {
    if (obj != null) return true;
    logger.error(msg);
    task.setStatus(Status.ERROR);
    DBService.updateTaskStatus(conn, false, task);
    conn.commit();
    return false;
}

现在您可以更改主要方法以反复调用checkValidString

Integer id = null;
try {
    id = (Integer)extradata.get("id");
} catch(Exception e) {
    logger.error(e);
}
if (!isValid(id, "no id set", conn, task)) return;

String name = null;
try {
    name = (String)extradata.get("name");
} catch(Exception e) {
    logger.error(e);
}
if (!isValid(name, "no name set", conn, task)) return;

String city = null;
try {
    city = (String)extradata.get("city");
}
catch(Exception e) {
    logger.error(e);
}
if (!isValid(city, "no city set", conn, task)) return;

答案 2 :(得分:1)

Integer id = null;
String name = null;
String city = null;

try {
    id = (Integer)extradata.get("id");
    name = (String)extradata.get("name");
    city = (String)extradata.get("city");
catch(Exception e) {
    logger.error(e);
}

if (id == null || name == null || city == null) {
    String msg = (id == null ? "id" : name == null ? "name" : "city");
    logger.error("no "+msg+" set");
    task.setStatus(Status.ERROR);
    DBService.updateTaskStatus(conn, false, task);
    conn.commit();
    return;
}

答案 3 :(得分:0)

试试这个:

    Integer id = null;
    try {
        id = (Integer)extradata.get("id");
    }
    catch(Exception e) {
        logger.error(e);
    }

    if (id == null) {
        handleNullVariable("id");
        return;
    }

    String name = null;
    try {
        name = (String)extradata.get("name");
    }
    catch(Exception e) {
        logger.error(e);
    }

    if (name == null) {
        handleNullVariable("name");
        return;
    }

    String city = null;
    try {
        city = (String)extradata.get("city");
    }
    catch(Exception e) {
        logger.error(e);
    }

    if (city == null) {
        handleNullVariable("city");
        return;
    }

    void handleNullVariable(String variableName)
    {
    logger.error("no "+ variableName + " set");
     task.setStatus(Status.ERROR);
        DBService.updateTaskStatus(conn, false, task);
        conn.commit();
        }

答案 4 :(得分:0)

class NotFoundException extends Exception {
}
...
Object getValue(String name) {
    Object res;
    try {
        res=extradata.get(name);
    } catch(Exception e) {
        logger.error(e);
    }
    if (res == null) {
        logger.error("no "+name+" set");
        task.setStatus(Status.ERROR);
        DBService.updateTaskStatus(conn, false, task);
        conn.commit();
        throw new NotFoundException();
    }
}
...
try {
    Integer id = (Integer)getValue("id");
    String name  = (String )getValue("name");
    String city  = (String )getValue("city");
 } catch(Exception e) {
    return;
 }

答案 5 :(得分:0)

public static <E> E getData(String key)
{
    E ret = null;
    try {
        ret = (E)extradata.get(key);
    }
    catch(Exception e) {
        logger.error(e);
    }

    if (ret == null) {
        throw new Exception(key);
    }
    return ret;
}

调用它
try
{
    Integer id = getData("id");
    String name = getData("name");
    String city = getData("city");
}
catch(Exception e)
{
    logger.error("no " + e.getMessagE() + " set");
    task.setStatus(Status.ERROR);
    DBService.updateTaskStatus(conn, false, task);
    conn.commit();
    return;
}