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 arrayMerge 021 * Merge 2 arrays 022 */ 023package lucee.runtime.functions.arrays; 024 025import lucee.runtime.PageContext; 026import lucee.runtime.exp.PageException; 027import lucee.runtime.functions.BIF; 028import lucee.runtime.op.Caster; 029import lucee.runtime.type.Array; 030import lucee.runtime.type.ArrayImpl; 031 032 033public final class ArrayMerge extends BIF { 034 035 private static final long serialVersionUID = -391473381762154998L; 036 037 public static Array call(PageContext pc , Array arr1, Array arr2) throws PageException { 038 return call(pc,arr1,arr2,false); 039 } 040 public static Array call(PageContext pc , Array arr1, Array arr2, boolean leaveIndex) throws PageException { 041 042 ArrayImpl arr = new ArrayImpl(); 043 arr.ensureCapacity(arr1.size() + arr2.size()); 044 045 if(leaveIndex) { 046 set(arr, arr2); 047 set(arr, arr1); 048 return arr; 049 } 050 append(arr, arr1); 051 append(arr, arr2); 052 return arr; 053 } 054 055 @Override 056 public Object invoke(PageContext pc, Object[] args) throws PageException { 057 if(args.length==2)return call(pc,Caster.toArray(args[0]),Caster.toArray(args[1])); 058 return call(pc,Caster.toArray(args[0]),Caster.toArray(args[1]), Caster.toBooleanValue(args[2])); 059 } 060 061 062 public static void set(Array target,Array source) throws PageException { 063 int[] srcKeys=source.intKeys(); 064 for(int i=0;i<srcKeys.length;i++) { 065 target.setE(srcKeys[i],source.getE(srcKeys[i])); 066 } 067 } 068 069 public static void append(Array target,Array source) throws PageException { 070 int[] srcKeys=source.intKeys(); 071 for(int i=0;i<srcKeys.length;i++) { 072 target.append(source.getE(srcKeys[i])); 073 } 074 } 075 076}