Skip to content
Snippets Groups Projects
Commit b40723a9 authored by felmer's avatar felmer
Browse files

move TableMap and IKeyExtractor from lims project to common

SVN: 2061
parent 12709379
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2007 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.collections;
/**
* Interface defining the role of a key extractor.
*
* @author Franz-Josef Elmer
*/
public interface IKeyExtractor<K, E>
{
/**
* Returns the key of type <code>K</code> from an entity <code>E</code>.
*/
public K getKey(E e);
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
*
* @author Franz-Josef Elmer
*/
public class TableMap<K, E> implements Iterable<E>
{
private final Map<K, E> map = new LinkedHashMap<K, E>();
private final IKeyExtractor<K, E> extractor;
public TableMap(Collection<E> rows, IKeyExtractor<K, E> extractor)
{
this.extractor = extractor;
for (E row : rows)
{
add(row);
}
}
public void add(E row)
{
map.put(extractor.getKey(row), row);
}
public E get(K key)
{
return map.get(key);
}
public Iterator<E> iterator()
{
return new Iterator<E>()
{
private Iterator<Map.Entry<K, E>> iterator = map.entrySet().iterator();
public boolean hasNext()
{
return iterator.hasNext();
}
public E next()
{
return iterator.next().getValue();
}
public void remove()
{
throw new UnsupportedOperationException("Can not remove an element.");
}
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment