简体   繁体   English

Java 中带有 Document 和 BasicDBObject 的通用方法

[英]Generic Method with Document and BasicDBObject in Java

I'm learning generics and trying to write generic method in a project that I'm working on.我正在学习 generics 并尝试在我正在处理的项目中编写通用方法。 I have a use case where I have a method which is recursive, which is exactly same but the parameters type are either Document or BasicDBObject.我有一个用例,我有一个递归方法,它完全相同,但参数类型是 Document 或 BasicDBObject。 These parameters have nested list of objects so each of them again have objects of type Document or BasicDBObject inside them.这些参数具有嵌套的对象列表,因此它们中的每一个都再次具有 Document 或 BasicDBObject 类型的对象。 So I have overloaded the methods with both the types.所以我用这两种类型重载了方法。 Can I use generics to change this method to accept any type?我可以使用 generics 更改此方法以接受任何类型吗?

public <T> String convertToRules(T rulesObj) {
    final StringBuilder expressionBuilder = new StringBuilder("${");
    if (rulesObj.getString("condition") != null) {
        String condition = rulesObj.getString("condition");
        if (rulesObj.get("rules") != null) {
           ArrayList<BasicDBObject> rules = (ArrayList<BasicDBObject>) rulesObj.get("rules");
           // rules can be ArrayList<BasicDBObject> or ArrayList<Document> so just mentioning them 
           // both here in question to avoid confusion of what type they are.
           ArrayList<Document> rules = (ArrayList<Document>) rulesObj.get("rules");
           rules.forEach(rule -> {
            // Some code
            expressionBuilder.append(convertToRules(rule));
       }
    }
 }

There are compiler errors in the above code as I was trying to convert the existing method to generic method.当我试图将现有方法转换为泛型方法时,上述代码中存在编译器错误。 Below are the actual methods.下面是实际的方法。

public String convertToRules(BasicDBObject rulesObj) {
    final StringBuilder expressionBuilder = new StringBuilder("${");
    if (rulesObj.getString("condition") != null) {
        String condition = rulesObj.getString("condition");
        if (rulesObj.get("rules") != null) {
           ArrayList<BasicDBObject> rules = (ArrayList<BasicDBObject>) rulesObj.get("rules");
           rules.forEach(rule -> {
            // Some code
            expressionBuilder.append(convertToRules(rule));
       }
    }
 }


public String convertToRules(Document rulesObj) {
    final StringBuilder expressionBuilder = new StringBuilder("${");
    if (rulesObj.getString("condition") != null) {
        String condition = rulesObj.getString("condition");
        if (rulesObj.get("rules") != null) {
           ArrayList<Document> rules = (ArrayList<Document>) rulesObj.get("rules");
           rules.forEach(rule -> {
            // Some code
            expressionBuilder.append(convertToRules(rule));
       }
    }
 }

I'm pretty sure this is not the right way to do it, need some guidance on how this can be achieved.我很确定这不是正确的方法,需要一些关于如何实现这一点的指导。 I want to know if I'm thinking the right way about generics or this implementation is wrong.我想知道我对 generics 的想法是否正确,或者这个实现是错误的。

You can make an abstract superclass or interface for both of the class.您可以为 class 创建一个抽象超类或接口。 Let's say SuperClass with the common methods like getString() of the BasicDBObject and Document.假设 SuperClass 具有 BasicDBObject 和 Document 的 getString() 等常用方法。 Then, override the methods in the two class.然后,覆盖两个class中的方法。 Then for the generic method, you can make it like <?那么对于泛型方法,你可以让它像 <? extends SuperClass.扩展超类。 Then you can read the property of SuperClass easily.然后你可以很容易地阅读 SuperClass 的属性。 I think it should work fine.我认为它应该可以正常工作。

RuleType - Contract of Two objects. RuleType - 两个对象的契约。

import java.util.List;

public interface RuleType<T> {
    String getString(String parameter);
    List<T> get(String param);
}

BasicDBObject基本数据库对象

import java.util.ArrayList;
import java.util.List;

public class BasicDBObject implements RuleType<BasicDBObject> {

    @Override
    public String getString(String parameter) {
        return "Something";
    }

    @Override
    public List<BasicDBObject> get(String param) {
        return new ArrayList<>();
    }
}

Doument文件

import java.util.ArrayList;
import java.util.List;

public class Document implements RuleType<Document> {

    @Override
    public String getString(String parameter) {
        return "Something";
    }

    @Override
    public List<Document> get(String param) {
        return new ArrayList<>();
    }
}

Generic Method (Logic implementation)通用方法(逻辑实现)

public <T extends RuleType> String convertToRules(T rulesObj) {
        final StringBuilder expressionBuilder = new StringBuilder("${");
        if (rulesObj.getString("condition") != null) {
            String condition = rulesObj.getString("condition");
            if (rulesObj.get("rules") != null) {
                List<T> rules = rulesObj.get("rules");
                rules.forEach(rule ->
                    // Some code
                    expressionBuilder.append(convertToRules(rule))
                );
            }
        }
        return expressionBuilder.toString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM