001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function len
021 */
022package lucee.runtime.functions.string;
023
024import java.util.List;
025import java.util.Map;
026
027import lucee.runtime.PageContext;
028import lucee.runtime.exp.FunctionException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.Collection;
032import lucee.runtime.type.Query;
033
034public final class Len implements Function {
035        public static double call(PageContext pc , String string) {
036                return string.length();
037        }
038        public static double call(PageContext pc , Object obj) throws FunctionException {
039                double len=invoke(obj, -1);
040                if(len==-1)throw new FunctionException(pc,"len",1,"object","this type  ["+Caster.toTypeName(obj)+"] is not supported for returning the len");
041                return len;
042        }
043        
044        public static double invoke(Object obj, double defaultValue) {
045                if(obj instanceof CharSequence)return ((CharSequence)obj).length();
046                if(obj instanceof Query)return ((Query)obj).getRecordcount();
047                if(obj instanceof Collection)return ((Collection)obj).size();
048                if(obj instanceof Map)return ((Map)obj).size();
049                if(obj instanceof List)return ((List)obj).size();
050                if(obj instanceof Object[])return ((Object[])obj).length;
051                if(obj instanceof short[])return ((short[])obj).length;
052                if(obj instanceof int[])return ((int[])obj).length;
053                if(obj instanceof float[])return ((float[])obj).length;
054                if(obj instanceof double[])return ((double[])obj).length;
055                if(obj instanceof long[])return ((long[])obj).length;
056                if(obj instanceof char[])return ((char[])obj).length;
057                if(obj instanceof boolean[])return ((boolean[])obj).length;
058                if(obj instanceof byte[])return ((byte[])obj).length;
059                String str = Caster.toString(obj,null);
060                if(str!=null) return str.length();
061                
062                return defaultValue;
063        }
064}