afv-library/skills/apex-development/apex-class/templates/utility.cls
2026-02-10 11:57:55 -05:00

98 lines
3.5 KiB
OpenEdge ABL

/**
* @description Utility class for {describe the category of utilities: String, Date, Collection, etc.}.
* All methods are static and side-effect-free (no SOQL, no DML).
* Private constructor prevents instantiation.
* @author Generated by Apex Class Writer Skill
*/
public with sharing class {ClassName} {
// Private Constructor
/**
* @description Prevents instantiation use static methods only
*/
@TestVisible
private {ClassName}() {
// Utility class do not instantiate
}
// Public Methods
// TODO: Add utility methods below. Examples:
/**
* @description Safely converts a String to an Integer, returning a default if parsing fails
* @param value The String to parse
* @param defaultValue The fallback value if parsing fails
* @return The parsed Integer or the default value
* @example
* Integer result = {ClassName}.safeParseInteger('42', 0); // returns 42
* Integer result = {ClassName}.safeParseInteger('abc', 0); // returns 0
*/
public static Integer safeParseInteger(String value, Integer defaultValue) {
if (String.isBlank(value)) {
return defaultValue;
}
try {
return Integer.valueOf(value.trim());
} catch (TypeException e) {
return defaultValue;
}
}
/**
* @description Chunks a list into smaller sublists of the specified size.
* Useful for processing records in governor-limit-safe batches.
* @param items The list to chunk
* @param chunkSize The maximum size of each chunk
* @return A list of sublists, each containing up to chunkSize elements
* @example
* List<List<String>> chunks = {ClassName}.chunkList(myList, 200);
*/
public static List<List<Object>> chunkList(List<Object> items, Integer chunkSize) {
List<List<Object>> chunks = new List<List<Object>>();
if (items == null || items.isEmpty() || chunkSize <= 0) {
return chunks;
}
List<Object> currentChunk = new List<Object>();
for (Object item : items) {
currentChunk.add(item);
if (currentChunk.size() == chunkSize) {
chunks.add(currentChunk);
currentChunk = new List<Object>();
}
}
if (!currentChunk.isEmpty()) {
chunks.add(currentChunk);
}
return chunks;
}
/**
* @description Extracts a Set of non-null field values from a list of SObjects
* @param records The SObject records to extract from
* @param fieldName The API name of the field to extract
* @return A Set of non-null String values
* @example
* Set<String> emails = {ClassName}.pluckStrings(contacts, 'Email');
*/
public static Set<String> pluckStrings(List<SObject> records, String fieldName) {
Set<String> values = new Set<String>();
if (records == null || String.isBlank(fieldName)) {
return values;
}
for (SObject record : records) {
Object val = record.get(fieldName);
if (val != null) {
values.add(String.valueOf(val));
}
}
return values;
}
}