[Schmitzm-commits] r1887 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Fri Mar 2 15:00:42 CET 2012
Author: mojays
Date: 2012-03-02 15:00:41 +0100 (Fri, 02 Mar 2012)
New Revision: 1887
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/SequentialOrderedMap.java
Log:
new SequentialOrderedMap: map which is ordered according to the sequence the elements are put in the map
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/SequentialOrderedMap.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/SequentialOrderedMap.java (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/SequentialOrderedMap.java 2012-03-02 14:00:41 UTC (rev 1887)
@@ -0,0 +1,122 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.lang;
+
+import java.util.Comparator;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.Vector;
+
+
+/**
+ * This class implements a map which elements are sorted according to the sequence the
+ * elements are added to the map (sequence of the {@link #put(Object, Object)} calls).<br>
+ * If {@link #put(Object, Object)} is called multiple times for the same key,
+ * this does not effect the ordering.<br>
+ * The {@link #remove(Object)} method must be called first to effect the ordering by
+ * a second {@link #put(Object, Object)} call on the same key.
+ * @author Martin O.J. Schmitz
+ *
+ */
+public class SequentialOrderedMap<K,V> extends TreeMap<K,V> {
+
+ /**
+ * Comparator which holds the map keys an a sorted list ({@link Vector}) and
+ * compares map keys according to the index in this list.
+ */
+ protected static class SequantialComparator<K> extends Vector<K> implements Comparator<K> {
+ @Override
+ public int compare(K o1, K o2) {
+ if ( o1 == null && o2 == null )
+ return 0;
+ if ( o1 != null && o2 != null && o1.equals(o2) )
+ return 0;
+ int idx1 = indexOf(o1);
+ int idx2 = indexOf(o2);
+ if ( idx1 < 0 && idx2 >= 0 )
+ return Integer.MAX_VALUE;
+ if ( idx2 < 0 && idx1 >= 0 )
+ return Integer.MIN_VALUE;
+ return idx1 - idx2;
+ }
+ }
+
+ /**
+ * Creates a new map.
+ */
+ public SequentialOrderedMap() {
+ super(new SequantialComparator<K>());
+ }
+
+ /**
+ * Creates a new map.
+ * @param map initial elements
+ */
+ public SequentialOrderedMap(Map<K,V> map) {
+ this();
+ putAll(map);
+ }
+
+ /**
+ * Returns the comparator of the map, which compares the elements
+ * according to sequence they were added to the map.
+ * @see SequantialComparator
+ */
+ @Override
+ public SequantialComparator<K> comparator() {
+ return (SequantialComparator<K>)super.comparator();
+ }
+
+ /**
+ * Adds an element to the map. If the key does not yet
+ * exists in the map it is arranged at the end of the order.
+ * If the key already exists the order is not changed!
+ * @param key element key
+ * @param value a value
+ */
+ @Override
+ public V put(K key, V value) {
+ if ( !comparator().contains(key) )
+ comparator().add(key);
+ return super.put(key, value);
+ }
+
+ /**
+ * Removes an element from the map and from the order. A subsequent
+ * {@link #put(Object, Object)} call will rearrange the key
+ * at the end of the order.
+ * @param key key of the element to remove
+ */
+ @Override
+ public V remove(Object key) {
+ comparator().remove(key);
+ return super.remove(key);
+ }
+}
More information about the Schmitzm-commits
mailing list