2013-08-21 81 views
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-B的版本中的任何版本有任何更改,則只將該更改傳遞給process method

我幾乎在我的下面的代碼,但目前發生了什麼 - 如果數據庫沒有變化的Bundle-A和Bundle-B版本,那麼它也調用與Bundle-A和Bundle-B的處理方法信息。

我想要的是 - 第一次(從主應用程序運行時),它應該使用Bundle-A和Bundle-B調用過程方法,並且下一次它應該只在調用過程方法時發生任何更改該捆綁包的版本。

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

回答

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()最後一部分爲

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

+0

:好的。我補充說,但如果這些軟件包的版本沒有變化,它仍然會第二次調用過程方法。如果你願意,你也可以嘗試運行它。 – ferhan

+0

堅持..我搞砸了莫名其妙..讓我編輯代碼馬上爲你.. – ferhan

+0

我剛糾正我的代碼..讓我知道如果仍然不清楚.. – ferhan

相關問題