Over a million developers have joined DZone.

Give Your Code a Shot of Caffeine

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Almost all programmers and developers at least have some working knowledge of Java, and it’s the most commonly used language for Android apps. The TIOBE Index for October 2014 names Java as the second most used coding language on the Internet. So, if you're like me and you like to work as efficiently as possible, Percolate has some exciting news for you.

On October 13th, Percolate announced its Caffeine Android library. Percolate promises that the library will speed up development for Android, as Java, in their own words, can be very “verbose.” Caffeine promises to do these three things:

1. Shrink Your Codebase

“Some tasks that would require multiple lines of code now only take one, or maybe two lines.  At Percolate, using this library, we have not only shrunk our code base, but also eliminated fair amount of code duplication.  This follows the DRY, (short for Don’t Repeat Yourself), software development principle, which is a very good thing to strive for.”

2. Provide Greater Protection Against Buggy Code

“In the case of Android applications, poor exception handling can lead to buggy apps that crash, which results in a poor experience for users.  Caffeine helps solve this by providing very thorough, well reviewed, and heavily tested code that can be used in critical parts of your application, where exception handling may otherwise be forgotten."

3. Be Part of an Open-Source Family

“Percolate has always strived to produce high-quality, feature-rich software that is fun to use.  Caffeine was built with those standards, as were our other open source tools: jennifer, jsonmatch, and redset."

At an initial glance, it seems that all the codes have one theme in common: Your time spent coding and maintaining that code will be greatly decreased. For example: Caffeine’s CountUtil code contains number formatting and stylistic choices, i.e. you would have 15k instead of 15,000.

01.packagecom.percolate.caffeine;
02. 
03.importjava.text.DecimalFormat;
04. 
05./**
06.* <h2>Utility methods for formatting counts</h2>
07.* <p/>
08.* <h3>Common uses:</h3>
09.* CountUtil.{@link #getFormattedCount getFormattedCount}(1200000); // returns "1.2m"
10. 
11.*/
12. 
13.publicclassCountUtil {
14. 
15./**
16.* @see #getFormattedCount(Long)
17.*/
18.publicstaticString getFormattedCount(intcount) {
19.returngetFormattedCount(Long.valueOf(count));
20.}
21. 
22./**
23.* @see #getFormattedCount(Long)
24.*/
25.publicstaticString getFormattedCount(String count) {
26.returngetFormattedCount(Long.parseLong(count));
27.}
28. 
29./**
30.* Used to format a given number into a short representation.
31.* <p/>
32.* Examples:
33.* Given 9100, will return "9.1k".
34.* Given 8100000, will return "8.1m"
35.* Given 10, will return 10"
36.*
37.* @param count Value to convert.
38.* @return Formatted value (see examples)
39.*/
40.publicstaticString getFormattedCount(Long count) {
41.finalString unit;
42.finalDouble dbl;
43.finalDecimalFormat format = newDecimalFormat("#.#");
44.if(count < 1000) {
45.returnformat.format(count);
46.} elseif(count < 1000000) {
47.unit = "k";
48.dbl = count / 1000.0;
49.} elseif(count < 1000000000) {
50.unit = "m";
51.dbl = count / 1000000.0;
52.} else{
53.unit = "b";
54.dbl = count / 1000000000.0;
55.}
56.returnformat.format(dbl) + unit;
57.}
58.}


Want to check if your device’s connectivity? Use Caffeine’s PhoneUtils code:

01.packagecom.percolate.caffeine;
02. 
03.importandroid.content.Context;
04.importandroid.net.ConnectivityManager;
05.importandroid.net.NetworkInfo;
06.importandroid.provider.Settings;
07. 
08./**
09.* <h2>Common phone utility methods</h2>
10.* <p/>
11.* <h3>Common uses:</h3>
12.* PhoneUtils.{@link #isRotationEnabled isRotationEnabled}(this);
13. 
14.* PhoneUtils.{@link #isNetworkAvailable isNetworkAvailable}(this);
15. 
16.* PhoneUtils.{@link #isConnectedWifi isConnectedWifi}(this);
17. 
18.* PhoneUtils.{@link #isConnectedMobile isConnectedMobile}(this);
19. 
20.*/
21.publicclassPhoneUtils {
22. 
23./**
24.* Checks to see if the user has rotation enabled/disabled in their phone settings.
25.*
26.* @param context The current Context or Activity that this method is called from
27.* @return true if rotation is enabled, otherwise false.
28.*/
29.publicstaticbooleanisRotationEnabled(Context context) {
30.returnandroid.provider.Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
31.}
32. 
33./**
34.* Checks to see if the device is connected to a network (cell, wifi, etc).
35.*
36.* @param context The current Context or Activity that this method is called from
37.* @return true if a network connection is available, otherwise false.
38.*/
39.publicstaticbooleanisNetworkAvailable(Context context) {
40.ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
41.NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
42.returnactiveNetworkInfo != null&& activeNetworkInfo.isConnected();
43.}
44. 
45./**
46.* Check if there is any connectivity to a Wifi network.
47.* <p/>
48.* Can be used in combination with {@link #isConnectedMobile}
49.* to provide different features if the device is on a wifi network or a cell network.
50.*
51.* @param context The current Context or Activity that this method is called from
52.* @return true if a wifi connection is available, otherwise false.
53.*/
54.publicstaticbooleanisConnectedWifi(Context context) {
55.ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
56.NetworkInfo info = cm.getActiveNetworkInfo();
57.return(info != null&& info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
58.}
59. 
60./**
61.* Check if there is any connectivity to a mobile network
62.* <p/>
63.* Can be used in combination with {@link #isConnectedWifi}
64.* to provide different features if the device is on a wifi network or a cell network.
65.*
66.* @param context The current Context or Activity that this method is called from
67.* @return true if a mobile connection is available, otherwise false.
68.*/
69.publicstaticbooleanisConnectedMobile(Context context) {
70.ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
71.NetworkInfo info = cm.getActiveNetworkInfo();
72.return(info != null&& info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
73.}
74. 
75.}

These are just two examples of Caffeine code, courtesy of GitHub. How can you use Caffeine to streamline your development?

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}