// ---------------------------------------------------------------- // Sequence // ---------------------------------------------------------------- public class Sequence { // ---------------------------------------------------------------- // Sequence - Create a sequence of 'sizes.length' items, each // having the number of elements specified by // each entry in 'sizes' // ---------------------------------------------------------------- public Sequence(int[] sizes) { mSizes = sizes; mCurrent = new int[mSizes.length]; for(int i = 0; i < mCurrent.length; i ++) mCurrent[i] = 0; mFirst = true; } // ---------------------------------------------------------------- // Sequence - Create a sequence of 'count' items each having // 'size' entries. A car's odometer that goes from // 0000 -> 9999 can be simulated via creating a // Sequence with 'count' value of 4 and a 'size' // value of 10 // ---------------------------------------------------------------- public Sequence(int size, int count) { this(getSizes(size, count)); } // ---------------------------------------------------------------- // getNext - Return the next sequence value or null if there are // no more values to be returned (we've run through // the entirety of our Sequence) // ---------------------------------------------------------------- public int[] getNext() { if(mFirst) { mFirst = false; } else { int i = mCurrent.length - 1; while(i >= 0) { if(mCurrent[i] + 1 < mSizes[i]) { mCurrent[i]++; break; } mCurrent[i--] = 0; } if(i < 0) return(null); } return(mCurrent); } // ---------------------------------------------------------------- // getSizes - Construct an array of 'count' items with each // item set to 'size'. This function allows us to // feed our second constructor into our first, // more flexible/powerful one. // ---------------------------------------------------------------- private static int[] getSizes(int size, int count) { int[] values = new int[count]; for(int i = 0; i < values.length; i ++) values[i] = size; return(values); } // ---------------------------------------------------------------- // Member Variables // ---------------------------------------------------------------- private int[] mSizes; private int[] mCurrent; private boolean mFirst; }