You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/11/25 21:47:14 UTC

cvs commit: jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor Algorithms.java

rwaldhoff    2003/11/25 12:47:14

  Modified:    functor/src/java/org/apache/commons/functor Algorithms.java
  Log:
  extract inner class FindWithinGenerator
  
  Revision  Changes    Path
  1.12      +49 -42    jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/Algorithms.java
  
  Index: Algorithms.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/Algorithms.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Algorithms.java	25 Nov 2003 19:39:44 -0000	1.11
  +++ Algorithms.java	25 Nov 2003 20:47:14 -0000	1.12
  @@ -164,7 +164,7 @@
        * Returns a {@link Generator} that will apply the given {@link UnaryFunction} to each
        * generated element.
        */
  -    public static final Generator apply(final Generator gen, final UnaryFunction func) {
  +    public static final Generator apply(final Generator gen, final UnaryFunction func) {        
           return new BaseGenerator(gen) {
               public void run(final UnaryProcedure proc) {
                   gen.run(new UnaryProcedure() {
  @@ -190,21 +190,10 @@
        *
        * @see #detect(Generator,UnaryPredicate)
        */
  -    public static final boolean contains(final Generator gen, final UnaryPredicate pred) {
  -        // javas' inner classes suck, i should do this a different way i guess
  -        final boolean[] returnCode = new boolean[1];
  -        returnCode[0] = false;
  -
  -        gen.run(new UnaryProcedure() {
  -            public void run(Object obj) {
  -                if (pred.test(obj)) {
  -                    returnCode[0] = true;
  -                    gen.stop();
  -                }
  -            }
  -        });
  -
  -        return returnCode[0];
  +    public static final boolean contains(Generator gen, UnaryPredicate pred) {
  +        FindWithinGenerator finder = new FindWithinGenerator(gen,pred);
  +        gen.run(finder);
  +        return finder.wasFound();
       }
   
       /**
  @@ -231,18 +220,10 @@
        * @throws NoSuchElementException If no element could be found.
        */
       public static final Object detect(final Generator gen, final UnaryPredicate pred) {
  -        final Object[] foundObj = new Object[1];
  -        gen.run(new UnaryProcedure() {
  -            public void run(Object obj) {
  -                if(pred.test(obj)) {
  -                    foundObj[0] = obj;
  -                    gen.stop();
  -                }
  -            }
  -        });
  -
  -        if (foundObj[0] != null) {
  -            return foundObj[0];
  +        FindWithinGenerator finder = new FindWithinGenerator(gen,pred);
  +        gen.run(finder);
  +        if(finder.wasFound()) {
  +            return finder.getFoundObject();
           } else {
               throw new NoSuchElementException("No element matching " + pred + " was found.");
           }
  @@ -257,18 +238,10 @@
        * @see #detect(Generator,UnaryPredicate)
        */
       public static final Object detect(final Generator gen, final UnaryPredicate pred, Object ifNone) {
  -        final Object[] foundObj = new Object[1];
  -        gen.run(new UnaryProcedure() {
  -            public void run(Object obj) {
  -                if(pred.test(obj)) {
  -                    foundObj[0] = obj;
  -                    gen.stop();
  -                }
  -            }
  -        });
  -
  -        if (foundObj[0] != null) {
  -            return foundObj[0];
  +        FindWithinGenerator finder = new FindWithinGenerator(gen,pred);
  +        gen.run(finder);
  +        if(finder.wasFound()) {
  +            return finder.getFoundObject();
           } else {
               return ifNone;
           }
  @@ -439,4 +412,38 @@
   
           return result;
       }
  +    
  +    // inner classes
  +    //---------------------------------------------------------------
  +    
  +    private static class FindWithinGenerator implements UnaryProcedure {
  +        FindWithinGenerator(Generator gen, UnaryPredicate pred) {
  +            this.generator = gen;
  +            this.predicate = pred;
  +            this.found = false;
  +            this.foundObject = null;
  +        }
  +
  +        public void run(Object obj) {
  +            if(predicate.test(obj)) {
  +                found = true;
  +                foundObject = obj;
  +                generator.stop();
  +            }
  +        }
  +        
  +        boolean wasFound() {
  +            return found;
  +        }
  +        
  +        Object getFoundObject() {
  +            return foundObject;
  +        }
  +        
  +        private UnaryPredicate predicate = null;
  +        private boolean found = false;
  +        private Object foundObject = null;
  +        private Generator generator = null;
  +    }
  +    
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org