hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1407244 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/config/Registry.java test/java/org/apache/http/config/ test/java/org/apache/http/config/TestRegistry.java
Date Thu, 08 Nov 2012 19:53:29 GMT
Author: olegk
Date: Thu Nov  8 19:53:28 2012
New Revision: 1407244

URL: http://svn.apache.org/viewvc?rev=1407244&view=rev
Log:
Added generic Registry class

Added:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
  (with props)
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
  (with props)

Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java?rev=1407244&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
Thu Nov  8 19:53:28 2012
@@ -0,0 +1,87 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.config;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.util.Args;
+
+/**
+ * Generic registry of items keyed by low-case string ID.
+ *
+ * @since 4.3
+ */
+@ThreadSafe
+public final class Registry<I> {
+
+    private final Map<String, I> map;
+
+    public static class Builder<I> {
+
+        final Map<String, I> items;
+
+        public Builder() {
+            super();
+            this.items = new HashMap<String, I>();
+        }
+
+        public Builder<I> register(final String id, final I item) {
+            Args.notEmpty(id, "ID");
+            Args.notNull(item, "Item");
+            items.put(id.toLowerCase(Locale.US), item);
+            return this;
+        }
+
+        public Registry<I> build() {
+            return new Registry<I>(items);
+        }
+
+    }
+
+    Registry(final Map<String, I> map) {
+        super();
+        this.map = new ConcurrentHashMap<String, I>(map);
+    }
+
+    public I lookup(final String key) {
+        if (key == null) {
+            return null;
+        }
+        return map.get(key.toLowerCase(Locale.US));
+    }
+
+    @Override
+    public String toString() {
+        return map.toString();
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/config/Registry.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java?rev=1407244&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
Thu Nov  8 19:53:28 2012
@@ -0,0 +1,44 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.config;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class TestRegistry {
+
+    @Test
+    public void testCompleted() throws Exception {
+        Registry<String> reg = new Registry.Builder<String>().register("Stuff",
"Stuff").build();
+        Assert.assertEquals("Stuff", reg.lookup("Stuff"));
+        Assert.assertEquals("Stuff", reg.lookup("stuff"));
+        Assert.assertEquals(null, reg.lookup("miss"));
+        Assert.assertEquals(null, reg.lookup(null));
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/config/TestRegistry.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message