[Mulgara-svn] r537 - branches/mgr-73/src/jar/util/java/org/mulgara/util

andrae at mulgara.org andrae at mulgara.org
Wed Nov 14 08:15:59 UTC 2007


Author: andrae
Date: 2007-11-14 02:15:58 -0600 (Wed, 14 Nov 2007)
New Revision: 537

Added:
   branches/mgr-73/src/jar/util/java/org/mulgara/util/Assoc1toNMap.java
Log:
Forgot to include this utility file.

Provides a 1:N associative map.



Added: branches/mgr-73/src/jar/util/java/org/mulgara/util/Assoc1toNMap.java
===================================================================
--- branches/mgr-73/src/jar/util/java/org/mulgara/util/Assoc1toNMap.java	2007-11-14 08:13:10 UTC (rev 536)
+++ branches/mgr-73/src/jar/util/java/org/mulgara/util/Assoc1toNMap.java	2007-11-14 08:15:58 UTC (rev 537)
@@ -0,0 +1,91 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.rosenlaw.com/OSL3.0.htm
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * This file is an original work developed by Netymon Pty Ltd
+ * (http://www.netymon.com, mailto:mail at netymon.com). Portions created
+ * by Netymon Pty Ltd are Copyright (c) 2006 Netymon Pty Ltd.
+ * All Rights Reserved.
+ */
+
+package org.mulgara.util;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @created 2007-11-12
+ *
+ * @author <a href="mailto:andrae at netymon.com">Andrae Muys</a>
+ *
+ * @company <A href="mailto:mail at netymon.com">Netymon Pty Ltd</A>
+ *
+ * @copyright &copy;2007 <a href="http://www.topazproject.org/">Topaz Project</a>
+ *
+ * @licence Open Software License v3.0</a>
+ */
+
+public class Assoc1toNMap<T1,T2> implements Iterable<T1> {
+  private Map<T1, Set<T2>> map1toN;
+  private Map<T2, T1> mapNto1;
+
+  public Assoc1toNMap() {
+    map1toN = new HashMap<T1, Set<T2>>();
+    mapNto1 = new HashMap<T2, T1>();
+  }
+
+  public T1 get1(T2 t2) {
+    return mapNto1.get(t2);
+  }
+
+  public Set<T2> getN(T1 t1) {
+    return map1toN.get(t1);
+  }
+
+  public void put(T1 t1, T2 t2) {
+    Set<T2> t2set = getN(t1);
+    if (t2set == null) {
+      t2set = new HashSet<T2>();
+      map1toN.put(t1, t2set);
+    }
+    t2set.add(t2);
+    
+    mapNto1.put(t2, t1);
+  }
+
+  public void remove1(T1 t1) {
+    Set<T2> t2set = map1toN.remove(t1);
+    for (T2 t2 : t2set) {
+      mapNto1.remove(t2);
+    }
+  }
+
+  public void removeN(T2 t2) {
+    T1 t1 = mapNto1.remove(t2);
+    Set<T2> t2set = map1toN.get(t1);
+    t2set.remove(t2);
+    if (t2set.isEmpty()) {
+      map1toN.remove(t1);
+    }
+  }
+
+  public boolean contains1(T1 t1) {
+    return map1toN.containsKey(t1);
+  }
+
+  public boolean containsN(T2 t2) {
+    return mapNto1.containsKey(t2);
+  }
+
+  public Iterator<T1> iterator() {
+    return map1toN.keySet().iterator();
+}




More information about the Mulgara-svn mailing list