First, what is apex-lang?
apex-lang is an open-source library of helper classes written purely in apex whose goal is to address shortcomings in the core apex classes. (If you’re not familiar with apex, apex is the programming language provided by salesforce.com’s development platform and is essentially a trimmed down version of java with added syntax for exploiting the force.com platform.)
apex-lang was inspired by the Apache Commons Lang project whose creators had a similar goal of addressing gaps in java’s core API. apex is fairly feature rich; however, let’s face it, functionality in its core API is sorely lacking. And why shouldn’t it? salesforce.com is in the “on-demand platform” business, not the API building business. On the other hand, apex-lang is very much in the API building (not for profit) business. So, its the intent of apex-lang to fill in the gaps in the core apex classes. And to fill them more quickly than salesforce can. Salesforce only has 3-4 releases a year; in contrast, apex-lang can be released as many times as needed during a single year.
Installing apex-lang
There are a couple of ways you can install apex-lang.
Option 1: Install the Unmanaged Package
1) Go to http://code.google.com/p/apex-lang/
2) Click on the Install link
3) Enter your username and password for your salesforce org (obviously, do this in a sandbox or DE org first)
4) Click Continue.
5) On “Step 1. Approve Package API Access”, click Next
6) On “Step 2. Choose security level”, select “Grant access to all users”
7) On “Step 3. Install package”, click Install. After a few moments, installation should be complete.
Option 2: Download and import into Force.com IDE
1) Open Force.com IDE
2) Select your project and expand the classes folder
3) Go to http://code.google.com/p/apex-lang/
4) Click on the Download link
5) Save and then extract the apex-lang source zip file to your hard drive
6) Open the extracted folder in Windows Explorer.
7) Select all files (including the *-meta.xml files).
8) Drag and drop the files into your project’s class folder in the Force.com IDE. This should trigger the Force.com IDE to save the classes locally as well as to your salesforce org.
Utilizing apex-lang
The easiest way kick apex-lang’s tires is to log into your salesforce org and click on the “System Log” link in the top right corner of the screen. This will open up a screen which will allow you to execute anonymous apex. Simply copy and paste the following code into the lower text box and click “Execute Apex >>”.
System.debug(StringUtils.abbreviate('A string to abbreviate.',8));
System.debug(StringUtils.rightPad('Test',10,'X'));
System.debug(ArrayUtils.toString(new String[]{'Test','10','X'}));
System.debug(ArrayUtils.toString(new Double[]{1.2,3.4,5.6}));
To understand what functionality is available in apex-lang, review the Test classes. They’re the best examples since every class and method is covered 100% by the test code. And they don’t just cover the code, it truly tests the code via many, many asserts. The StringUtils class contains so much code that the corresponding test class had to be broken into two test classes TestStringUtils and TestStringUtils2. To more fully understand available methods, you’ll want to refer to the Apache Commons Lang Javadoc.




Facebook
flickr
Linked In
Twitter