Tag Archives: language java

ConcurrentModificationException when using unmodifiableCollection

Learning something new everyday.

One of the best ways to avoid data manipulation problems in concurrent environments is to use read only data sets, one of the best way way to create a read only collection in Java is to use unmodifiableCollection method, however this method is not as innocent as you may think.

Behind the scenes unmodifiable collections are just a stub that redirect all methods to the original collections, except for methods that actually change the collection like add etc, in which case it throws an exception.

Test yourself, what happens when trying to iterate over an unmodifiable collection, when in a different thread new elements are added to the original collection… ConcurrentModificationException .

The best way to avoid it is to actually create a copy, e.g. new LinkedList(…)

To sum it up:

  1. Use unmodifiable collections whenever you want to make sure that in an external usage of the collection nothing will be modified, for example use it as a return value for your methods.
  2. Use different mechanisms in case of a multi-threaded environment, for example creating a real copy.
Advertisements

Leave a comment

Filed under API, Uncategorized

Instagram API wrapper for Android

Synopsis

Instagram(IG) API wrapper for Android for handling an Instagram app lifecycle. Split into 2 sub-projects:

  • InstagramAuth Authentication library, based uppon Facebook for Android library.
  • restig A wrapper arround IG rest API calls. Based upon restFB.

Documentation

Currently short on documentation, will add more as time permits.

Examples

InstagramAuth

/**
 * Authorize a user in Instagram. Will show up the auth dialog and will
 * allow the user to enter his/her credentials.
*

* <strong> Pay attention</strong> Use the listener afterwards to save the
 * authentication token.
 *
 * @param listener
 * - A dialog listener that can react to various authentication
 * events. Cannot be null.
 * @param permissions
 * - Use various permissions. Can be empty but not null.
 * @param activity
 * - An activity that will be the parent of the dialog box.
 */
 public void authorize(final DialogListener listener,
 final Activity activity, final String... permissions) {
 InstagramModule module = new InstagramModule();
 final InstagramAuthDialog dialog = new InstagramAuthDialog(activity,
 listener, mAppId, permissions);
 dialog.setCancelable(false);
 dialog.show();
 }

public class InstagramModule implements
 DialogListener {
 @Override
 public void onComplete(final Bundle values) {
 String accessToken = values.getString("access_token");
 }

@Override
 public void onError(final DialogError error) {
 // Log error
 }

@Override
 public void onCancel() {
 // User canceled.
 }
 }
 ...
 authorize(new InstagramModule(), activity, "likes", "comments");

restig

InstagramClient userClient = new DefaultInstagramClient(appId, mAccessToken);
final String endPoint = format("media/%s/comments",
"1234");
final InstagramCollection comments = client
.fetchCollection(endPoint, Comment.class);

You can download the source from github

Leave a comment

Filed under API