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

com.google.gwt.editor.client.impl.DelegateMap Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2010 Google Inc.
 * 
 * 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 com.google.gwt.editor.client.impl;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.EditorContext;
import com.google.gwt.editor.client.EditorDriver;
import com.google.gwt.editor.client.EditorVisitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Allows fast traversal of an Editor hierarchy.
 */
public class DelegateMap implements Iterable> {
  /**
   * Defines an equivalence relationship to allow objects with non-identity
   * equality to be used as data keys.
   */
  public interface KeyMethod {
    Object key(Object object);
  }

  private static class MapIterator implements
      Iterator> {
    private AbstractEditorDelegate next;
    private Iterator> list;
    private Iterator>> values;

    public MapIterator(DelegateMap map) {
      values = map.map.values().iterator();
      next();
    }

    public boolean hasNext() {
      return next != null;
    }

    public AbstractEditorDelegate next() {
      AbstractEditorDelegate toReturn = next;

      if (list != null && list.hasNext()) {
        // Simple case, just advance the pointer
        next = list.next();
      } else {
        // Uninitialized, or current list exhausted
        next = null;
        while (values.hasNext()) {
          // Find the next non-empty iterator
          list = values.next().iterator();
          if (list.hasNext()) {
            next = list.next();
            break;
          }
        }
      }
      return toReturn;
    }

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

  public static final KeyMethod IDENTITY = new KeyMethod() {
    public Object key(Object object) {
      return object;
    }
  };

  public static DelegateMap of(EditorDriver driver, KeyMethod key) {
    final DelegateMap toReturn = new DelegateMap(key);
    driver.accept(new EditorVisitor() {
      @Override
      public  void endVisit(EditorContext ctx) {
        toReturn.put(ctx.getAbsolutePath(), ctx.getEditor());
        @SuppressWarnings("unchecked")
        AbstractEditorDelegate delegate = (AbstractEditorDelegate) ctx.getEditorDelegate();
        if (delegate != null) {
          toReturn.put(delegate.getObject(), delegate);
        }
      }
    });
    return toReturn;
  }

  private final Map>> map = new HashMap>>();
  private final Map>> delegatesByPath = new HashMap>>();
  private final Map>> editorsByPath = new HashMap>>();

  private final KeyMethod keyMethod;

  DelegateMap(KeyMethod key) {
    this.keyMethod = key;
  }

  public List> get(Object object) {
    Object key = keyMethod.key(object);
    return key == null ? null : map.get(key);
  }

  /**
   * Returns a list of EditorDelegates available at a particular absolute path.
   */
  public List> getDelegatesByPath(String path) {
    return delegatesByPath.get(path);
  }

  /**
   * Returns a list of Editors available at a particular absolute path.
   */
  public List> getEditorByPath(String path) {
    return editorsByPath.get(path);
  }

  /**
   * Accesses the delegate map without using the KeyMethod.
   */
  public List> getRaw(Object key) {
    return map.get(key);
  }

  public Iterator> iterator() {
    return new MapIterator(this);
  }

   void add(Map> map, K key, V value) {
    List list = map.get(key);
    if (list == null) {
      list = new ArrayList();
      map.put(key, list);
    }
    list.add(value);
  }

   void put(String path, Editor editor) {
    add(editorsByPath, path, editor);
  }

   void put(T object, AbstractEditorDelegate delegate) {
    add(delegatesByPath, delegate.getPath(), delegate);

    Object key = keyMethod.key(object);
    if (key == null) {
      return;
    }

    add(map, key, delegate);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy