PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/Apex/ArrayUtils.cls

https://gitlab.com/Aaeinstein54/linguist
Visual Basic for Applications | 458 lines | 400 code | 56 blank | 2 comment | 183 complexity | 2f3ed04175299a3cbe1710dfbc91d5f5 MD5 | raw file
  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. global static String[] EMPTY_STRING_ARRAY = new String[]{};
  14. global static Integer MAX_NUMBER_OF_ELEMENTS_IN_LIST {get{return 1000;}}
  15. global static List<String> objectToString(List<Object> objects){
  16. List<String> strings = null;
  17. if(objects != null){
  18. strings = new List<String>();
  19. if(objects.size() > 0){
  20. for(Object obj : objects){
  21. if(obj instanceof String){
  22. strings.add((String)obj);
  23. }
  24. }
  25. }
  26. }
  27. return strings;
  28. }
  29. global static Object[] reverse(Object[] anArray) {
  30. if (anArray == null) {
  31. return null;
  32. }
  33. Integer i = 0;
  34. Integer j = anArray.size() - 1;
  35. Object tmp;
  36. while (j > i) {
  37. tmp = anArray[j];
  38. anArray[j] = anArray[i];
  39. anArray[i] = tmp;
  40. j--;
  41. i++;
  42. }
  43. return anArray;
  44. }
  45. global static SObject[] reverse(SObject[] anArray) {
  46. if (anArray == null) {
  47. return null;
  48. }
  49. Integer i = 0;
  50. Integer j = anArray.size() - 1;
  51. SObject tmp;
  52. while (j > i) {
  53. tmp = anArray[j];
  54. anArray[j] = anArray[i];
  55. anArray[i] = tmp;
  56. j--;
  57. i++;
  58. }
  59. return anArray;
  60. }
  61. global static List<String> lowerCase(List<String> strs){
  62. List<String> returnValue = null;
  63. if(strs != null){
  64. returnValue = new List<String>();
  65. if(strs.size() > 0){
  66. for(String str : strs){
  67. returnValue.add(str == null ? null : str.toLowerCase());
  68. }
  69. }
  70. }
  71. return returnValue;
  72. }
  73. global static List<String> upperCase(List<String> strs){
  74. List<String> returnValue = null;
  75. if(strs != null){
  76. returnValue = new List<String>();
  77. if(strs.size() > 0){
  78. for(String str : strs){
  79. returnValue.add(str == null ? null : str.toUpperCase());
  80. }
  81. }
  82. }
  83. return returnValue;
  84. }
  85. global static List<String> trim(List<String> strs){
  86. List<String> returnValue = null;
  87. if(strs != null){
  88. returnValue = new List<String>();
  89. if(strs.size() > 0){
  90. for(String str : strs){
  91. returnValue.add(str == null ? null : str.trim());
  92. }
  93. }
  94. }
  95. return returnValue;
  96. }
  97. global static Object[] mergex(Object[] array1, Object[] array2){
  98. if(array1 == null){ return array2; }
  99. if(array2 == null){ return array1; }
  100. Object[] merged = new Object[array1.size() + array2.size()];
  101. for(Integer i = 0; i < array1.size(); i++){
  102. merged[i] = array1[i];
  103. }
  104. for(Integer i = 0; i < array2.size(); i++){
  105. merged[i+array1.size()] = array2[i];
  106. }
  107. return merged;
  108. }
  109. global static SObject[] mergex(SObject[] array1, SObject[] array2){
  110. if(array1 == null){ return array2; }
  111. if(array2 == null){ return array1; }
  112. if(array1.size() <= 0){ return array2; }
  113. List<SObject> merged = new List<SObject>();
  114. for(SObject sObj : array1){ merged.add(sObj); }
  115. for(SObject sObj : array2){ merged.add(sObj); }
  116. return merged;
  117. }
  118. global static Boolean isEmpty(Object[] objectArray){
  119. if(objectArray == null){
  120. return true;
  121. }
  122. return objectArray.size() == 0;
  123. }
  124. global static Boolean isEmpty(SObject[] objectArray){
  125. if(objectArray == null){
  126. return true;
  127. }
  128. return objectArray.size() == 0;
  129. }
  130. global static Boolean isNotEmpty(Object[] objectArray){
  131. return !isEmpty(objectArray);
  132. }
  133. global static Boolean isNotEmpty(SObject[] objectArray){
  134. return !isEmpty(objectArray);
  135. }
  136. global static Object[] pluck(SObject[] objectArray, String fieldName){
  137. if(isEmpty(objectArray) || fieldName == null || fieldName.trim() == null || fieldName.trim().length() == 0){
  138. return new Object[]{};
  139. }
  140. Object[] plucked = new Object[objectArray.size()];
  141. for(Integer i = 0; i < objectArray.size(); i++){
  142. plucked[i] = objectArray[i].get(fieldName);
  143. }
  144. return plucked;
  145. }
  146. global static String toString(Object[] objectArray){
  147. if(objectArray == null){
  148. return 'null';
  149. }
  150. String returnValue = '{';
  151. for(Integer i = 0; i < objectArray.size(); i++){
  152. if(i!=0){ returnValue += ','; }
  153. returnValue += '\'' + objectArray[i] + '\'';
  154. }
  155. returnValue += '}';
  156. return returnValue;
  157. }
  158. global static String toString(SObject[] objectArray){
  159. if(objectArray == null){
  160. return 'null';
  161. }
  162. String returnValue = '{';
  163. for(Integer i = 0; i < objectArray.size(); i++){
  164. if(i!=0){ returnValue += ','; }
  165. returnValue += '\'' + objectArray[i] + '\'';
  166. }
  167. returnValue += '}';
  168. return returnValue;
  169. }
  170. global static void assertArraysAreEqual(Object[] expected, Object[] actual){
  171. //check to see if one param is null but the other is not
  172. System.assert((expected == null && actual == null)|| (expected != null && actual != null),
  173. 'Assertion failed, the following two arrays are not equal. Expected: '
  174. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  175. if(expected != null && actual != null){
  176. System.assert(expected.size() == actual.size(), 'Assertion failed, the following two arrays are not equal. Expected: '
  177. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  178. for(Integer i = 0; i < expected.size(); i++){
  179. System.assert(expected[i] == actual[i], 'Assertion failed, the following two arrays are not equal. Expected: '
  180. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  181. }
  182. }
  183. }
  184. global static void assertArraysAreEqual(SObject[] expected, SObject[] actual){
  185. //check to see if one param is null but the other is not
  186. System.assert((expected == null && actual == null)|| (expected != null && actual != null),
  187. 'Assertion failed, the following two arrays are not equal. Expected: '
  188. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  189. if(expected != null && actual != null){
  190. System.assert(expected.size() == actual.size(), 'Assertion failed, the following two arrays are not equal. Expected: '
  191. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  192. for(Integer i = 0; i < expected.size(); i++){
  193. System.assert(expected[i] == actual[i], 'Assertion failed, the following two arrays are not equal. Expected: '
  194. + ArrayUtils.toString(expected) + ', Actual: ' + ArrayUtils.toString(actual));
  195. }
  196. }
  197. }
  198. global static List<Object> merg(List<Object> list1, List<Object> list2) {
  199. List<Object> returnList = new List<Object>();
  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(isNotEmpty(list1)){
  204. for(Object elmt : list1){
  205. returnList.add(elmt);
  206. }
  207. }
  208. if(isNotEmpty(list2)){
  209. for(Object elmt : list2){
  210. returnList.add(elmt);
  211. }
  212. }
  213. return returnList;
  214. }
  215. global static List<SObject> merg(List<SObject> list1, List<SObject> list2) {
  216. if(list1 != null && list2 != null && (list1.size()+list2.size()) > MAX_NUMBER_OF_ELEMENTS_IN_LIST){
  217. 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);
  218. }
  219. if(isEmpty(list1) && isEmpty(list2)){
  220. return null;
  221. }
  222. List<SObject> returnList = new List<SObject>();
  223. if(list1 != null){
  224. for(SObject elmt : list1){
  225. returnList.add(elmt);
  226. }
  227. }
  228. if(list2 != null){
  229. for(SObject elmt : list2){
  230. returnList.add(elmt);
  231. }
  232. }
  233. return returnList;
  234. }
  235. global static List<Object> subset(List<Object> aList, Integer count) {
  236. return subset(aList,0,count);
  237. }
  238. global static List<Object> subset(List<Object> list1, Integer startIndex, Integer count) {
  239. List<Object> returnList = new List<Object>();
  240. if(list1 != null && list1.size() > 0 && startIndex >= 0 && startIndex <= list1.size()-1 && count > 0){
  241. for(Integer i = startIndex; i < list1.size() && i - startIndex < count; i++){
  242. returnList.add(list1.get(i));
  243. }
  244. }
  245. return returnList;
  246. }
  247. global static List<SObject> subset(List<SObject> aList, Integer count) {
  248. return subset(aList,0,count);
  249. }
  250. global static List<SObject> subset(List<SObject> list1, Integer startIndex, Integer count) {
  251. List<SObject> returnList = null;
  252. if(list1 != null && list1.size() > 0 && startIndex <= list1.size()-1 && count > 0){
  253. returnList = new List<SObject>();
  254. for(Integer i = startIndex; i < list1.size() && i - startIndex < count; i++){
  255. returnList.add(list1.get(i));
  256. }
  257. }
  258. return returnList;
  259. }
  260. //===============================================
  261. //LIST/ARRAY SORTING
  262. //===============================================
  263. //FOR FORCE.COM PRIMITIVES (Double,Integer,ID,etc.):
  264. global static List<Object> qsort(List<Object> theList) {
  265. return qsort(theList,new PrimitiveComparator());
  266. }
  267. global static List<Object> qsort(List<Object> theList, Boolean sortAsc) {
  268. return qsort(theList,new PrimitiveComparator(),sortAsc);
  269. }
  270. global static List<Object> qsort(List<Object> theList, ObjectComparator comparator) {
  271. return qsort(theList,comparator,true);
  272. }
  273. global static List<Object> qsort(List<Object> theList, ObjectComparator comparator, Boolean sortAsc) {
  274. return qsort(theList, 0, (theList == null ? 0 : theList.size()-1),comparator,sortAsc);
  275. }
  276. //FOR SALESFORCE OBJECTS (sObjects):
  277. global static List<SObject> qsort(List<SObject> theList, ISObjectComparator comparator) {
  278. return qsort(theList,comparator,true);
  279. }
  280. global static List<SObject> qsort(List<SObject> theList, ISObjectComparator comparator,Boolean sortAsc ) {
  281. return qsort(theList, 0, (theList == null ? 0 : theList.size()-1),comparator,sortAsc);
  282. }
  283. private static List<Object> qsort(List<Object> theList,
  284. Integer lo0,
  285. Integer hi0,
  286. ObjectComparator comparator,
  287. Boolean sortAsc){
  288. Integer lo = lo0;
  289. Integer hi = hi0;
  290. if (lo >= hi) {
  291. return theList;
  292. } else if( lo == hi - 1 ) {
  293. if (( comparator.compare(theList[lo],theList[hi])>0 && sortAsc) ||
  294. (comparator.compare(theList[lo],theList[hi])<0 && !sortAsc)
  295. ) {
  296. Object prs = theList[lo];
  297. theList[lo] = theList[hi];
  298. theList[hi] = prs;
  299. }
  300. return theList;
  301. }
  302. Object pivot = theList[(lo + hi) / 2];
  303. theList[(lo + hi) / 2] = theList[hi];
  304. theList[hi] = pivot;
  305. while( lo < hi ) {
  306. while ((comparator.compare(theList[lo], pivot)<=0 && lo < hi && sortAsc) ||
  307. (comparator.compare(theList[lo], pivot)>=0 && lo < hi && !sortAsc)
  308. ) { lo++; }
  309. while (( comparator.compare(pivot,theList[hi])<=0 && lo < hi && sortAsc) ||
  310. ( comparator.compare(pivot,theList[hi])>=0 && lo < hi && !sortAsc)
  311. ) { hi--; }
  312. if( lo < hi ){
  313. Object prs = theList[lo];
  314. theList[lo] = theList[hi];
  315. theList[hi] = prs;
  316. }
  317. }
  318. theList[hi0] = theList[hi];
  319. theList[hi] = pivot;
  320. qsort(theList, lo0, lo-1,comparator,sortAsc);
  321. qsort(theList, hi+1, hi0,comparator,sortAsc);
  322. return theList;
  323. }
  324. private static List<SObject> qsort(List<SObject> theList,
  325. Integer lo0,
  326. Integer hi0,
  327. ISObjectComparator comparator,
  328. Boolean sortAsc){
  329. Integer lo = lo0;
  330. Integer hi = hi0;
  331. if (lo >= hi) {
  332. return theList;
  333. } else if( lo == hi - 1 ) {
  334. if (( comparator.compare(theList[lo],theList[hi])>0 && sortAsc) ||
  335. (comparator.compare(theList[lo],theList[hi])<0 && !sortAsc)
  336. ) {
  337. SObject prs = theList[lo];
  338. theList[lo] = theList[hi];
  339. theList[hi] = prs;
  340. }
  341. return theList;
  342. }
  343. SObject pivot = theList[(lo + hi) / 2];
  344. theList[(lo + hi) / 2] = theList[hi];
  345. theList[hi] = pivot;
  346. while( lo < hi ) {
  347. while ((comparator.compare(theList[lo], pivot)<=0 && lo < hi && sortAsc) ||
  348. (comparator.compare(theList[lo], pivot)>=0 && lo < hi && !sortAsc)
  349. ) { lo++; }
  350. while (( comparator.compare(pivot,theList[hi])<=0 && lo < hi && sortAsc) ||
  351. ( comparator.compare(pivot,theList[hi])>=0 && lo < hi && !sortAsc)
  352. ) { hi--; }
  353. if( lo < hi ){
  354. SObject prs = theList[lo];
  355. theList[lo] = theList[hi];
  356. theList[hi] = prs;
  357. }
  358. }
  359. theList[hi0] = theList[hi];
  360. theList[hi] = pivot;
  361. qsort(theList, lo0, lo-1,comparator,sortAsc);
  362. qsort(theList, hi+1, hi0,comparator,sortAsc);
  363. return theList;
  364. }
  365. /*
  366. global static List<Object> unique(List<Object> theList) {
  367. List<Object> uniques = new List<Object>();
  368. Set<Object> keys = new Set<Object>();
  369. if(theList != null && theList.size() > 0){
  370. for(Object obj : theList){
  371. if(keys.contains(obj)){
  372. continue;
  373. } else {
  374. keys.add(obj);
  375. uniques.add(obj);
  376. }
  377. }
  378. }
  379. return uniques;
  380. }
  381. global static List<SObject> unique(List<SObject> theList) {
  382. if(theList == null){
  383. return null;
  384. }
  385. List<SObject> uniques = createEmptySObjectList(theList.get(0));
  386. Set<String> keys = new Set<String>();
  387. if(theList != null && theList.size() > 0){
  388. String key = null;
  389. for(SObject obj : theList){
  390. key = obj == null ? null : ''+obj;
  391. if(keys.contains(key)){
  392. continue;
  393. } else {
  394. keys.add(key);
  395. uniques.add(obj);
  396. }
  397. }
  398. }
  399. return uniques;
  400. }
  401. */
  402. }