Permalink
Browse files

Extract blips from bundles in BlipProcessor

This functionality is reusable, so let's move it into a library class.
  • Loading branch information...
1 parent af776cd commit eb8275c1e8ba2fb53a8e107167a8941e764899cc @emk committed Aug 30, 2009
Showing with 45 additions and 24 deletions.
  1. +40 −0 src/buglinky/BlipProcessor.java
  2. +5 −24 src/buglinky/BugLinkyServlet.java
@@ -15,13 +15,17 @@
package buglinky;
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.wave.api.Annotation;
import com.google.wave.api.Blip;
+import com.google.wave.api.Event;
import com.google.wave.api.Range;
+import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
/**
@@ -38,6 +42,42 @@
private int totalCorrection;
/**
+ * Apply a list of blip processors to any blips which have changed.
+ *
+ * @param processors The event processors to apply, in order.
+ * @param bundle The event bundle indicating which blips have changed.
+ * @param myAddress The Wave address of the current bot, used to make sure
+ * we don't respond to our own messages. BE CAREFUL!
+ * Failure to specify the correct address will cause
+ * infinite loops and overwhelm the servers.
+ */
+ static void applyProcessorsToChangedBlips(
+ ArrayList<BlipProcessor> processors,
+ RobotMessageBundle bundle, String myAddress) {
+ // Find all affected blips.
+ HashSet<Blip> changedBlips = new HashSet<Blip>();
+ for (Event e : bundle.getEvents()) {
+ if (!e.getModifiedBy().equals(myAddress)) {
+ switch (e.getType()) {
+ case BLIP_SUBMITTED: // The user has clicked "Done".
+ case BLIP_VERSION_CHANGED: // The blip has been updated.
+ changedBlips.add(e.getBlip());
+ break;
+
+ default:
+ break;
+ }
+ }
+ }
+
+ // Process all affected blips.
+ for (Blip blip : changedBlips) {
+ for (BlipProcessor processor : processors)
+ processor.processBlip(blip);
+ }
+ }
+
+ /**
* Apply this text processor to the specified blip. This function
* is not re-entrant.
*
@@ -20,7 +20,6 @@
import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
-import com.google.wave.api.Event;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
@@ -55,7 +54,7 @@
public void processEvents(RobotMessageBundle bundle) {
if (bundle.wasSelfAdded())
addInstructionsToWave(bundle);
- dispatchEvents(bundle);
+ processBlips(bundle);
}
/** Add an instruction blip to this wave if we were just added. */
@@ -67,32 +66,14 @@ private void addInstructionsToWave(RobotMessageBundle bundle) {
textView.append(INSTRUCTIONS);
}
- /** Dispatch events to the appropriate handler method. */
- private void dispatchEvents(RobotMessageBundle bundle) {
+ /** Process any blips which have changed. */
+ private void processBlips(RobotMessageBundle bundle) {
// We clean up URLs first, so that we can annotate the newly-created
// text in the second pass.
ArrayList<BlipProcessor> processors = new ArrayList<BlipProcessor>();
processors.add(new BugUrlReplacer(BUG_URL));
processors.add(new BugNumberLinker(BUG_URL));
-
- // Process each event.
- for (Event e : bundle.getEvents()) {
- if (!e.getModifiedBy().equals(BOT_ADDRESS)) {
- switch (e.getType()) {
- // One or the other of these should be wired up in
- // capabilities.xml. If we use BLIP_SUBMITTED, we'll apply
- // our links once the user clicks "Done". If we use
- // BLIP_VERSION_CHANGED, we'll apply our links in real time.
- case BLIP_SUBMITTED:
- case BLIP_VERSION_CHANGED:
- for (BlipProcessor processor : processors)
- processor.processBlip(e.getBlip());
- break;
-
- default:
- break;
- }
- }
- }
+ BlipProcessor.applyProcessorsToChangedBlips(processors, bundle,
+ BOT_ADDRESS);
}
}

0 comments on commit eb8275c

Please sign in to comment.