A Legacy Notes Developer's journey into madness.

Updated incinerate()

Devin Olson  February 27 2018 11:35:42 AM
Back in August of 2013 I originally posted Nate and Tim's Excellent Recycle.    

Granted, if you are coding in XPages the OpenNTF Domino API has pretty much made the need for this method irrelevant (as the ODA's first goal is to eliminate the "ham-fisted Exception handling in the lotus.domino API").

HOWEVER, if you are doing non-XPages Domino Java code then this recycle method is still relevant.

I had a need to enhance this method to handle Map and Collection values.  I present to you this enhanced code, in the hopes that it helps ease your pain when developing Java code in Notes and Domino.

Hope this helps!

/**
* Recycles Domino Objects without throwing an exception
*
* @author Nathan T. Freeman, Tim Tripcony, Devin S. Olson
*
* @param dominoObjects
*            Domino Objects to be recycled.
*            
* @see http://learningxpages.com/blog.nsf/dx/02272018023541PMDOLQYV.htm
*/
@SuppressWarnings("unchecked")
public
static void incinerate(final Object... objects) {
     if (null == objects) { return; }

     try {
             for (final Object object : objects) {
                     if (object != null) {
                             if (object instanceof Base) {
                                     // normal recycle
                                     ((Base) object).recycle();

                             } else if (object instanceof Map) {
                                     // incinerate all keys and values
                                     final Set entries = ((Map) object).entrySet();
                                     for (final Map.Entry entry : entries) {
                                             incinerate(entry.getKey(), entry.getValue());
                                     }

                             } else if (object instanceof Collection) {
                                     // incinerate every element in the collection
                                     final Iterator i = ((Collection) object).iterator();
                                     while (i.hasNext()) {
                                             final Object element = i.next();
                                             incinerate(element);
                                     }

                             } else if (object.getClass().isArray()) {
                                     // incinerate every element in the array
                                     final Object[] objectsArray = (Object[]) object;
                                     for (final Object element : objectsArray) {
                                             incinerate(element);
                                     }
                             }
                     }
             }

     } catch (final NotesException recycleSucks) {
             // optionally log exception (why bother?)
     }
}
Comments
No Comments Found