Apex-Lang List Functions (Merge, etc.)


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /* ============================================================
  2. * This code is part of the "apex-lang" open source project avaiable at:
  3. *
  4. * http://code.google.com/p/apex-lang/
  5. *
  6. * This code is licensed under the Apache License, Version 2.0. You may obtain a
  7. * copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. * ============================================================
  11. */
  12. global class ArrayUtils {
  13.  
  14. global static String[] EMPTY_STRING_ARRAY = new String[]{};
  15. global static Integer MAX_NUMBER_OF_ELEMENTS_IN_LIST {get{return 1000;}}
  16.  
  17. global static Object[] reverse(Object[] anArray) {
  18. if (anArray == null) {
  19. return null;
  20. }
  21. Integer i = 0;
  22. Integer j = anArray.size() - 1;
  23. Object tmp;
  24. while (j > i) {
  25. tmp = anArray[j];
  26. anArray[j] = anArray[i];
  27. anArray[i] = tmp;
  28. j--;
  29. i++;
  30. }
  31. return anArray;
  32. }
  33.  
  34. global static SObject[] reverse(SObject[] anArray) {
  35. if (anArray == null) {
  36. return null;
  37. }
  38. Integer i = 0;
  39. Integer j = anArray.size() - 1;
  40. SObject tmp;
  41. while (j > i) {
  42. tmp = anArray[j];
  43. anArray[j] = anArray[i];
  44. anArray[i] = tmp;
  45. j--;
  46. i++;
  47. }
  48. return anArray;
  49. }
  50.  
  51. global static Object[] mergex(Object[] array1, Object[] array2){
  52. if(array1 == null){ return array2; }
  53. if(array2 == null){ return array1; }
  54. Object[] merged = new Object[array1.size() + array2.size()];
  55. for(Integer i = 0; i < array1.size(); i++){
  56. merged[i] = array1[i];
  57. }
  58. for(Integer i = 0; i < array2.size(); i++){
  59. merged[i+array1.size()] = array2[i];
  60. }
  61. return merged;
  62. }
  63.  
  64. global static SObject[] mergex(SObject[] array1, SObject[] array2){
  65. if(array1 == null){ return array2; }
  66. if(array2 == null){ return array1; }
  67. if(array1.size() <= 0){ return array2; }
  68. List<SObject> merged = createEmptySObjectList(array1[0]);
  69. for(SObject sObj : array1){ merged.add(sObj); }
  70. for(SObject sObj : array2){ merged.add(sObj); }
  71. return merged;
  72. }
  73.  
  74. global static Boolean isEmpty(Object[] objectArray){
  75. if(objectArray == null){
  76. return true;
  77. }
  78. return objectArray.size() == 0;
  79. }
  80.  
  81. global static Boolean isEmpty(SObject[] objectArray){
  82. if(objectArray == null){
  83. return true;
  84. }
  85. return objectArray.size() == 0;
  86. }
  87.  
  88. global static Boolean isNotEmpty(Object[] objectArray){
  89. return !isEmpty(objectArray);
  90. }
  91.  
  92. global static Boolean isNotEmpty(SObject[] objectArray){
  93. return !isEmpty(objectArray);
  94. }
  95.  
  96. global static Object[] pluck(SObject[] objectArray, String fieldName){
  97. if(isEmpty(objectArray) || StringUtils.isBlank(fieldName)){
  98. return new Object[]{};
  99. }
  100. Object[] plucked = new Object[objectArray.size()];
  101. for(Integer i = 0; i < objectArray.size(); i++){
  102. plucked[i] = objectArray[i].get(fieldName);
  103. }
  104. return plucked;
  105. }
  106.  
  107.  
  108. global static String toString(Object[] objectArray){
  109. if(objectArray == null){
  110. return 'null';
  111. }
  112. String returnValue = '{';
  113. for(Integer i = 0; i < objectArray.size(); i++){
  114. if(i!=0){ returnValue += ','; }
  115. returnValue += '\'' + objectArray[i] + '\'';
  116. }
  117. returnValue += '}';
  118. return returnValue;
  119. }
  120.  
  121. global static String toString(SObject[] objectArray){
  122. if(objectArray == null){
  123. return 'null';
  124. }
  125. String returnValue = '{';
  126. for(Integer i = 0; i < objectArray.size(); i++){
  127. if(i!=0){ returnValue += ','; }
  128. returnValue += '\'' + objectArray[i] + '\'';
  129. }
  130. returnValue += '}';
  131. return returnValue;
  132. }
  133.  
  134. global static void assertArraysAreEqual(Object[] expected, Object[] actual){
  135. //check to see if one param is null but the other is not
  136. System.assert((expected == null && actual == null)|| (expected != null && actual != null),
  137. 'Assertion failed, the following two arrays are not equal. Expected: '
  138. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  139. if(expected != null && actual != null){
  140. System.assert(expected.size() == actual.size(), 'Assertion failed, the following two arrays are not equal. Expected: '
  141. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  142. for(Integer i = 0; i < expected.size(); i++){
  143. System.assert(expected[i] == actual[i], 'Assertion failed, the following two arrays are not equal. Expected: '
  144. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  145. }
  146. }
  147. }
  148.  
  149. global static void assertArraysAreEqual(SObject[] expected, SObject[] actual){
  150. //check to see if one param is null but the other is not
  151. System.assert((expected == null && actual == null)|| (expected != null && actual != null),
  152. 'Assertion failed, the following two arrays are not equal. Expected: '
  153. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  154. if(expected != null && actual != null){
  155. System.assert(expected.size() == actual.size(), 'Assertion failed, the following two arrays are not equal. Expected: '
  156. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  157. for(Integer i = 0; i < expected.size(); i++){
  158. System.assert(expected[i] == actual[i], 'Assertion failed, the following two arrays are not equal. Expected: '
  159. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  160. }
  161. }
  162. }
  163.  
  164. /**
  165. * This method is no longer needed as in Spring '10, you can created
  166. * generic SObject lists.
  167. *
  168. * @deprecated
  169. */
  170. global static List<SObject> createEmptySObjectList(SObject prototype){
  171. if(prototype == null){
  172. return null;
  173. }
  174. return Database.query(
  175. 'select Id from '
  176. + StringUtils.split(''+prototype,':')[0]
  177. + ' where Id = \'0015000000Mrr40\' LIMIT 0'); // use dummy Id to ensure no return
  178. }
  179.  
  180. global static List<Object> merg(List<Object> list1, List<Object> list2) {
  181. List<Object> returnList = new List<Object>();
  182. if(list1 != null && list2 != null && (list1.size()+list2.size()) > MAX_NUMBER_OF_ELEMENTS_IN_LIST){
  183. throw new IllegalArgumentException('Lists cannot be merged because new list would be greater than maximum number of elements in a list: ' + MAX_NUMBER_OF_ELEMENTS_IN_LIST);
  184. }
  185. if(isNotEmpty(list1)){
  186. for(Object elmt : list1){
  187. returnList.add(elmt);
  188. }
  189. }
  190. if(isNotEmpty(list2)){
  191. for(Object elmt : list2){
  192. returnList.add(elmt);
  193. }
  194. }
  195. return returnList;
  196. }
  197.  
  198.  
  199. global static List<SObject> merg(List<SObject> list1, List<SObject> list2) {
  200. if(list1 != null && list2 != null && (list1.size()+list2.size()) > MAX_NUMBER_OF_ELEMENTS_IN_LIST){
  201. throw new IllegalArgumentException('Lists cannot be merged because new list would be greater than maximum number of elements in a list: ' + MAX_NUMBER_OF_ELEMENTS_IN_LIST);
  202. }
  203. if(isEmpty(list1) && isEmpty(list2)){
  204. return null;
  205. }
  206. List<SObject> returnList = createEmptySObjectList(isNotEmpty(list1) ? list1.get(0) : list2.get(0));
  207. if(list1 != null){
  208. for(SObject elmt : list1){
  209. returnList.add(elmt);
  210. }
  211. }
  212. if(list2 != null){
  213. for(SObject elmt : list2){
  214. returnList.add(elmt);
  215. }
  216. }
  217. return returnList;
  218. }
  219.  
  220. global static List<Object> subset(List<Object> aList, Integer count) {
  221. return subset(aList,0,count);
  222. }
  223.  
  224. global static List<Object> subset(List<Object> list1, Integer startIndex, Integer count) {
  225. List<Object> returnList = new List<Object>();
  226. if(list1 != null && list1.size() > 0 && startIndex >= 0 && startIndex <= list1.size()-1 && count > 0){
  227. for(Integer i = startIndex; i < list1.size() && i - startIndex < count; i++){
  228. returnList.add(list1.get(i));
  229. }
  230. }
  231. return returnList;
  232. }
  233.  
  234.  
  235. global static List<SObject> subset(List<SObject> aList, Integer count) {
  236. return subset(aList,0,count);
  237. }
  238.  
  239. global static List<SObject> subset(List<SObject> list1, Integer startIndex, Integer count) {
  240. List<SObject> returnList = null;
  241. if(list1 != null && list1.size() > 0 && startIndex <= list1.size()-1 && count > 0){
  242. returnList = createEmptySObjectList(list1.get(0));
  243. for(Integer i = startIndex; i < list1.size() && i - startIndex < count; i++){
  244. returnList.add(list1.get(i));
  245. }
  246. }
  247. return returnList;
  248. }
  249.  
  250. //===============================================
  251. //LIST/ARRAY SORTING
  252. //===============================================
  253.  
  254. //FOR FORCE.COM PRIMITIVES (Double,Integer,ID,etc.):
  255. global static List<Object> qsort(List<Object> theList) {
  256. return qsort(theList,new PrimitiveComparator());
  257. }
  258.  
  259. global static List<Object> qsort(List<Object> theList, Boolean sortAsc) {
  260. return qsort(theList,new PrimitiveComparator(),sortAsc);
  261. }
  262.  
  263. global static List<Object> qsort(List<Object> theList, ObjectComparator comparator) {
  264. return qsort(theList,comparator,true);
  265. }
  266.  
  267. global static List<Object> qsort(List<Object> theList, ObjectComparator comparator, Boolean sortAsc) {
  268. return qsort(theList, 0, (theList == null ? 0 : theList.size()-1),comparator,sortAsc);
  269. }
  270.  
  271.  
  272.  
  273. //FOR SALESFORCE OBJECTS (sObjects):
  274. global static List<SObject> qsort(List<SObject> theList, ISObjectComparator comparator) {
  275. return qsort(theList,comparator,true);
  276. }
  277.  
  278. global static List<SObject> qsort(List<SObject> theList, ISObjectComparator comparator,Boolean sortAsc ) {
  279. return qsort(theList, 0, (theList == null ? 0 : theList.size()-1),comparator,sortAsc);
  280. }
  281.  
  282. private static List<Object> qsort(List<Object> theList,
  283. Integer lo0,
  284. Integer hi0,
  285. ObjectComparator comparator,
  286. Boolean sortAsc){
  287. Integer lo = lo0;
  288. Integer hi = hi0;
  289.  
  290. if (lo >= hi) {
  291. return theList;
  292. } else if( lo == hi - 1 ) {
  293.  
  294. if (( comparator.compare(theList[lo],theList[hi])>0 && sortAsc) ||
  295. (comparator.compare(theList[lo],theList[hi])<0 && !sortAsc)
  296. ) {
  297. Object prs = theList[lo];
  298. theList[lo] = theList[hi];
  299. theList[hi] = prs;
  300. }
  301. return theList;
  302. }
  303.  
  304. Object pivot = theList[(lo + hi) / 2];
  305. theList[(lo + hi) / 2] = theList[hi];
  306. theList[hi] = pivot;
  307.  
  308. while( lo < hi ) {
  309. while ((comparator.compare(theList[lo], pivot)<=0 && lo < hi && sortAsc) ||
  310. (comparator.compare(theList[lo], pivot)>=0 && lo < hi && !sortAsc)
  311. ) { lo++; }
  312. while (( comparator.compare(pivot,theList[hi])<=0 && lo < hi && sortAsc) ||
  313. ( comparator.compare(pivot,theList[hi])>=0 && lo < hi && !sortAsc)
  314. ) { hi--; }
  315.  
  316. if( lo < hi ){
  317. Object prs = theList[lo];
  318. theList[lo] = theList[hi];
  319. theList[hi] = prs;
  320. }
  321. }
  322.  
  323. theList[hi0] = theList[hi];
  324. theList[hi] = pivot;
  325.  
  326. qsort(theList, lo0, lo-1,comparator,sortAsc);
  327. qsort(theList, hi+1, hi0,comparator,sortAsc);
  328. return theList;
  329. }
  330.  
  331.  
  332. private static List<SObject> qsort(List<SObject> theList,
  333. Integer lo0,
  334. Integer hi0,
  335. ISObjectComparator comparator,
  336. Boolean sortAsc){
  337. Integer lo = lo0;
  338. Integer hi = hi0;
  339.  
  340. if (lo >= hi) {
  341. return theList;
  342. } else if( lo == hi - 1 ) {
  343.  
  344. if (( comparator.compare(theList[lo],theList[hi])>0 && sortAsc) ||
  345. (comparator.compare(theList[lo],theList[hi])<0 && !sortAsc)
  346. ) {
  347. SObject prs = theList[lo];
  348. theList[lo] = theList[hi];
  349. theList[hi] = prs;
  350. }
  351. return theList;
  352. }
  353.  
  354. SObject pivot = theList[(lo + hi) / 2];
  355. theList[(lo + hi) / 2] = theList[hi];
  356. theList[hi] = pivot;
  357.  
  358. while( lo < hi ) {
  359. while ((comparator.compare(theList[lo], pivot)<=0 && lo < hi && sortAsc) ||
  360. (comparator.compare(theList[lo], pivot)>=0 && lo < hi && !sortAsc)
  361. ) { lo++; }
  362. while (( comparator.compare(pivot,theList[hi])<=0 && lo < hi && sortAsc) ||
  363. ( comparator.compare(pivot,theList[hi])>=0 && lo < hi && !sortAsc)
  364. ) { hi--; }
  365.  
  366. if( lo < hi ){
  367. SObject prs = theList[lo];
  368. theList[lo] = theList[hi];
  369. theList[hi] = prs;
  370. }
  371. }
  372.  
  373. theList[hi0] = theList[hi];
  374. theList[hi] = pivot;
  375.  
  376. qsort(theList, lo0, lo-1,comparator,sortAsc);
  377. qsort(theList, hi+1, hi0,comparator,sortAsc);
  378. return theList;
  379. }
  380. /*
  381. global static List<Object> unique(List<Object> theList) {
  382. List<Object> uniques = new List<Object>();
  383. Set<Object> keys = new Set<Object>();
  384. if(theList != null && theList.size() > 0){
  385. for(Object obj : theList){
  386. if(keys.contains(obj)){
  387. continue;
  388. } else {
  389. keys.add(obj);
  390. uniques.add(obj);
  391. }
  392. }
  393. }
  394. return uniques;
  395. }
  396.  
  397. global static List<SObject> unique(List<SObject> theList) {
  398. if(theList == null){
  399. return null;
  400. }
  401. List<SObject> uniques = createEmptySObjectList(theList.get(0));
  402. Set<String> keys = new Set<String>();
  403. if(theList != null && theList.size() > 0){
  404. String key = null;
  405. for(SObject obj : theList){
  406. key = obj == null ? null : ''+obj;
  407. if(keys.contains(key)){
  408. continue;
  409. } else {
  410. keys.add(key);
  411. uniques.add(obj);
  412. }
  413. }
  414. }
  415. return uniques;
  416. }
  417. */
  418.  
  419.  
  420. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.