View Javadoc

    1   package sharin.unlinq;
    2
    3   import java.util.ArrayList;
    4   import java.util.Collection;
    5   import java.util.Collections;
    6   import java.util.Comparator;
    7   import java.util.Iterator;
    8   import java.util.List;
    9   import java.util.Map;
    10  import java.util.TreeMap;
    11
    12  public class BasicOrderedEnumerable<T> implements OrderedEnumerable<T> {
    13
    14      private final Enumerable<T> enumerable;
    15
    16      private final List<List<T>> listList;
    17
    18      public <K> BasicOrderedEnumerable(Enumerable<T> enumerable,
    19              Func<T, K> keySelector, boolean descending) {
    20
    21          this(enumerable, keySelector, descending, null);
    22      }
    23
    24      public <K> BasicOrderedEnumerable(Enumerable<T> enumerable,
    25              Func<T, K> keySelector, boolean descending, Comparator<K> comparator) {
    26
    27          List<List<T>> listList = null;
    28
    29          if (enumerable instanceof BasicOrderedEnumerable<?>) {
    30              listList = apply(toBasicOrderedEnumerable(enumerable).listList,
    31                      keySelector, descending, comparator);
    32
    33          } else {
    34              listList = new ArrayList<List<T>>();
    35              listList.add(enumerable.toList());
    36              listList = apply(listList, keySelector, descending, comparator);
    37          }
    38
    39          this.listList = listList;
    40
    41          List<T> all = new ArrayList<T>();
    42
    43          for (List<T> list : listList) {
    44              all.addAll(list);
    45          }
    46
    47          this.enumerable = new BasicEnumerable<T>(all);
    48      }
    49
    50      private BasicOrderedEnumerable<T> toBasicOrderedEnumerable(
    51              Enumerable<T> enumerable) {
    52
    53          return (BasicOrderedEnumerable<T>) enumerable;
    54      }
    55
    56      private <K> List<List<T>> apply(List<List<T>> prevListList,
    57              Func<T, K> keySelector, boolean descending, Comparator<K> comparator) {
    58
    59          List<List<T>> currListList = new ArrayList<List<T>>();
    60
    61          for (List<T> prevList : prevListList) {
    62              TreeMap<K, List<T>> map = createTreeMap(comparator);
    63
    64              for (T entity : prevList) {
    65                  K key = keySelector.call(entity);
    66                  List<T> currList = map.get(key);
    67
    68                  if (currList == null) {
    69                      currList = new ArrayList<T>();
    70                      map.put(key, currList);
    71                  }
    72
    73                  currList.add(entity);
    74              }
    75
    76              Collection<List<T>> values = map.values();
    77
    78              if (descending) {
    79                  List<List<T>> list = new ArrayList<List<T>>(values);
    80                  Collections.reverse(list);
    81                  values = list;
    82              }
    83
    84              currListList.addAll(values);
    85          }
    86
    87          return currListList;
    88      }
    89
    90      private <K> TreeMap<K, List<T>> createTreeMap(Comparator<K> comparator) {
    91
    92          if (comparator == null) {
    93              return new TreeMap<K, List<T>>();
    94          }
    95
    96          return new TreeMap<K, List<T>>(comparator);
    97      }
    98
    99      public <K> OrderedEnumerable<T> thenBy(Func<T, K> keySelector) {
    100         return new BasicOrderedEnumerable<T>(this, keySelector, false);
    101     }
    102
    103     public <K> OrderedEnumerable<T> thenBy(Func<T, K> keySelector,
    104             Comparator<K> comparator) {
    105
    106         return new BasicOrderedEnumerable<T>(this, keySelector, false,
    107                 comparator);
    108     }
    109
    110     public <K> OrderedEnumerable<T> thenByDescending(Func<T, K> keySelector) {
    111         return new BasicOrderedEnumerable<T>(this, keySelector, true);
    112     }
    113
    114     public <K> OrderedEnumerable<T> thenByDescending(Func<T, K> keySelector,
    115             Comparator<K> comparator) {
    116
    117         return new BasicOrderedEnumerable<T>(this, keySelector, true,
    118                 comparator);
    119     }
    120
    121     public <A, R> R aggregate(A seed, Func2<A, T, A> func,
    122             Func<A, R> resultSelector) {
    123         return enumerable.aggregate(seed, func, resultSelector);
    124     }
    125
    126     public <A> A aggregate(A seed, Func2<A, T, A> func) {
    127         return enumerable.aggregate(seed, func);
    128     }
    129
    130     public T aggregate(Func2<T, T, T> func) {
    131         return enumerable.aggregate(func);
    132     }
    133
    134     public Boolean all(Func<T, Boolean> predicate) {
    135         return enumerable.all(predicate);
    136     }
    137
    138     public Boolean any() {
    139         return enumerable.any();
    140     }
    141
    142     public Boolean any(Func<T, Boolean> predicate) {
    143         return enumerable.any(predicate);
    144     }
    145
    146     public Enumerable<T> concat(Enumerable<T> second) {
    147         return enumerable.concat(second);
    148     }
    149
    150     public boolean contains(Object value) {
    151         return enumerable.contains(value);
    152     }
    153
    154     public int count() {
    155         return enumerable.count();
    156     }
    157
    158     public int count(Func<T, Boolean> predicate) {
    159         return enumerable.count(predicate);
    160     }
    161
    162     public Enumerable<T> defaultIfEmpty() {
    163         return enumerable.defaultIfEmpty();
    164     }
    165
    166     public Enumerable<T> defaultIfEmpty(T defaultValue) {
    167         return enumerable.defaultIfEmpty(defaultValue);
    168     }
    169
    170     public Enumerable<T> distinct() {
    171         return enumerable.distinct();
    172     }
    173
    174     public T elementAt(int index) {
    175         return enumerable.elementAt(index);
    176     }
    177
    178     public T elementAtOrDefault(int index) {
    179         return enumerable.elementAtOrDefault(index);
    180     }
    181
    182     public Enumerable<T> except(Enumerable<T> second) {
    183         return enumerable.except(second);
    184     }
    185
    186     public T first() {
    187         return enumerable.first();
    188     }
    189
    190     public T first(Func<T, Boolean> predicate) {
    191         return enumerable.first(predicate);
    192     }
    193
    194     public T firstOrDefault() {
    195         return enumerable.firstOrDefault();
    196     }
    197
    198     public T firstOrDefault(Func<T, Boolean> predicate) {
    199         return enumerable.firstOrDefault(predicate);
    200     }
    201
    202     public <K, E, R> Enumerable<R> groupBy(Func<T, K> keySelector,
    203             Func<T, E> elementSelector,
    204             Func2<K, Enumerable<E>, R> resultSelector) {
    205
    206         return enumerable.groupBy(keySelector, elementSelector, resultSelector);
    207     }
    208
    209     public <K, E> Enumerable<Grouping<K, E>> groupBy(Func<T, K> keySelector,
    210             Func<T, E> elementSelector) {
    211
    212         return enumerable.groupBy(keySelector, elementSelector);
    213     }
    214
    215     public <K, R> Enumerable<R> groupBy(Func<T, K> keySelector,
    216             Func2<K, Enumerable<T>, R> resultSelector) {
    217         return enumerable.groupBy(keySelector, resultSelector);
    218     }
    219
    220     public <K> Enumerable<Grouping<K, T>> groupBy(Func<T, K> keySelector) {
    221         return enumerable.groupBy(keySelector);
    222     }
    223
    224     public <I, K, R> Enumerable<R> groupJoin(Enumerable<I> inner,
    225             Func<T, K> outerKeySelector, Func<I, K> innerKeySelector,
    226             Func2<T, Enumerable<I>, R> resultSelector) {
    227
    228         return enumerable.groupJoin(inner, outerKeySelector, innerKeySelector,
    229                 resultSelector);
    230     }
    231
    232     public Enumerable<T> intersect(Enumerable<T> second) {
    233         return enumerable.intersect(second);
    234     }
    235
    236     public Iterator<T> iterator() {
    237         return enumerable.iterator();
    238     }
    239
    240     public <I, K, R> Enumerable<R> join(Enumerable<I> inner,
    241             Func<T, K> outerKeySelector, Func<I, K> innerKeySelector,
    242             Func2<T, I, R> resultSelector) {
    243         return enumerable.join(inner, outerKeySelector, innerKeySelector,
    244                 resultSelector);
    245     }
    246
    247     public T last() {
    248         return enumerable.last();
    249     }
    250
    251     public T last(Func<T, Boolean> predicate) {
    252         return enumerable.last(predicate);
    253     }
    254
    255     public T lastOrDefault() {
    256         return enumerable.lastOrDefault();
    257     }
    258
    259     public T lastOrDefault(Func<T, Boolean> predicate) {
    260         return enumerable.lastOrDefault(predicate);
    261     }
    262
    263     public long longCount() {
    264         return enumerable.longCount();
    265     }
    266
    267     public long longCount(Func<T, Boolean> predicate) {
    268         return enumerable.longCount(predicate);
    269     }
    270
    271     public T max() {
    272         return enumerable.max();
    273     }
    274
    275     public <R extends Comparable<R>> R max(Func<T, R> selector) {
    276         return enumerable.max(selector);
    277     }
    278
    279     public T min() {
    280         return enumerable.min();
    281     }
    282
    283     public <R extends Comparable<R>> R min(Func<T, R> selector) {
    284         return enumerable.min(selector);
    285     }
    286
    287     public <R> Enumerable<R> ofType(Class<R> resultClass) {
    288         return enumerable.ofType(resultClass);
    289     }
    290
    291     public <K> OrderedEnumerable<T> orderBy(Func<T, K> keySelector,
    292             Comparator<K> comparator) {
    293         return enumerable.orderBy(keySelector, comparator);
    294     }
    295
    296     public <K> OrderedEnumerable<T> orderBy(Func<T, K> keySelector) {
    297         return enumerable.orderBy(keySelector);
    298     }
    299
    300     public <K> OrderedEnumerable<T> orderByDescending(Func<T, K> keySelector,
    301             Comparator<K> comparator) {
    302         return enumerable.orderByDescending(keySelector, comparator);
    303     }
    304
    305     public <K> OrderedEnumerable<T> orderByDescending(Func<T, K> keySelector) {
    306         return enumerable.orderByDescending(keySelector);
    307     }
    308
    309     public Enumerable<T> reverse() {
    310         return enumerable.reverse();
    311     }
    312
    313     public <R> Enumerable<R> select(Func<T, R> selector) {
    314         return enumerable.select(selector);
    315     }
    316
    317     public <R> Enumerable<R> select(Func2<T, Integer, R> selector) {
    318         return enumerable.select(selector);
    319     }
    320
    321     public <C, R> Enumerable<R> selectMany(
    322             Func<T, Enumerable<C>> collectionSelector,
    323             Func2<T, C, R> resultSelector) {
    324         return enumerable.selectMany(collectionSelector, resultSelector);
    325     }
    326
    327     public <R> Enumerable<R> selectMany(Func<T, Enumerable<R>> selector) {
    328         return enumerable.selectMany(selector);
    329     }
    330
    331     public <C, R> Enumerable<R> selectMany(
    332             Func2<T, Integer, Enumerable<C>> collectionSelector,
    333             Func2<T, C, R> resultSelector) {
    334         return enumerable.selectMany(collectionSelector, resultSelector);
    335     }
    336
    337     public <R> Enumerable<R> selectMany(
    338             Func2<T, Integer, Enumerable<R>> selector) {
    339         return enumerable.selectMany(selector);
    340     }
    341
    342     public Boolean sequenceEqual(Enumerable<T> second) {
    343         return enumerable.sequenceEqual(second);
    344     }
    345
    346     public T single() {
    347         return enumerable.single();
    348     }
    349
    350     public T single(Func<T, Boolean> predicate) {
    351         return enumerable.single(predicate);
    352     }
    353
    354     public T singleOrDefault() {
    355         return enumerable.singleOrDefault();
    356     }
    357
    358     public T singleOrDefault(Func<T, Boolean> predicate) {
    359         return enumerable.singleOrDefault(predicate);
    360     }
    361
    362     public Enumerable<T> skip(int count) {
    363         return enumerable.skip(count);
    364     }
    365
    366     public Enumerable<T> skipWhile(Func<T, Boolean> predicate) {
    367         return enumerable.skipWhile(predicate);
    368     }
    369
    370     public Enumerable<T> skipWhile(Func2<T, Integer, Boolean> predicate) {
    371         return enumerable.skipWhile(predicate);
    372     }
    373
    374     public int sum() {
    375         return enumerable.sum();
    376     }
    377
    378     public int sum(Func<T, Integer> selector) {
    379         return enumerable.sum(selector);
    380     }
    381
    382     public double sumDouble() {
    383         return enumerable.sumDouble();
    384     }
    385
    386     public double sumDouble(Func<T, Double> selector) {
    387         return enumerable.sumDouble(selector);
    388     }
    389
    390     public float sumFloat() {
    391         return enumerable.sumFloat();
    392     }
    393
    394     public float sumFloat(Func<T, Float> selector) {
    395         return enumerable.sumFloat(selector);
    396     }
    397
    398     public long sumLong() {
    399         return enumerable.sumLong();
    400     }
    401
    402     public long sumLong(Func<T, Long> selector) {
    403         return enumerable.sumLong(selector);
    404     }
    405
    406     public Enumerable<T> take(int count) {
    407         return enumerable.take(count);
    408     }
    409
    410     public Enumerable<T> takeWhile(Func<T, Boolean> predicate) {
    411         return enumerable.takeWhile(predicate);
    412     }
    413
    414     public Enumerable<T> takeWhile(Func2<T, Integer, Boolean> predicate) {
    415         return enumerable.takeWhile(predicate);
    416     }
    417
    418     public Object[] toArray() {
    419         return enumerable.toArray();
    420     }
    421
    422     public <R> R[] toArray(Class<R> resultClass) {
    423         return enumerable.toArray(resultClass);
    424     }
    425
    426     public List<T> toList() {
    427         return enumerable.toList();
    428     }
    429
    430     public EnumerableList<T> toEnumerableList() {
    431         return enumerable.toEnumerableList();
    432     }
    433
    434     public <K, E> Lookup<K, E> toLookup(Func<T, K> keySelector,
    435             Func<T, E> elementSelector) {
    436         return enumerable.toLookup(keySelector, elementSelector);
    437     }
    438
    439     public <K> Lookup<K, T> toLookup(Func<T, K> keySelector) {
    440         return enumerable.toLookup(keySelector);
    441     }
    442
    443     public <K, E> Dictionary<K, E> toDictionary(Func<T, K> keySelector,
    444             Func<T, E> elementSelector) {
    445         return enumerable.toDictionary(keySelector, elementSelector);
    446     }
    447
    448     public <K> Dictionary<K, T> toDictionary(Func<T, K> keySelector) {
    449         return enumerable.toDictionary(keySelector);
    450     }
    451
    452     public Enumerable<T> union(Enumerable<T> second) {
    453         return enumerable.union(second);
    454     }
    455
    456     public Enumerable<T> where(Func<T, Boolean> predicate) {
    457         return enumerable.where(predicate);
    458     }
    459
    460     public Enumerable<T> where(Func2<T, Integer, Boolean> predicate) {
    461         return enumerable.where(predicate);
    462     }
    463
    464     public double average() {
    465         return enumerable.average();
    466     }
    467
    468     public double average(Func<T, Integer> selector) {
    469         return enumerable.average(selector);
    470     }
    471
    472     public double averageDouble() {
    473         return enumerable.averageDouble();
    474     }
    475
    476     public double averageDouble(Func<T, Double> selector) {
    477         return enumerable.averageDouble(selector);
    478     }
    479
    480     public float averageFloat() {
    481         return enumerable.averageFloat();
    482     }
    483
    484     public float averageFloat(Func<T, Float> selector) {
    485         return enumerable.averageFloat(selector);
    486     }
    487
    488     public double averageLong() {
    489         return enumerable.averageLong();
    490     }
    491
    492     public double averageLong(Func<T, Long> selector) {
    493         return enumerable.averageLong(selector);
    494     }
    495
    496     public T firstOrDefault(Func<T, Boolean> predicate, T defaultValue) {
    497         return enumerable.firstOrDefault(predicate, defaultValue);
    498     }
    499
    500     public T firstOrDefault(T defaultValue) {
    501         return enumerable.firstOrDefault(defaultValue);
    502     }
    503
    504     public T lastOrDefault(Func<T, Boolean> predicate, T defaultValue) {
    505         return enumerable.lastOrDefault(predicate, defaultValue);
    506     }
    507
    508     public T lastOrDefault(T defaultValue) {
    509         return enumerable.lastOrDefault(defaultValue);
    510     }
    511
    512     public T singleOrDefault(Func<T, Boolean> predicate, T defaultValue) {
    513         return enumerable.singleOrDefault(predicate, defaultValue);
    514     }
    515
    516     public T singleOrDefault(T defaultValue) {
    517         return enumerable.singleOrDefault(defaultValue);
    518     }
    519
    520     public T elementAtOrDefault(int index, T defaultValue) {
    521         return enumerable.elementAtOrDefault(index, defaultValue);
    522     }
    523
    524     public <R> Enumerable<R> cast(Class<R> resultClass) {
    525         return enumerable.cast(resultClass);
    526     }
    527
    528     public <K, E> Map<K, E> toMap(Func<T, K> keySelector,
    529             Func<T, E> elementSelector) {
    530         return enumerable.toMap(keySelector, elementSelector);
    531     }
    532
    533     public <K> Map<K, T> toMap(Func<T, K> keySelector) {
    534         return enumerable.toMap(keySelector);
    535     }
    536
    537     public <K, E> Map<K, List<E>> toListMap(Func<T, K> keySelector,
    538             Func<T, E> elementSelector) {
    539         return enumerable.toListMap(keySelector, elementSelector);
    540     }
    541
    542     public <K> Map<K, List<T>> toListMap(Func<T, K> keySelector) {
    543         return enumerable.toListMap(keySelector);
    544     }
    545 }