0

解决此问题的最佳方法是什么?我想在运行我的应用程序时第一次调用一个特定的方法,但第二次它应该只在有任何变化时调用。如果没有变化,那么我不想调用该方法。

下面是将从我的主应用程序调用的方法。在下面的方法中,我正在从数据库中检索数据。第一次,从数据库中检索数据后,地图将具有如下内容-

BundleFramework    1.0.0
Bundle-A         1.0.0
Bundle-B         1.0.0

现在,我想分别打印出 BundleFramework 及其版本。然后第一次调用将在地图中包含Bundle-A和Bundle-B及其版本的处理方法。

现在我的后台线程每两秒运行一次,它也会调用getBundlesInformation方法。现在,Bundle-A and Bundle-B如果.Bundle-A or Bundle-Bprocess method

我在下面的代码中几乎就在那里,但目前正在发生的事情是 - 如果 Bundle-A 和 Bundle-B 版本的数据库没有变化,那么它也会使用 Bundle-A 和 Bundle-B 信息调用处理方法。

我想要的是 - 第一次(从主应用程序运行时),它应该使用 Bundle-A 和 Bundle-B 调用 process 方法,下一次它应该只在版本有任何变化时调用 process 方法捆。

public static Map<String, String> frameworkInfo = new LinkedHashMap<String, String>();
public static Map<String, String> bundleList = new LinkedHashMap<String, String>();
public static Map<String, String> newBundleList = new LinkedHashMap<String, String>();
private static Map<String, String> oldBundleList = new LinkedHashMap<String, String>();


private static void getAttributesFromDatabase() {

    Map<String, String> bundleInformation = new LinkedHashMap<String, String>();

    bundleInformation = getFromDatabase();

    if(!bundleInformation.get("BundleFramework").equals(frameworkInfo.get("BundleFramework"))) {
        frameworkInfo.put("BundleFramework", bundleInformation.get("BundleFramework")); 
        String version = frameworkInfo.get("BundleFramework");
        printFrameworkBundle("BundleFramework", version);
    }

    bundleInformation.remove("BundleFramework");

    if(!bundleInformation.isEmpty()) {
        oldBundleList = bundleList;
        bundleList = bundleInformation;
    }

    final Map<String, MapDifference.ValueDifference<String>> entriesDiffering = Maps.difference(oldBundleList, bundleList).entriesDiffering();
    if (!entriesDiffering.isEmpty()) {

        for (String key : entriesDiffering.keySet()) {
            newBundleList.put(key, bundleList.get(key));
            System.out.println("{" + key + "=" + bundleList.get(key) + "}");
        } 

        process(newBundleList);
    }
    process(bundleList);
}

private static Map<String, String> getFromDatabase() {

    Map<String, String> data= new LinkedHashMap<String, String>();

    String version0 = "1.0.0";
    String version1 = "1.0.0";
    String version2 = "1.0.0";

    data.put("BundleFramework", version0);
    data.put("Bundle-A", version1);
    data.put("Bundle-B", version2);

    return data;
}

有人可以帮我吗?我正在使用 Nosql 数据库,所以我不能有触发功能。

更新代码:-

以下是我的完整代码-

public class App {

    public static Map<String, String> frameworkInfo = new LinkedHashMap<String, String>();
    public static Map<String, String> bundleList = new LinkedHashMap<String, String>();
    public static Map<String, String> newBundleList = new LinkedHashMap<String, String>();
    private static Map<String, String> oldBundleList = new LinkedHashMap<String, String>();


    public static void main(String[] args) {

        getAttributesFromDatabase();

        loggingAfterEveryXMilliseconds();

    }


    private static void makeCall(Map<String, String> test) {
        System.out.println(test);
    }


    private static void getAttributesFromDatabase() {

        Map<String, String> bundleInformation = new LinkedHashMap<String, String>();

        bundleInformation = getFromDatabase();

        if(!bundleInformation.get("Framework").equals(frameworkInfo.get("Framework"))) {
            frameworkInfo.put("Framework", bundleInformation.get("Framework")); 
            String version = frameworkInfo.get("Framework");
            printFrameworkBundle("Framework", version);
        }

        bundleInformation.remove("Framework");

        if(!bundleInformation.isEmpty()) {
            oldBundleList = bundleList;
            bundleList = bundleInformation;
        }

        if(oldBundleList != null) {
            final Map<String, MapDifference.ValueDifference<String>> entriesDiffering = Maps.difference(oldBundleList, bundleList).entriesDiffering();

            if (!entriesDiffering.isEmpty()) {          
                for (String key : entriesDiffering.keySet()) {
                    newBundleList.put(key, bundleList.get(key));
                    System.out.println("{" + key + "=" + bundleList.get(key) + "}");
                } 
                makeCall(newBundleList);
            }
        } else {
            makeCall(bundleList);
        }
    }

    private static void printFrameworkBundle(String string, String version) {
        System.out.println(string+" - "+version);       
    }

    private static Map<String, String> getFromDatabase() {

        Map<String, String> hello = new LinkedHashMap<String, String>();

        String version0 = "1.0.0";
        String version1 = "1.0.0";
        String version2 = "1.0.0";

        hello.put("Framework", version0);
        hello.put("BundleA", version1);
        hello.put("BundleB", version2);

        return hello;
    }

    /**
     * This is a simple which will call the logHistogramInfo method after every
     * X Milliseconds
     */
    private static void loggingAfterEveryXMilliseconds() {
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {

                    }
                    getAttributesFromDatabase();
                }
            }
        }.start();
    }
}
4

1 回答 1

1

用于String#equals()字符串比较。!=即使两个字符串在内容上是等效的,不等于也很可能返回 true。因此,将您的if条件更改为

if(!(BundleFrameworkInfo.get("BundleFramework") != null &&
   BundleFrameworkInfo.get("BundleFramework").equals(bundleList.get("BundleFramework"))))

您的其余代码非常混乱。例如,

if(!bundleList.isEmpty()) {
    oldBundleList = bundleList;
    bundleList = bundleList; // what would self-assigning achieve?
}

此外,由于只要bundleList不是空的,您将设置oldBundleListbundleList. 因此,假设Maps.difference()按预期工作,它根本不应该返回任何差异。

编辑:(根据 OP 的更新代码)

getAttributesFromDatabase()更改as的最后一部分

if(!bundleInformation.isEmpty()) {
    oldBundleList = bundleList;
    bundleList = bundleInformation;
}

if(!oldBundleList.isEmpty()) {
    final Map<String, MapDifference.ValueDifference<String>> entriesDiffering =
                  Maps.difference(oldBundleList, bundleList).entriesDiffering();

    if (!entriesDiffering.isEmpty()) {          
        for (String key : entriesDiffering.keySet()) {
            newBundleList.put(key, bundleList.get(key));
            System.out.println("{" + key + "=" + bundleList.get(key) + "}");
        } 
        process(newBundleList);
    }
} else {
    process(bundleList);
}

编辑:(冗余代码)

你有很多冗余/空的Map创作。例如,

// redundant
Map<String, String> bundleInformation = new LinkedHashMap<String, String>();    
bundleInformation = getFromDatabase();

// initialize directly
Map<String, String> bundleInformation = getFromDatabase();

成员变量bundleListoldBundleList.

于 2013-08-21T03:47:56.680 回答