001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.util; 017 018import java.util.Iterator; 019 020import org.apache.commons.collections.IteratorUtils; 021 022/** 023 * This class provides utility methods to support the operation of transactional services 024 */ 025public final class TransactionalServiceUtils { 026 027 private TransactionalServiceUtils() { 028 throw new UnsupportedOperationException("do not call"); 029 } 030 /** 031 * Copys iterators so that they may be used outside of this class. Often, the DAO may 032 * return iterators that may not be used outside of this class because the transaction/ 033 * connection may be automatically closed by Spring. 034 * 035 * This method copies all of the elements in the OJB backed iterators into list-based iterators 036 * by placing the returned BOs into a list 037 * 038 * @param iter an OJB backed iterator to copy 039 * @return an Iterator that may be used outside of this class 040 */ 041 public static <E> Iterator<E> copyToExternallyUsuableIterator(Iterator<E> iter) { 042 return IteratorUtils.toList(iter).iterator(); 043 } 044 045 /** 046 * Returns the first element and exhausts an iterator 047 * 048 * @param <E> the type of elements in the iterator 049 * @param iterator the iterator to exhaust 050 * @return the first element of the iterator; null if the iterator's empty 051 */ 052 public static <E> E retrieveFirstAndExhaustIterator(Iterator<E> iterator) { 053 E returnVal = null; 054 if (iterator.hasNext()) { 055 returnVal = iterator.next(); 056 } 057 exhaustIterator(iterator); 058 return returnVal; 059 } 060 061 /** 062 * Exhausts (i.e. complete iterates through) an iterator 063 * 064 * @param iterator 065 */ 066 public static void exhaustIterator(Iterator<?> iterator) { 067 while (iterator.hasNext()) { 068 iterator.next(); 069 } 070 } 071}