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.hadoop.sequencefile; 031 032import java.util.Arrays; 033 034import org.apache.hadoop.io.SequenceFile; 035import org.openimaj.data.RandomData; 036 037/** 038 * The state describing the extraction process from {@link SequenceFile}. 039 * Basically, this class stores the current state of the extraction and 040 * determines whether the next item should be extracted or not. 041 * 042 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 044 */ 045public class ExtractionState { 046 private int count = 0; 047 private int max = -1; 048 private int[] randomInts = null; 049 050 /** 051 * @return the number of items that have been extracted 052 */ 053 public int getCount() { 054 return count; 055 } 056 057 /** 058 * @return true if the next record is allowed; false otherwise 059 */ 060 protected boolean allowNext() { 061 if (this.max != -1 && this.count >= this.max) 062 return false; 063 if (this.randomInts == null) 064 return true; 065 066 final int thingLocation = Arrays.binarySearch(randomInts, this.count); 067 068 return thingLocation >= 0; 069 } 070 071 /** 072 * @return true if the extraction is finished; i.e. the maximum number of 073 * records has been reached 074 */ 075 public boolean isFinished() { 076 return this.max != -1 && this.count >= this.max; 077 } 078 079 protected void tick() { 080 this.count++; 081 } 082 083 /** 084 * Instruct the state to extract number records from the file randomly. 085 * 086 * @param number 087 * number of records to extract 088 * @param total 089 * total number of records in the file 090 */ 091 public void setRandomSelection(int number, int total) { 092 this.randomInts = RandomData.getUniqueRandomInts(number, 0, total); 093 Arrays.sort(randomInts); 094 System.out.println(this.randomInts.length + " random ints selected"); 095 System.out.println(Arrays.toString(randomInts)); 096 } 097 098 /** 099 * Set the maximum allowed number of records to extract. 100 * 101 * @param max 102 * the number of records. 103 */ 104 public void setMaxFileExtract(int max) { 105 this.max = max; 106 } 107}