项目作者: surajp

项目描述 :
A simple lazy collections framework in apex
高级语言: Apex
项目地址: git://github.com/surajp/lazy-apex-collections.git
创建时间: 2020-05-27T11:51:14Z
项目社区:https://github.com/surajp/lazy-apex-collections

开源协议:MIT License

下载


A simple lazy collections framework in Apex

The operations are run only when toList() or toMapByField() is called on the collection. The records are looped over only once

  • Usage
  1. Account[] result = acctCollection
  2. .mapValues(new AccountMap())
  3. .filterValues(new AccountRevenueFilter())
  4. .mapValues(new AccountMap())
  5. .toList();
  • Aternatively, you can retrieve a map of record by field value
  1. Map<String,Account> acctIndustryMap = new Map<String,Account>();
  2. acctCollection
  3. .mapValues(new AccountMap())
  4. .filterValues(new AccountRevenueFilter())
  5. .mapValues(new AccountMap())
  6. .toMapByField(Account.Industry,acctIndustryMap);
  7. //acctIndustryMap will contain the mapped values. If there are multiple records with the same 'Industry' value, the last record in the list with the value will be returned in the map
  • or, you can retrieve a map of list of records by field value
  1. Map<String,List<Account>> acctsIndustryMap = new Map<String,List<Account>>();
  2. acctCollection
  3. .mapValues(new AccountMap())
  4. .filterValues(new AccountRevenueFilter())
  5. .mapValues(new AccountMap())
  6. .toMapByField(Account.Industry,acctsIndustryMap);
  7. //acctIndustryMap will contain the mapped values
  • Creating a Collection
  1. Account[] accounts = [Select Name,AnnualRevenue from Accounts];
  2. Collection acctCollection = Collection.of(accounts);
  • How to implement a mapper class
  1. class AccountMap implements MapOperation {
  2. public SObject doMap(SObject a) {
  3. Account acc = (Account) a;
  4. acc.AnnualRevenue = acc.Name.startsWith(('Ac)
  5. ? acc.AnnualRevenue == null ? 10000 : acc.AnnualRevenue * 2
  6. : 5000;
  7. return acc;
  8. }
  9. }
  • How to implement a filter class
  1. class AccountRevenueFilter implements FilterOperation {
  2. public Boolean doFilter(SObject a) {
  3. return ((Account) a).AnnualRevenue > 5001;
  4. }
  5. }