[Cherbot-commit] SF.net SVN: cherbot: [79] trunk/src
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-17 20:32:22
|
Revision: 79
http://svn.sourceforge.net/cherbot/?rev=79&view=rev
Author: christianhujer
Date: 2007-06-17 13:32:19 -0700 (Sun, 17 Jun 2007)
Log Message:
-----------
Added delegation based on regular expressions.
Added Paths:
-----------
trunk/src/net/sf/cherbot/redel/
trunk/src/net/sf/cherbot/redel/PatternDelegator.java
trunk/src/net/sf/cherbot/redel/Patterns.java
trunk/src/net/sf/cherbot/redel/package-info.java
trunk/src/test/net/sf/cherbot/redel/
trunk/src/test/net/sf/cherbot/redel/PatternDelegatorTest.java
Added: trunk/src/net/sf/cherbot/redel/PatternDelegator.java
===================================================================
--- trunk/src/net/sf/cherbot/redel/PatternDelegator.java (rev 0)
+++ trunk/src/net/sf/cherbot/redel/PatternDelegator.java 2007-06-17 20:32:19 UTC (rev 79)
@@ -0,0 +1,69 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.redel;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/** Delegator that searches a method that matches a regular expression and invokes it.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class PatternDelegator {
+
+ /** The target to use delegation on. */
+ @NotNull private final Object target;
+
+ /** The methods to use delegation on. */
+ @NotNull private final Map<Pattern, Method> targetMethods = new HashMap<Pattern, Method>();
+
+ /** Create a PatternDelegator.
+ * @param target Object to use delegation on.
+ */
+ public PatternDelegator(@NotNull final Object target) {
+ this.target = target;
+ final Method[] methods = target.getClass().getMethods();
+ for (final Method method : methods) {
+ @Nullable final Patterns patterns = method.getAnnotation(Patterns.class);
+ if (patterns != null) {
+ for (final String expression : patterns.value()) {
+ targetMethods.put(Pattern.compile(expression), method);
+ }
+ }
+ }
+ }
+
+ /** Process a String.
+ * @param string String to process.
+ * @return The number of matching methods that were found.
+ * @throws IllegalAccessException In case the target method cannot be invoked.
+ * @throws InvocationTargetException In case the target method threw an exception.
+ */
+ public int process(@NotNull final String string) throws IllegalAccessException, InvocationTargetException {
+ int matchesFound = 0;
+ for (final Map.Entry<Pattern, Method> entry : targetMethods.entrySet()) {
+ final Pattern pattern = entry.getKey();
+ final Matcher matcher = pattern.matcher(string);
+ if (matcher.matches()) {
+ matchesFound++;
+ final String[] groups = new String[matcher.groupCount()];
+ for (int i = 0; i < groups.length; i++) {
+ groups[i] = matcher.group(i + 1);
+ }
+ final Method method = entry.getValue();
+ method.invoke(target, (Object[]) groups);
+ }
+ }
+ return matchesFound;
+ }
+
+} // class PatternDelegator
Property changes on: trunk/src/net/sf/cherbot/redel/PatternDelegator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/net/sf/cherbot/redel/Patterns.java
===================================================================
--- trunk/src/net/sf/cherbot/redel/Patterns.java (rev 0)
+++ trunk/src/net/sf/cherbot/redel/Patterns.java 2007-06-17 20:32:19 UTC (rev 79)
@@ -0,0 +1,23 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot.redel;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/** Annotation for methods that should be automatically called when their regular expression matches.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Patterns {
+
+ /** Returns the regular expressions that should match for this method to be invoked.
+ * @return The regular expressions to match.
+ */
+ String[] value();
+
+} // @interface Patterns
Property changes on: trunk/src/net/sf/cherbot/redel/Patterns.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/net/sf/cherbot/redel/package-info.java
===================================================================
--- trunk/src/net/sf/cherbot/redel/package-info.java (rev 0)
+++ trunk/src/net/sf/cherbot/redel/package-info.java 2007-06-17 20:32:19 UTC (rev 79)
@@ -0,0 +1,10 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+/** A framework for simple delegating facades based on regular expressions.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+package net.sf.cherbot.redel;
Property changes on: trunk/src/net/sf/cherbot/redel/package-info.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/test/net/sf/cherbot/redel/PatternDelegatorTest.java
===================================================================
--- trunk/src/test/net/sf/cherbot/redel/PatternDelegatorTest.java (rev 0)
+++ trunk/src/test/net/sf/cherbot/redel/PatternDelegatorTest.java 2007-06-17 20:32:19 UTC (rev 79)
@@ -0,0 +1,90 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package test.net.sf.cherbot.redel;
+
+import org.junit.Test;
+import org.junit.Assert;
+import org.jetbrains.annotations.NotNull;
+import net.sf.cherbot.redel.Patterns;
+import net.sf.cherbot.redel.PatternDelegator;
+
+/** Unit-Test for{@link net.sf.cherbot.redel.PatternDelegator}.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class PatternDelegatorTest {
+
+ /** Tests whether delegation works.
+ * @throws Exception Exception (unexpected).
+ */
+ @Test public void testDelegation() throws Exception {
+ final PatternsDummy dummy = new PatternsDummy();
+ Assert.assertTrue("process1 must not have been invoked yet.", dummy.getInvocations() == 0);
+ final PatternDelegator testling = new PatternDelegator(dummy);
+ Assert.assertTrue("process1 must still not have been invoked yet.", dummy.getInvocations() == 0);
+ testling.process("foo: bar");
+ Assert.assertTrue("process1 must have been invoked once now.", dummy.getInvocations() == 1);
+ Assert.assertEquals("First match group must be passed as first argument.", "foo", dummy.getGroup1());
+ Assert.assertEquals("Second match group must be passed as second argument.", "bar", dummy.getGroup2());
+ }
+
+ /** Dummy for testing the PatternDelegator.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+ public static class PatternsDummy {
+
+ /** Most recent group1 parameter value of {@link #process1(String, String)}. */
+ private String group1;
+
+ /** Most recent group2 parameter value of {@link #process1(String, String)}. */
+ private String group2;
+
+ /** Number of invocations of {@link #process1(String, String)}. */
+ private int invocations;
+
+ /** Dummy test method.
+ * @param group1 First group
+ * @param group2 Second group
+ */
+ @Patterns("^(.*?): (.*)$")
+ public void process1(@NotNull final String group1, @NotNull final String group2) {
+ this.group1 = group1;
+ this.group2 = group2;
+ invocations++;
+ }
+
+ /** Returns the group1 parameter of the most recent {@link #process1(String,String)} invocation.
+ * @return Most recent group1 value.
+ */
+ public String getGroup1() {
+ return group1;
+ }
+
+ /** Returns the group2 parameter of the most recent {@link #process1(String,String)} invocation.
+ * @return Most recent group2 value.
+ */
+ public String getGroup2() {
+ return group2;
+ }
+
+ /** Returns the number of invocations of {@link #process1(String,String)}.
+ * @return The number of invocations of {@link #process1(String,String)}.
+ * @see #resetInvocations()
+ */
+ public int getInvocations() {
+ return invocations;
+ }
+
+ /** Resets the number of invocations.
+ * @see #getInvocations()
+ */
+ public void resetInvocations() {
+ invocations = 0;
+ }
+
+ } // class PatternsDummy
+
+} // class PatternDelegatorTest
Property changes on: trunk/src/test/net/sf/cherbot/redel/PatternDelegatorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|