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

juzu.impl.common.SimpleMap Maven / Gradle / Ivy

/*
 * Copyright 2013 eXo Platform SAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.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 juzu.impl.common;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Make implementation of maps easier. Note that this map is not optimized for speed on all operations, the goal
 * of this map is to make easy the implementation of maps.
 *
 * @author Julien Viet
 */
public abstract class SimpleMap extends AbstractMap {

  protected abstract Iterator keys();

  @Override
  public abstract V get(Object key);

  @Override
  public final boolean containsKey(Object key) {
    return get(key) != null;
  }

  @Override
  public final Set> entrySet() {
    return entries;
  }

  private AbstractSet> entries = new AbstractSet>() {

    @Override
    public Iterator> iterator() {
      final Iterator names = keys();
      return new Iterator>() {
        public boolean hasNext() {
          return names.hasNext();
        }

        public Entry next() {
          final K name = names.next();
          return new Entry() {
            public K getKey() {
              return name;
            }

            public V getValue() {
              return get(name);
            }

            public V setValue(V value) {
              throw new UnsupportedOperationException();
            }
          };
        }

        public void remove() {
          throw new UnsupportedOperationException();
        }
      };
    }

    @Override
    public int size() {
      int size = 0;
      for (Iterator names = keys();names.hasNext();) {
        size++;
      }
      return size;
    }
  };
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy