The page had the usual input text boxes and a list-box into which entries as seen within a companion list-box could be selected to add those selections to the second list-box. When the user removed items from the list-box, they were returned to the companion list-box. The problem was to capture those events as “adds’ or “deletes”.
The solution uses two ArrayLists. The first (named “preservedSelectedValues”) holds the list of items in a list-box BEFORE changes. The second (named “selectedValues”), holds the list of items in that list-box after changes. Both ArrayLists hold those entries as SelectItem objects.
Since I need to know what was added to or deleted from the list-box represented by the second ArrayLists it can be treated as a simple reconciliation problem. The brute force code, reeking of COBOL or RPG approaches, is represented here:
//Capture changes.
if (preservedSelectedValues.size() >= selectedValues.size()) {
int foundCount = 0;
for (int i=0; i < preservedSelectedValues.size(); i++) {
boolean found = false;
SelectItem psi = (SelectItem)preservedSelectedValues.get(i);
for(int j=0; j < selectedValues.size(); j++){
SelectItem si = (SelectItem)selectedValues.get(j);
//Found - No change
if(psi.getLabel().equals(si.getLabel()) ) {
found = true;
foundCount++;
break;
}
}
//The item was deleted
if (!found ) {
System.out.println("Deleted : " + psi.getLabel() + " " + psi.getValue());
}
}
if(preservedSelectedValues.size() == foundCount) {
System.out.println("No changes were detected.");
}
if(preservedSelectedValues.size() > foundCount) {
System.out.println("Some items have been deleted.");
}
}
if (selectedValues.size() >= preservedSelectedValues.size()) {
int foundCount = 0;
for (int i=0; i < selectedValues.size(); i++) {
boolean found = false;
SelectItem psi = (SelectItem)selectedValues.get(i);
for(int j=0; j < preservedSelectedValues.size(); j++){
SelectItem si = (SelectItem)preservedSelectedValues.get(j);
//Found - No change
if(psi.getLabel().equals(si.getLabel()) ) {
found = true;
foundCount++;
break;
}
}
//The item was added
if (!found ) {
System.out.println("Added : " + psi.getLabel() + " " + psi.getValue());
}
}
if(selectedValues.size() == foundCount) {
System.out.println("No changes were detected.");
}
if(selectedValues.size() > foundCount) {
System.out.println("Some items have been added.");
}
}
The better way is to create a comparator class which has dual responsibilities. The first responsibility is to act as a comparator to assist in sorting the ArrayLists. The second responsibility is to assist in the comparison for differences between the ArrayLists. The comparator class accepts 2 objects and then casts them to SelectItem objects to perform compares against the value properties. The returned value from this exercise is an integer reporting whether it was found to be less than, equal to or greater than. The comparator:
/**
* Modification History
*
* Date Project Pgmr Description
* -------- --------- ------- -----------------------------------------------
*
*
*/
package com.util;
import java.util.Comparator;
import javax.faces.model.SelectItem;
public class SelectItemComparator implements Comparator {
String name = this.getClass().getName();
/**
* Constructor
*
*/
public SelectItemComparator() {
super();
}
/**
* Compares the suppled SelectItem objects to each other. This method is used
* to resolve sorts and to determine equality.
*
*/
public int compare(Object o1, Object o2) {
SelectItem s1 = (SelectItem)o1;
SelectItem s2 = (SelectItem)o2;
return s1.getValue().toString().compareTo((s2.getValue().toString()));
}
}
The implementation does the sort followed by the comparison operations. The sprit is the same as the first set of code shown at the top of this post.
//Capture changes. First, sort the arraylists containing the SelectItem objects
Collections.sort(preservedSelectedValues, new SelectItemComparator());
Collections.sort(selectedValues, new SelectItemComparator());
//Second, perform the compares
for (int i=0,size=preservedSelectedValues.size(); i < size; i++) {
SelectItem psi = (SelectItem)preservedSelectedValues.get(i);
int pos = Collections.binarySearch(selectedValues, psi, new SelectItemComparator() );
if (pos<0) {
System.out.println("Comparator method. Deleted : " + psi.getLabel() + " " + psi.getValue());
}
}
for (int i=0,size=selectedValues.size(); i < size; i++) {
SelectItem si = (SelectItem)selectedValues.get(i);
int pos = Collections.binarySearch(preservedSelectedValues, si, new SelectItemComparator() );
if (pos<0) {
System.out.println("Comparator method. Added : " + si.getLabel() + " " + si.getValue());
}
}
There. Much better.
No comments:
Post a Comment