Tuesday, 20 August 2013

Print out the entries from the map when the value gets changed

Print out the entries from the map when the value gets changed

I am working on a project in which I need to print out the data from the
database in a certain way. Let's take an example, suppose in my database,
I have below entries only-
Framework 1.0.0
BundleB 1.0.0
BundleC 1.0.0
Then my Java method that will make a call to the database which will
return me a map of above data.
My map will have above data as below-
Key as Framework, Value as 1.0.0
Key as BundleB, Value as 1.0.0
Key as BundleC, Value as 1.0.0
Suppose, I started my program for the first time, then it will print out
like this with the below code I have, which is perfectly fine.
Framework - 1.0.0
And then I am running background thread every 2 seconds that will make a
call to the database and get all the data again from the database. And
every two seconds, it will print out the same thing as below- (which is
not what I want)
Framework - 1.0.0
I want to print out Framework - 1.0.0 for the first time when I am running
my program but second time when the background thread is running, it
should print out only when the version gets changed for that Framework,
otherwise don't print out anything.
Meaning after some time, if somebody changes the version information in
the database like this-
Framework 1.0.1
BundleB 1.0.0
BundleC 1.0.0
then only it should print out like this-
Framework - 1.0.1
I hope the questions is clear enough. Below is my code that I have so far.
public class Test {
public static Map<String, String> bundleList = 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 getAttributesFromDatabase() {
Map<String, String> bundleInformation = new LinkedHashMap<String,
String>();
bundleInformation = getFromDatabase();
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()) {
bundleList.put(key, bundleList.get(key));
}
}
String version = bundleList.get("Zero");
printOutZeroInformation("Zero", version);
}
private static void printOutZeroInformation(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;
}
private static void loggingAfterEveryXMilliseconds() {
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
}
getAttributesFromDatabase();
}
}
}.start();
}
}
Any help will be appreciated on this.

No comments:

Post a Comment