3

请,关于如何重构这个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;
}
4

6 回答 6

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);
于 2013-09-18T15:29:21.217 回答
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;
}

现在您可以更改 main 方法以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;
于 2013-09-18T15:23:58.910 回答
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;
}
于 2013-09-18T15:24:32.453 回答
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;
 }
于 2013-09-18T15:31:39.867 回答
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;
}
于 2013-09-18T15:32:00.020 回答
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();
        }
于 2013-09-18T15:25:13.573 回答