1 package net.jforum.entities; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import net.jforum.view.forum.common.PostCommon; 8 9 15 public class PollChanges { 16 private List deletedOptions = new ArrayList (); 17 private List newOptions = new ArrayList (); 18 private List changedOptions = new ArrayList (); 19 20 private boolean hasChanges; 21 22 private Poll first; 23 private Poll second; 24 25 32 public PollChanges(Poll first, Poll second) { 33 this.first = first; 34 this.second = second; 35 } 36 37 public void addChangedOption(PollOption option) { 38 this.changedOptions.add(option); 39 this.hasChanges = true; 40 } 41 42 public List getChangedOptions() { 43 return this.changedOptions; 44 } 45 46 public void addDeletedOption(PollOption option) { 47 this.deletedOptions.add(option); 48 this.hasChanges = true; 49 } 50 51 public List getDeletedOptions() { 52 return this.deletedOptions; 53 } 54 55 public void addNewOption(PollOption option) { 56 this.newOptions.add(option); 57 this.hasChanges = true; 58 } 59 60 public List getNewOptions() { 61 return this.newOptions; 62 } 63 64 public boolean hasChanges() { 65 this.searchForChanges(); 66 return this.hasChanges; 67 } 68 69 private void searchForChanges() { 70 if (first == null || second == null) { 71 return; 72 } 73 74 boolean isSame = first.getLabel().equals(second.getLabel()); 75 isSame &= first.getLength() == second.getLength(); 76 77 this.hasChanges = !isSame; 78 79 List firstOptions = first.getOptions(); 80 List secondOptions = second.getOptions(); 81 82 int firstSize = firstOptions.size(); 83 int secondSize = secondOptions.size(); 84 int maxCount = Math.min(firstSize, secondSize); 85 86 for (Iterator iter = firstOptions.iterator(); iter.hasNext(); ) { 88 PollOption option = (PollOption)iter.next(); 89 PollOption changed = this.findOptionById(option.getId(), secondOptions); 90 91 if (changed != null && !option.getText().equals(changed.getText())) { 92 this.addChangedOption(changed); 93 } 94 else if (changed == null) { 95 this.addDeletedOption(option); 96 } 97 } 98 99 for (Iterator iter = secondOptions.iterator(); iter.hasNext(); ) { 101 PollOption option = (PollOption)iter.next(); 102 103 if (this.findOptionById(option.getId(), firstOptions) == null) { 104 this.addNewOption(option); 105 } 106 } 107 } 108 109 private PollOption findOptionById(int id, List options) { 110 for (Iterator iter = options.iterator(); iter.hasNext(); ) { 111 PollOption o = (PollOption)iter.next(); 112 113 if (o.getId() == id) { 114 return o; 115 } 116 } 117 118 return null; 119 } 120 } | Popular Tags |