1 16 package org.apache.velocity.tools.generic; 17 18 import java.lang.reflect.Array ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 52 public class ArrayTool { 53 54 57 public ArrayTool() { 58 } 59 60 79 public List list(Object array) { 80 if (!this.isArray(array)) { 81 return null; 82 } 83 84 int length = Array.getLength(array); 86 List asList = new ArrayList (length); 87 for (int index = 0; index < length; ++index) { 88 asList.add(Array.get(array, index)); 89 } 90 return asList; 91 } 92 93 106 public Object get(Object array, int index) { 107 if (!this.isArray(array)) { 108 return null; 109 } 110 111 try { 112 return Array.get(array, index); 113 } catch (IndexOutOfBoundsException e) { 114 return null; 116 } 117 } 118 119 132 public Object set(Object array, int index, Object value) { 133 if (!this.isArray(array)) { 134 return null; 135 } 136 137 try { 138 Array.set(array, index, value); 139 return ""; 140 } catch (IndexOutOfBoundsException e) { 141 return null; 143 } 144 } 145 146 157 public Integer length(Object array) { 158 if (!this.isArray(array)) { 159 return null; 160 } 161 162 return new Integer (Array.getLength(array)); 164 } 165 166 177 public Object clone(Object array) { 178 if (!this.isArray(array)) { 179 return null; 180 } 181 182 Class type = array.getClass().getComponentType(); 183 int length = Array.getLength(array); 184 Object clone = Array.newInstance(type, length); 185 System.arraycopy(array, 0, clone, 0, length); 186 return clone; 187 } 188 189 195 public boolean isArray(Object object) { 196 if (object == null) { 197 return false; 198 } 199 return object.getClass().isArray(); 200 } 201 } 202 | Popular Tags |