| /******************************************************************************* |
| * Copyright (c) 2004-2008 Akos Horvath, Gergely Varro and Daniel Varro |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Akos Horvath, Gergely Varro - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.rgg;
|
|
|
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.LinkedList; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.NoSuchElementException; |
| |
| import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.IKeyGenerator; |
|
|
|
|
| public class HistoryList<KeyType, ValueType> extends LinkedList<ValueType> {
|
| private static final long serialVersionUID = 4033836249907829630L;
|
| private IKeyGenerator<KeyType, ValueType> keyGenerator;
|
| private Map<KeyType, List<ValueType>> map;
|
|
|
| public HistoryList(IKeyGenerator<KeyType, ValueType> keyGenerator) {
|
| this.keyGenerator = keyGenerator;
|
| this.map = new HashMap<KeyType, List<ValueType>>();
|
| }
|
|
|
| public List<ValueType> get(KeyType key) {
|
| return map.get(key);
|
| }
|
|
|
| public void init() {
|
| clear();
|
| map.clear();
|
| }
|
|
|
| public Iterator<ValueType> iterator() {
|
| return new Itr(super.iterator());
|
| }
|
|
|
| private class Itr implements Iterator<ValueType> {
|
| private Iterator<ValueType> backingIterator;
|
| private ValueType lastReturned;
|
|
|
| private Itr(Iterator<ValueType> i) {
|
| backingIterator = i;
|
| lastReturned = null;
|
| }
|
|
|
| public boolean hasNext() {
|
| return backingIterator.hasNext();
|
| }
|
|
|
| public ValueType next() {
|
| try {
|
| lastReturned = backingIterator.next();
|
| return lastReturned;
|
| } catch (NoSuchElementException e) {
|
| lastReturned = null;
|
| throw e;
|
| }
|
| }
|
|
|
| public void remove() {
|
| if (lastReturned != null) {
|
| KeyType key = keyGenerator.calculateKey(lastReturned);
|
| List<ValueType> list = map.get(key);
|
| if (list == null) {
|
| list = new LinkedList<ValueType>();
|
| }
|
| list.add(lastReturned);
|
| map.put(key, list);
|
| backingIterator.remove();
|
| lastReturned = null;
|
| } else {
|
| throw new IllegalStateException();
|
| }
|
| }
|
| }
|
| }
|