View Javadoc
1 /* 2 * Copyright (c) 2003, Henri Yandell 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or 6 * without modification, are permitted provided that the 7 * following conditions are met: 8 * 9 * + Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * + Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * + Neither the name of Genjava-Core nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.generationjava.collections; 33 34 import java.util.ArrayList; 35 import java.util.Iterator; 36 import java.util.Collections; 37 import java.util.Comparator; 38 39 import com.generationjava.compare.ObjectComparator; 40 41 /*** 42 * An Iterator which can be sorted. 43 */ 44 public class SortedIterator implements Iterator { 45 46 private ArrayList cache; 47 private Iterator iterator; 48 private boolean started; 49 50 public SortedIterator(Iterator iterator) { 51 this.cache = new ArrayList(); 52 int idx = 0; 53 while(iterator.hasNext()) { 54 this.cache.add( new SortIndex(idx, iterator.next()) ); 55 idx++; 56 } 57 if(this.iterator == null) { 58 this.iterator = cache.iterator(); 59 } 60 } 61 62 /// Start of Iterator 63 public Object next() { 64 this.started = true; 65 return ((SortIndex)this.iterator.next()).getIndexed(); 66 } 67 68 public boolean hasNext() { 69 return this.iterator.hasNext(); 70 } 71 72 public void remove() { 73 throw new UnsupportedOperationException("Unable to remove once it is sorted. "); 74 } 75 /// End of Iterator 76 77 public void sort(Comparator cmp) { 78 if(this.started) { 79 throw new RuntimeException("Cannot sort as this iterator has been read from. "); 80 } 81 SortComparator srtr = new SortComparator(cmp); 82 Collections.sort(cache, srtr); 83 if(this.iterator == null) { 84 this.iterator = cache.iterator(); 85 } 86 } 87 88 public void sort() { 89 sort( new ObjectComparator() ); 90 } 91 92 93 } 94 95 class SortIndex { 96 97 private int idx; 98 private Object object; 99 100 public SortIndex(int idx, Object object) { 101 this.idx = idx; 102 this.object = object; 103 } 104 105 public int getIndex() { 106 return this.idx; 107 } 108 109 public Object getIndexed() { 110 return this.object; 111 } 112 113 } 114 115 class SortComparator implements java.util.Comparator { 116 117 private Comparator cmp; 118 119 public SortComparator(Comparator cmp) { 120 this.cmp = cmp; 121 } 122 123 public int compare(Object o1, Object o2) { 124 SortIndex si1 = (SortIndex)o1; 125 SortIndex si2 = (SortIndex)o2; 126 o1 = si1.getIndexed(); 127 o2 = si2.getIndexed(); 128 return this.cmp.compare(o1,o2); 129 } 130 131 }

This page was automatically generated by Maven