com.google.gwt.editor.client.impl.DelegateMap Maven / Gradle / Ivy
/*
* 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