01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.core.util;
17:
18: import java.util.Iterator;
19:
20: import org.apache.commons.collections.IteratorUtils;
21:
22: /**
23: * This class provides utility methods to support the operation of transactional services
24: */
25: public class TransactionalServiceUtils {
26: /**
27: * Copys iterators so that they may be used outside of this class. Often, the DAO may
28: * return iterators that may not be used outside of this class because the transaction/
29: * connection may be automatically closed by Spring.
30: *
31: * This method copies all of the elements in the OJB backed iterators into list-based iterators
32: * by placing the returned BOs into a list
33: *
34: * @param iter an OJB backed iterator to copy
35: * @return an Iterator that may be used outside of this class
36: */
37: public static <E> Iterator<E> copyToExternallyUsuableIterator(
38: Iterator<E> iter) {
39: return IteratorUtils.toList(iter).iterator();
40: }
41:
42: /**
43: * Returns the first element and exhausts an iterator
44: *
45: * @param <E> the type of elements in the iterator
46: * @param iterator the iterator to exhaust
47: * @return the first element of the iterator; null if the iterator's empty
48: */
49: public static <E> E retrieveFirstAndExhaustIterator(
50: Iterator<E> iterator) {
51: E returnVal = null;
52: if (iterator.hasNext()) {
53: returnVal = iterator.next();
54: }
55: exhaustIterator(iterator);
56: return returnVal;
57: }
58:
59: /**
60: * Exhausts (i.e. complete iterates through) an iterator
61: *
62: * @param iterator
63: */
64: public static void exhaustIterator(Iterator<?> iterator) {
65: while (iterator.hasNext()) {
66: iterator.next();
67: }
68: }
69: }
|