All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.org.apache.calcite.linq4j.LookupImpl Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in com.hazelcast.com.liance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.com.hazelcast.org.licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.org.apache.calcite.linq4j;

import com.hazelcast.org.apache.calcite.linq4j.function.Function2;

import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Implementation of {@link Lookup} that uses an underlying map.
 *
 * @param  Key type
 * @param  Value type
 */
class LookupImpl extends AbstractEnumerable>
    implements Lookup {
  private final Map> map;

  /**
   * Creates a MultiMapImpl.
   *
   * @param map Underlying map
   */
  LookupImpl(Map> map) {
    this.map = map;
  }

  public Enumerator> enumerator() {
    return new Enumerator>() {
      Enumerator>> enumerator = Linq4j.enumerator(
          map.entrySet());

      public Grouping current() {
        final Entry> keyAndList = enumerator.current();
        return new GroupingImpl<>(keyAndList.getKey(),
            keyAndList.getValue());
      }

      public boolean moveNext() {
        return enumerator.moveNext();
      }

      public void reset() {
        enumerator.reset();
      }

      public void close() {
        enumerator.close();
      }
    };
  }

  // Map methods

  public int size() {
    return map.size();
  }

  public boolean isEmpty() {
    return map.isEmpty();
  }

  public boolean containsKey(Object key) {
    return map.containsKey(key);
  }

  public boolean containsValue(Object value) {
    @SuppressWarnings("unchecked")
    List list = (List) value;
    return map.containsValue(list);
  }

  public Enumerable get(Object key) {
    final List list = map.get(key);
    return list == null ? null : Linq4j.asEnumerable(list);
  }

  public Enumerable put(K key, Enumerable value) {
    final List list = map.put(key, value.toList());
    return list == null ? null : Linq4j.asEnumerable(list);
  }

  public Enumerable remove(Object key) {
    final List list = map.remove(key);
    return list == null ? null : Linq4j.asEnumerable(list);
  }

  public void putAll(Map> m) {
    for (Entry> entry : m.entrySet()) {
      map.put(entry.getKey(), entry.getValue().toList());
    }
  }

  public void clear() {
    map.clear();
  }

  public Set keySet() {
    return map.keySet();
  }

  public Collection> values() {
    final Collection> lists = map.values();
    return new AbstractCollection>() {
      public Iterator> iterator() {
        return new Iterator>() {
          final Iterator> iterator = lists.iterator();

          public boolean hasNext() {
            return iterator.hasNext();
          }

          public Enumerable next() {
            return Linq4j.asEnumerable(iterator.next());
          }

          public void remove() {
            iterator.remove();
          }
        };
      }

      public int size() {
        return lists.size();
      }
    };
  }

  public Set>> entrySet() {
    final Set>> entries = map.entrySet();
    return new AbstractSet>>() {
      public Iterator>> iterator() {
        final Iterator>> iterator = entries.iterator();
        return new Iterator>>() {
          public boolean hasNext() {
            return iterator.hasNext();
          }

          public Entry> next() {
            final Entry> entry = iterator.next();
            return new AbstractMap.SimpleEntry<>(entry.getKey(),
                Linq4j.asEnumerable(entry.getValue()));
          }

          public void remove() {
            iterator.remove();
          }
        };
      }

      public int size() {
        return entries.size();
      }
    };
  }

  public  Enumerable applyResultSelector(
      final Function2, TResult> resultSelector) {
    return new AbstractEnumerable() {
      public Enumerator enumerator() {
        final Enumerator> groupingEnumerator =
            LookupImpl.this.enumerator();
        return new Enumerator() {
          public TResult current() {
            final Grouping grouping = groupingEnumerator.current();
            return resultSelector.apply(grouping.getKey(), grouping);
          }

          public boolean moveNext() {
            return groupingEnumerator.moveNext();
          }

          public void reset() {
            groupingEnumerator.reset();
          }

          public void close() {
            groupingEnumerator.close();
          }
        };
      }
    };
  }

  /**
   * Returns an enumerable over the values in this lookup, in map order.
   * If the map is sorted, the values will be emitted sorted by key.
   */
  public Enumerable valuesEnumerable() {
    return new AbstractEnumerable() {
      public Enumerator enumerator() {
        final Enumerator> listEnumerator =
            Linq4j.iterableEnumerator(values());
        return new Enumerator() {
          Enumerator enumerator = Linq4j.emptyEnumerator();

          public V current() {
            return enumerator.current();
          }

          public boolean moveNext() {
            for (;;) {
              if (enumerator.moveNext()) {
                return true;
              }
              enumerator.close();
              if (!listEnumerator.moveNext()) {
                enumerator = Linq4j.emptyEnumerator();
                return false;
              }
              enumerator = listEnumerator.current().enumerator();
            }
          }

          public void reset() {
            listEnumerator.reset();
            enumerator = Linq4j.emptyEnumerator();
          }

          public void close() {
            enumerator.close();
          }
        };
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy