View Javadoc

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