DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

First Look at Commons Lang 3.0 Beta

Shekhar Gulati user avatar by
Shekhar Gulati
·
Aug. 10, 10 · Interview
Like (0)
Save
Tweet
Share
14.34K Views

Join the DZone community and get the full member experience.

Join For Free

Apache Commons Lang 3.0 Beta was released on 4th August and the project has finally moved to Java 5. The API is now generified, varags are used where applicable, and some features which are now supported by Java itself have been  removed. All of this means that Lang 3.0 is not backwards compatible with the 2.x versions. To make it workable with the previous versions, the package name has been changed to 'apache.commons.lang3', allowing Lang 3.0 to sit side-by-side with previous version of Lang without any side effects. You can download the Commons Lang 3.0 beta from this link.

In this article, I will walk you through some of the features that have been added or improved in 3.0 release.

ArrayUtils 

The ArrayUtils class allows you to do operations on arrays, primitive arrays, and primitive wrapper arrays. This class exists since version 2.0 and in rhw 3.0 release it has been generified and a new method called toArray has been added. Lets take a look at its features:

  1. toArray method
    This is a new method added in release 3.0 which lets you create a type-safe generic array.This method is very useful because arrays cannot be created from a generic type. For more information on why arrays can't be created for a generic type, refer to this link. This method uses the fact that varags are implemented as arrays, so this methods take a varags of T type and returns an array of T.
    String[] array = ArrayUtils.toArray("shekhar", "gulati");
  2. Another use of toArray method is to create generic empty array.
           String[] array = ArrayUtils.<String> toArray();
  3. toMap method
    This method exists since the 2.x days and has been generified in 3.0 release.The result of generifying this method is that you get type safe maps, hence you avoid ClassCastExceptions. For example, in 2.x versions
    Map map = ArrayUtils.toMap(new String[][]{{"shekhar","gulati"}});
  4.  But in 3.0 version,    

    Map<String, Integer> map2 = ArrayUtils.<String,Integer>toMap(new Object[][]{{"shekhar",1}});
  5. Other methods like subarray,addAll, add, etc are also generified.Hence they are now typ-safe.

CharSequenceUtils

This class is a new class, added in the 3.0 version. This class allows you to do Null-safe operations on CharSequence. It has two methods:

  1. length method
    This method gives you the length of any CharSequence.
    CharSequenceUtils.length(CharBuffer.wrap("shekhar")); // return 7
    CharSequenceUtils.length("shekhar"); // return 7
    CharSequenceUtils.length(new StringBuilder("shekhar").append("gulati"));//return 13
  2. subSequence method
    This method returns a new CharSequence starting with char specified at the index.
    CharSequenceUtils.subSequence("test", 1);
    will return est

EnumUtils

 

The package 'org.apache.commons.lang.enums' has been removed from release 3.0 because from version 5 of Java, Enums are provided out of the box. A new class EnumUtils has been added which provides helper methods for Java enums. Lets take a look at some of its methods:

  1. getEnumMap method
    This method gives you a map of enums names and enums.
    enum ReportType{
    REPORT1,REPORT2
    }
    Map<String, ReportType> enumMap = EnumUtils.getEnumMap(ReportType.class);
  2. getEnumList method
    This method gives you a list of enums for a given enum class.
    List<ReportType> enumList = EnumUtils.getEnumList(ReportType.class);
  3. isValidEnum method
    This method checks if the given enum name is valid for given enum class.
    EnumUtils.isValidEnum(ReportType.class, "REPORT1");// will return true
    EnumUtils.isValidEnum(ReportType.class, "REPORT3"); // return false

ObjectUtils

ObjectUtils class methods like min,max, clone etc are generified which makes them type-safe. A new method firstNonNull(T...) is added. This method returns the first value in the array which is not null.

ObjectUtils.firstNonNull(null, null,null,null, "abc", null,"def");//will return "abc"

StringUtils

StringUtils does not need any introduction. I think every Java developer knows it. Some method signatures have changed to support CharSequence instead of String. For example, isEmpty(String) is changed to isEmpty(CharSequence). Some new methods have also been added :

  1. stripAccents method
    This method removes the accents from the string. This method works with JDK 1.6 method, and will fail on 1.5 version. 
    StringUtils.stripAccents("&ecute;clair") ;  //will return "eclair"
  2. normalizeSpace method
    This function returns the input string with whitespace normalized by using trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.
    StringUtils.normalizeSpace("  a  b   c  ") ; // will return "a b c"
These are some of the features that have been either added or improved in 3.0 beta release. I will talk about other new features in second part of this series on Commons-Lang 3.0. Some of other new features are related to concurrency support and pluggable API for text transformation.
BETA (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Tracking Software Architecture Decisions
  • Multi-Cloud Integration
  • 5 Software Developer Competencies: How To Recognize a Good Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: