Frage

What's the best way to accomplish this problem? I want to call a particular method for the first time when I am running my application but second time it should be called only whenever there is any change. If there is no change then I don't want to call that method.

Below is the method which will get called from my main application. And in the below method, I am retrieving the data from the database. For the first time, after retrieving the data from the db, map will have something like below-

BundleFramework    1.0.0
Bundle-A         1.0.0
Bundle-B         1.0.0

Now, I want to print out BundleFramework and its version separately. And then call the process method which will include Bundle-A and Bundle-B and its version in the map for the first time.

And now my background thread is running every two seconds, which will also call getBundlesInformation method. Now if there is no change in the version for Bundle-A and Bundle-B, then I don't want to call process method but if there is any change in any of the version of either Bundle-A or Bundle-B, then pass that change only to process method.

I am almost there in my below code but what is happening currently is- If there is no change in the database for Bundle-A and Bundle-B version then also it calls process method with Bundle-A and Bundle-B information.

What I want is- for the first time (when running from main application), it should call process method with Bundle-A and Bundle-B, and next time it should call process method only when there is any change in the version of that bundle.

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;
}

Can anybody help me with this? I am using Nosql database so I cannot have a trigger feature.

Updated Code:-

Below is my full code-

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();
    }
}
War es hilfreich?

Lösung

Use String#equals() for string comparison. Not equals != would most likely return true even when two strings are content-wise equivalent. So, change your if condition as

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

Your rest of the code is quite confusing. For example,

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

Also, since whenever bundleList is not empty you're setting oldBundleList to the bundleList. So, assuming Maps.difference() works as expected it should not return any differences at all.

EDIT : (as per OP's updated code)

Change the last part of 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);
}

EDIT : (redundant code)

You have a lot of redundant/empty Map creations. For example,

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

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

Same goes with member variables bundleList and oldBundleList.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top