解决此问题的最佳方法是什么?我想在运行我的应用程序时第一次调用特定方法,但第二次只有在有任何更改时才应调用它。如果没有变化,那么我不想调用那个方法。
以下是从我的主应用程序调用的方法。在下面的方法中,我正在从数据库中检索数据。在从数据库中检索数据后,第一次,地图将具有如下所示的内容 -
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();
}
}
答案 0 :(得分: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
不为空,您才会将oldBundleList
设置为bundleList
。因此,假设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();
与成员变量bundleList
和oldBundleList
相同。