0

我有这两种几乎相同的方法。唯一的区别是方法签名。最后一个参数可以是一个对象,也可以是一个对象列表。我觉得有一种方法可以将这些结合起来,但我不知道如何。

public Query where(String selection, String operator, Object value) {


        WhereClause clause = null;

        try {

            if (!columns.contains(selection)) {
                throw new Exception("No such field for the " + model.getClass().getSimpleName() + " model: " + selection);
            }

            clause = new WhereClause(selection, operator, value);

        } catch (Exception e) {
            clause = null;
            e.printStackTrace();
        }

        if (clause != null) {
            clauses.add(clause);
        }

        return this;
    }

    public Query where(String selection, String operator, List<Object> values) {

        WhereClause clause = null;

        try {

            if (!columns.contains(selection)) {
                throw new Exception("No such field for the " + model.getClass().getSimpleName() + " model: " + selection);
            }

            clause = new WhereClause(selection, operator, values);

        } catch (Exception e) {
            clause = null;
            e.printStackTrace();
        }

        if (clause != null) {
            clauses.add(clause);
        }

        return this;
    }
4

2 回答 2

2
public Query where(String selection, String operator, Object value) 
{
    ArrayList<Object> list = new ArrayList<Object>();
    list.add( value );
    return where(selection, operator, list);
}

编辑:

如果您错过了 Eric 的评论,您还可以使用:

List<Object> list = Collections.singletonList(value)
return where(selection, operator, list);
于 2013-05-29T03:28:37.090 回答
1

您可以将第一个方法的最后一个参数包装在单例列表中(如 Eric Jablow 所建议的那样),然后让方法 2 完成所有工作:

public Query where(String selection, String operator, Object value)
{
    List<Object> v = Collections.singletonList(value);

    return where(selection, operator, v);
}
于 2013-05-29T03:31:11.563 回答