You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by om...@apache.org on 2011/03/04 05:21:58 UTC

svn commit: r1077508 - in /hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util: GSet.java GSetByHashMap.java

Author: omalley
Date: Fri Mar  4 04:21:58 2011
New Revision: 1077508

URL: http://svn.apache.org/viewvc?rev=1077508&view=rev
Log:
commit 4908496f6689efc67cf17e2ed8a6b6f0fa67cae1
Author: Tsz Wo Wo Sze <ts...@ucdev29.inktomisearch.com>
Date:   Wed Jun 23 16:27:38 2010 +0000

    HDFS-1119: from https://issues.apache.org/jira/secure/attachment/12447508/h1119_20100525_y0.20.1xx.patch

Added:
    hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSet.java
    hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSetByHashMap.java

Added: hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSet.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSet.java?rev=1077508&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSet.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSet.java Fri Mar  4 04:21:58 2011
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.hdfs.util;
+
+/**
+ * A {@link GSet} is set,
+ * which supports the {@link #get(Object)} operation.
+ * The {@link #get(Object)} operation uses a key to lookup an element.
+ * 
+ * Null element is not supported.
+ * 
+ * @param <K> The type of the keys.
+ * @param <E> The type of the elements, which must be a subclass of the keys.
+ */
+public interface GSet<K, E extends K> extends Iterable<E> {
+  /**
+   * @return The size of this set.
+   */
+  int size();
+
+  /**
+   * Does this set contain an element corresponding to the given key?
+   * @param key The given key.
+   * @return true if the given key equals to a stored element.
+   *         Otherwise, return false.
+   */
+  boolean contains(K key);
+
+  /**
+   * Return the stored element which is equal to the given key.
+   * This operation is similar to {@link java.util.Map#get(Object)}.
+   * @param key The given key.
+   * @return The stored element if it exists.
+   *         Otherwise, return null.
+   */
+  E get(K key);
+
+  /**
+   * Add/replace an element.
+   * If the element does not exist, add it to the set.
+   * Otherwise, replace the existing element.
+   *
+   * Note that this operation
+   * is similar to {@link java.util.Map#put(Object, Object)}
+   * but is different from {@link java.util.Set#add(Object)}
+   * which does not replace the existing element if there is any.
+   *
+   * @param element The element being put.
+   * @return the previous stored element if there is any.
+   *         Otherwise, return null.
+   */
+  E put(E element);
+
+  /**
+   * Remove the element corresponding to the given key. 
+   * This operation is similar to {@link java.util.Map#remove(Object)}.
+   * @param key The key of the element being removed.
+   * @return If such element exists, return it.
+   *         Otherwise, return null. 
+   */
+  E remove(K key);
+}
\ No newline at end of file

Added: hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSetByHashMap.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSetByHashMap.java?rev=1077508&view=auto
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSetByHashMap.java (added)
+++ hadoop/common/branches/branch-0.20-security-patches/src/hdfs/org/apache/hadoop/hdfs/util/GSetByHashMap.java Fri Mar  4 04:21:58 2011
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.hdfs.util;
+
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * A {@link GSet} implementation by {@link HashMap}.
+ */
+public class GSetByHashMap<K, E extends K> implements GSet<K, E> {
+  private final HashMap<K, E> m;
+
+  public GSetByHashMap(int initialCapacity, float loadFactor) {
+    m = new HashMap<K, E>(initialCapacity, loadFactor);
+  }
+
+  @Override
+  public int size() {
+    return m.size();
+  }
+
+  @Override
+  public boolean contains(K k) {
+    return m.containsKey(k);
+  }
+
+  @Override
+  public E get(K k) {
+    return m.get(k);
+  }
+
+  @Override
+  public E put(E element) {
+    if (element == null) {
+      throw new UnsupportedOperationException("Null element is not supported.");
+    }
+    return m.put(element, element);
+  }
+
+  @Override
+  public E remove(K k) {
+    return m.remove(k);
+  }
+
+  @Override
+  public Iterator<E> iterator() {
+    return m.values().iterator();
+  }
+}