PollChanges.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 3k
Category:

Java Develop

Development Platform:

Java

  1. package net.jforum.entities;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import net.jforum.view.forum.common.PostCommon;
  6. /**
  7.  * An helper class that holds changes made to the pool.
  8.  * 
  9.  * @author Rafael Steil
  10.  * @version $Id: PollChanges.java,v 1.4 2007/04/24 02:19:46 rafaelsteil Exp $
  11.  */
  12. public class PollChanges {
  13. private List deletedOptions = new ArrayList();
  14. private List newOptions = new ArrayList();
  15. private List changedOptions = new ArrayList();
  16. private boolean hasChanges;
  17. private Poll first;
  18. private Poll second;
  19. /**
  20.  * @param first The "complete", most recent poll version. Usually the one
  21.  * that's in the database. 
  22.  * @param second The poll to compare with. It usually will be a poll filled
  23.  * by {@link PostCommon#fillPostFromRequest()}, so matches will be done againts the 
  24.  * existing poll and the data comming from the server. 
  25.  */
  26. public PollChanges(Poll first, Poll second) {
  27. this.first = first;
  28. this.second = second;
  29. }
  30. public void addChangedOption(PollOption option) {
  31. this.changedOptions.add(option);
  32. this.hasChanges = true;
  33. }
  34. public List getChangedOptions() {
  35. return this.changedOptions;
  36. }
  37. public void addDeletedOption(PollOption option) {
  38. this.deletedOptions.add(option);
  39. this.hasChanges = true;
  40. }
  41. public List getDeletedOptions() {
  42. return this.deletedOptions;
  43. }
  44. public void addNewOption(PollOption option) {
  45. this.newOptions.add(option);
  46. this.hasChanges = true;
  47. }
  48. public List getNewOptions() {
  49. return this.newOptions;
  50. }
  51. public boolean hasChanges() {
  52. this.searchForChanges();
  53. return this.hasChanges;
  54. }
  55. private void searchForChanges() {
  56. if (first == null || second == null) {
  57. return;
  58. }
  59. boolean isSame = first.getLabel().equals(second.getLabel());
  60. isSame &= first.getLength() == second.getLength();
  61. this.hasChanges = !isSame;
  62. List firstOptions = first.getOptions();
  63. List secondOptions = second.getOptions();
  64. // Search for changes in existing options
  65. for (Iterator iter = firstOptions.iterator(); iter.hasNext(); ) {
  66. PollOption option = (PollOption)iter.next();
  67. PollOption changed = this.findOptionById(option.getId(), secondOptions);
  68. if (changed != null && !option.getText().equals(changed.getText())) {
  69. this.addChangedOption(changed);
  70. }
  71. else if (changed == null) {
  72. this.addDeletedOption(option);
  73. }
  74. }
  75. // Check if the incoming poll added options
  76. for (Iterator iter = secondOptions.iterator(); iter.hasNext(); ) {
  77. PollOption option = (PollOption)iter.next();
  78. if (this.findOptionById(option.getId(), firstOptions) == null) {
  79. this.addNewOption(option);
  80. }
  81. }
  82. }
  83. private PollOption findOptionById(int id, List options) {
  84. for (Iterator iter = options.iterator(); iter.hasNext(); ) {
  85. PollOption o = (PollOption)iter.next();
  86. if (o.getId() == id) {
  87. return o;
  88. }
  89. }
  90. return null;
  91. }
  92. }