| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CountFunction |
|
| 3.0;3 |
| 1 | /* | |
| 2 | * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/function/CountFunction.java,v 1.14 2006/02/05 21:47:41 elharo Exp $ | |
| 3 | * $Revision: 1.14 $ | |
| 4 | * $Date: 2006/02/05 21:47:41 $ | |
| 5 | * | |
| 6 | * ==================================================================== | |
| 7 | * | |
| 8 | * Copyright 2000-2002 bob mcwhirter & James Strachan. | |
| 9 | * All rights reserved. | |
| 10 | * | |
| 11 | * Redistribution and use in source and binary forms, with or without | |
| 12 | * modification, are permitted provided that the following conditions are | |
| 13 | * met: | |
| 14 | * | |
| 15 | * * Redistributions of source code must retain the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer. | |
| 17 | * | |
| 18 | * * Redistributions in binary form must reproduce the above copyright | |
| 19 | * notice, this list of conditions and the following disclaimer in the | |
| 20 | * documentation and/or other materials provided with the distribution. | |
| 21 | * | |
| 22 | * * Neither the name of the Jaxen Project nor the names of its | |
| 23 | * contributors may be used to endorse or promote products derived | |
| 24 | * from this software without specific prior written permission. | |
| 25 | * | |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
| 27 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| 28 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
| 29 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | |
| 30 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 37 | * | |
| 38 | * ==================================================================== | |
| 39 | * This software consists of voluntary contributions made by many | |
| 40 | * individuals on behalf of the Jaxen Project and was originally | |
| 41 | * created by bob mcwhirter <bob@werken.com> and | |
| 42 | * James Strachan <jstrachan@apache.org>. For more information on the | |
| 43 | * Jaxen Project, please see <http://www.jaxen.org/>. | |
| 44 | * | |
| 45 | * $Id: CountFunction.java,v 1.14 2006/02/05 21:47:41 elharo Exp $ | |
| 46 | */ | |
| 47 | ||
| 48 | package org.jaxen.function; | |
| 49 | ||
| 50 | import java.util.List; | |
| 51 | ||
| 52 | import org.jaxen.Context; | |
| 53 | import org.jaxen.Function; | |
| 54 | import org.jaxen.FunctionCallException; | |
| 55 | ||
| 56 | /** | |
| 57 | * <p><b>4.1</b> <code><i>number</i> count(<i>node-set</i>)</code></p> | |
| 58 | * | |
| 59 | * <blockquote src="http://www.w3.org/TR/xpath#function-count"> | |
| 60 | * The <b>count</b> function returns the number of nodes in the argument node-set. | |
| 61 | * </blockquote> | |
| 62 | * @author bob mcwhirter (bob @ werken.com) | |
| 63 | * @see <a href="http://www.w3.org/TR/xpath#function-count">Section 4.1 of the XPath Specification</a> | |
| 64 | */ | |
| 65 | public class CountFunction implements Function | |
| 66 | { | |
| 67 | ||
| 68 | /** | |
| 69 | * Create a new <code>CountFunction</code> object. | |
| 70 | */ | |
| 71 | 106 | public CountFunction() {} |
| 72 | ||
| 73 | /** | |
| 74 | * <p> | |
| 75 | * Returns the number of nodes in the specified node-set. | |
| 76 | * </p> | |
| 77 | * @param context ignored | |
| 78 | * @param args the function arguments | |
| 79 | * | |
| 80 | * @return a <code>Double</code> giving the integral number of items in the first argument | |
| 81 | * | |
| 82 | * @throws FunctionCallException if args does not have exactly one | |
| 83 | * item; or that item is not a <code>List</code> | |
| 84 | */ | |
| 85 | public Object call(Context context, | |
| 86 | List args) throws FunctionCallException | |
| 87 | { | |
| 88 | 536 | if (args.size() == 1) |
| 89 | { | |
| 90 | 532 | return evaluate( args.get(0) ); |
| 91 | } | |
| 92 | ||
| 93 | 4 | throw new FunctionCallException( "count() requires one argument." ); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * <p> | |
| 98 | * Returns the number of nodes in the specified node-set. | |
| 99 | * </p> | |
| 100 | * | |
| 101 | * @param obj a <code>List</code> of nodes | |
| 102 | * @return the integral number of items in the list | |
| 103 | * @throws FunctionCallException if obj is not a <code>List</code> | |
| 104 | */ | |
| 105 | public static Double evaluate(Object obj) throws FunctionCallException | |
| 106 | { | |
| 107 | ||
| 108 | 532 | if (obj instanceof List) |
| 109 | { | |
| 110 | 530 | return new Double( ((List) obj).size() ); |
| 111 | } | |
| 112 | ||
| 113 | 2 | throw new FunctionCallException("count() function can only be used for node-sets"); |
| 114 | ||
| 115 | } | |
| 116 | ||
| 117 | } |