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/12/02 17:36:11 UTC

cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four ToInteger.java IsInteger.java Abs.java NthColumn.java DataMunger.java

rwaldhoff    2003/12/02 08:36:11

  Modified:    functor/src/test/org/apache/commons/functor/example/kata/four
                        DataMunger.java
  Added:       functor/src/test/org/apache/commons/functor/example/kata/four
                        ToInteger.java IsInteger.java Abs.java
                        NthColumn.java
  Log:
  extract inner classes
  
  Revision  Changes    Path
  1.2       +46 -111   jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/DataMunger.java
  
  Index: DataMunger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/DataMunger.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DataMunger.java	2 Dec 2003 01:12:07 -0000	1.1
  +++ DataMunger.java	2 Dec 2003 16:36:11 -0000	1.2
  @@ -59,13 +59,27 @@
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.Reader;
  -import java.util.StringTokenizer;
   
   import org.apache.commons.functor.Algorithms;
   import org.apache.commons.functor.BinaryFunction;
   import org.apache.commons.functor.UnaryFunction;
  -import org.apache.commons.functor.UnaryPredicate;
  +import org.apache.commons.functor.adapter.BinaryFunctionBinaryPredicate;
  +import org.apache.commons.functor.adapter.BinaryPredicateBinaryFunction;
  +import org.apache.commons.functor.adapter.IgnoreLeftFunction;
  +import org.apache.commons.functor.adapter.IgnoreRightFunction;
  +import org.apache.commons.functor.adapter.IgnoreRightPredicate;
  +import org.apache.commons.functor.adapter.UnaryFunctionUnaryPredicate;
  +import org.apache.commons.functor.adapter.UnaryPredicateUnaryFunction;
  +import org.apache.commons.functor.core.IsNull;
  +import org.apache.commons.functor.core.LeftIdentityFunction;
  +import org.apache.commons.functor.core.RightIdentityFunction;
  +import org.apache.commons.functor.core.comparator.IsLessThan;
  +import org.apache.commons.functor.core.composite.BinaryCompositeBinaryFunction;
   import org.apache.commons.functor.core.composite.CompositeUnaryFunction;
  +import org.apache.commons.functor.core.composite.ConditionalBinaryFunction;
  +import org.apache.commons.functor.core.composite.UnaryCompositeBinaryFunction;
  +import org.apache.commons.functor.example.kata.one.BinaryFunctionUnaryFunction;
  +import org.apache.commons.functor.example.kata.one.Subtract;
   import org.apache.commons.functor.example.lines.Lines;
   
   /**
  @@ -79,126 +93,47 @@
       }
   
       public static final Object process(final Reader file, final int selected, final int col1, final int col2) {
  -        return nthColumn(selected).evaluate(
  +        return NthColumn.instance(selected).evaluate(
               Algorithms.inject(
  -                Lines.from(file).where(nthColumnIsInteger(0)), 
  +                Lines.from(file).where(
  +                    UnaryFunctionUnaryPredicate.adapt(
  +                        new CompositeUnaryFunction(UnaryPredicateUnaryFunction.adapt(IsInteger.instance()),NthColumn.instance(0)))),
                   null,
                   lesserSpread(col1,col2)));            
       }
       
   
       /** 
  -     * A UnaryFunction that selects the nth column from the input 
  -     * String and converts it to an Integer.
  -     */
  -    private static final UnaryFunction nthInteger(int n) {  
  -        return new CompositeUnaryFunction(toInteger(),nthColumn(n));
  -    }
  -
  -    /** 
  -     * Selects the nth column from the input 
  -     * obj (String) and converts it to an int.
  -     */
  -    private static final int nthInteger(int n, Object obj) {
  -        return toInt(nthInteger(n).evaluate(obj));  
  -    }
  -
  -    /** 
  -     * A UnaryPredicate that returns true iff the the nth column 
  -     * in the input String can be converted into an Integer.
  -     * See {@link #toInteger}.
  -     */
  -    private static final UnaryPredicate nthColumnIsInteger(final int n) {
  -        return new UnaryPredicate() {
  -            public boolean test(Object obj) {
  -                try {
  -                    nthInteger(n).evaluate(obj);
  -                    return true;
  -                } catch(RuntimeException e) {
  -                    return false;
  -                }
  -            }
  -        };
  -    }
  -    
  -    /** 
  -     * A UnaryFunction that returns the nth whitespace
  -     * delimited column in the given input String, or 
  -     * null if there is no such column.
  -     */
  -    private static final UnaryFunction nthColumn(final int n) {
  -        return new UnaryFunction() {
  -            public Object evaluate(Object obj) {
  -                StringTokenizer toker = new StringTokenizer((String)obj);
  -                for(int count = 0; count < n && toker.hasMoreTokens();count++) {
  -                    toker.nextToken();
  -                }
  -                return toker.hasMoreTokens() ? toker.nextToken() : null;
  -            }
  -        };
  -    }
  -    
  -    /** 
  -     * Accessor method for {@link #TO_INTEGER}.
  -     */
  -    private static final UnaryFunction toInteger() {
  -        return TO_INTEGER;
  -    }
  -    
  -    /** 
  -     * Converts the input String to an Integer.
  -     * Any trailing characters that aren't digits
  -     * are ignored.
  -     */
  -    private static final UnaryFunction TO_INTEGER = new UnaryFunction() {
  -        public Object evaluate(Object obj) {
  -            return evaluate((String)obj);
  -        }
  -        
  -        public Object evaluate(String str) {
  -            StringBuffer buf = new StringBuffer();
  -            for(int i=0;i<str.length();i++) {
  -                if(Character.isDigit(str.charAt(i))) {
  -                    buf.append(str.charAt(i));
  -                } else {
  -                    break;
  -                }
  -            }
  -            try {
  -                return new Integer(buf.toString());
  -            } catch(NumberFormatException e) {
  -                throw new NumberFormatException(str);
  -            }
  -        }
  -    };
  -
  -    /** 
        * A BinaryFunction that will calcuate the absolute
        * difference between col1 and col2 in the given 
        * String arguments, and return the argument
        * whose difference is smallest.
        */
       private static final BinaryFunction lesserSpread(final int col1, final int col2) {
  -        return new BinaryFunction() {
  -            public Object evaluate(Object left, Object right) {
  -                if(null == left) {
  -                    return right;
  -                } else {
  -                    return absSpread(left) < absSpread(right) ? left : right;
  -                }
  -            }
  -            
  -            private int absSpread(Object obj) {
  -                return Math.abs(nthInteger(col1,obj) - nthInteger(col2,obj));
  -            }
  -        };
  +        return new ConditionalBinaryFunction(            
  +            IgnoreRightPredicate.adapt(IsNull.instance()), // if left is null
  +            RightIdentityFunction.instance(),              // return right
  +            new ConditionalBinaryFunction(                 // else calculate spread and compare
  +                BinaryFunctionBinaryPredicate.adapt(       
  +                    new BinaryCompositeBinaryFunction(
  +                        BinaryPredicateBinaryFunction.adapt(IsLessThan.instance()),
  +                        IgnoreRightFunction.adapt(absSpread(col1,col2)),
  +                        IgnoreLeftFunction.adapt(absSpread(col1,col2)))),
  +                LeftIdentityFunction.instance(),
  +                RightIdentityFunction.instance()
  +            )
  +        );
  +    }
  +
  +    private static UnaryFunction absSpread(final int col1, final int col2) {
  +        return new CompositeUnaryFunction(
  +            Abs.instance(), 
  +            new BinaryFunctionUnaryFunction(
  +                new UnaryCompositeBinaryFunction(
  +                    Subtract.instance(),
  +                    new CompositeUnaryFunction(ToInteger.instance(),NthColumn.instance(col1)),
  +                    new CompositeUnaryFunction(ToInteger.instance(),NthColumn.instance(col2)))
  +                ));
       }
   
  -    /**
  -     * Convert the given Number into an int.
  -     */    
  -    private static int toInt(Object obj) {
  -        return ((Number)obj).intValue();
  -    }
  -    
   }
  
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/ToInteger.java
  
  Index: ToInteger.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/ToInteger.java,v 1.1 2003/12/02 16:36:11 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived 
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.functor.example.kata.four;
  
  import org.apache.commons.functor.UnaryFunction;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/12/02 16:36:11 $
   * @author Rodney Waldhoff
   */
  public final class ToInteger implements UnaryFunction {
      public Object evaluate(Object obj) {
          return evaluate((String)obj);
      }
  
      public Object evaluate(String str) {
          StringBuffer buf = new StringBuffer();
          for(int i=0;i<str.length();i++) {
              if(Character.isDigit(str.charAt(i))) {
                  buf.append(str.charAt(i));
              } else {
                  break;
              }
          }
          try {
              return new Integer(buf.toString());
          } catch(NumberFormatException e) {
              throw new NumberFormatException(str);
          }
      }
      
      public static final ToInteger instance() {
          return INSTANCE;
      }
      
      private static final ToInteger INSTANCE = new ToInteger();
  }
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/IsInteger.java
  
  Index: IsInteger.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/IsInteger.java,v 1.1 2003/12/02 16:36:11 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived 
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.functor.example.kata.four;
  
  import org.apache.commons.functor.UnaryPredicate;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/12/02 16:36:11 $
   * @author Rodney Waldhoff
   */
  public final class IsInteger implements UnaryPredicate {
      public boolean test(Object obj) {
          try {
              ToInteger.instance().evaluate(obj);
              return true;
          } catch(RuntimeException e){
              return false;
          }
      }
      
      public static final IsInteger instance() {
          return INSTANCE;
      }
      
      private static final IsInteger INSTANCE = new IsInteger();
  }
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/Abs.java
  
  Index: Abs.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/Abs.java,v 1.1 2003/12/02 16:36:11 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived 
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.functor.example.kata.four;
  
  import org.apache.commons.functor.UnaryFunction;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/12/02 16:36:11 $
   * @author Rodney Waldhoff
   */
  public final class Abs implements UnaryFunction {
      public Object evaluate(Object obj) {
          return evaluate((Number)obj);
      }
  
      public Object evaluate(Number num) {
          return new Integer(Math.abs(num.intValue()));
      }
  
      public static final Abs instance() {
          return INSTANCE;
      }
      
      private static final Abs INSTANCE = new Abs();
  }
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/NthColumn.java
  
  Index: NthColumn.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/kata/four/NthColumn.java,v 1.1 2003/12/02 16:36:11 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived 
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.functor.example.kata.four;
  
  import java.util.StringTokenizer;
  
  import org.apache.commons.functor.UnaryFunction;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/12/02 16:36:11 $
   * @author Rodney Waldhoff
   */
  public final class NthColumn implements UnaryFunction {
      public NthColumn(int n) {
          this.n = n;
      }
      
      public Object evaluate(Object obj) {
          StringTokenizer toker = new StringTokenizer((String)obj);
          for(int count = 0; count < n && toker.hasMoreTokens();count++) {
              toker.nextToken();
          }
          return toker.hasMoreTokens() ? toker.nextToken() : null;
      }
  
      private final int n;
  
      public static final NthColumn instance(int n) {
          return new NthColumn(n);
      }
  }
  
  

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