001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.util.parallel.partition; 031 032import java.util.ArrayList; 033import java.util.Iterator; 034import java.util.List; 035import java.util.Queue; 036 037/** 038 * A {@link FixedSizeBlockingChunkPartitioner} dynamically partitions data into 039 * chunks of a fixed length. The partitioner does not need to know about the 040 * length of the data apriori. Beyond a {@link FixedSizeChunkPartitioner} this 041 * implementation doesn't use an underlying iterator. It can only be 042 * instantiated on queues and will always report to have more items available 043 * while it waits for the queue to be filled. 044 * 045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 046 * 047 * @param <T> 048 * Type of object in the partition 049 */ 050public class FixedSizeBlockingChunkPartitioner<T> implements Partitioner<T> { 051 private Queue<T> queue; 052 private int chunkSize = 20; 053 054 /** 055 * Construct with data in the form of a {@link Queue}. The number of items 056 * per chunk is set at the default value (20). 057 * 058 * @param objects 059 * the {@link Queue} representing the data. 060 */ 061 public FixedSizeBlockingChunkPartitioner(Queue<T> objects) { 062 this.queue = objects; 063 } 064 065 /** 066 * Construct with data in the form of an {@link Queue} and the given number 067 * of items per chunk. 068 * 069 * @param objects 070 * the {@link Queue} representing the data. 071 * @param chunkSize 072 * number of items in each chunk. 073 */ 074 public FixedSizeBlockingChunkPartitioner(Queue<T> objects, int chunkSize) { 075 this.queue = objects; 076 this.chunkSize = chunkSize; 077 } 078 079 @Override 080 public Iterator<Iterator<T>> getPartitions() { 081 return new Iterator<Iterator<T>>() { 082 083 @Override 084 public boolean hasNext() { 085 return true; 086 } 087 088 @Override 089 public Iterator<T> next() { 090 091 final List<T> list = new ArrayList<T>(chunkSize); 092 093 int i = 0; 094 while (i < chunkSize) { 095 T toAdd = null; 096 synchronized (queue) { 097 toAdd = queue.poll(); 098 } 099 if (toAdd == null) { 100 continue; 101 } 102 list.add(toAdd); 103 i++; 104 } 105 106 return list.iterator(); 107 108 } 109 110 @Override 111 public void remove() { 112 throw new UnsupportedOperationException("Not supported"); 113 } 114 }; 115 } 116}