Added: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/concurrentlinkedhashmap/Weighers.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/concurrentlinkedhashmap/Weighers.java?rev=1292435&view=auto
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/concurrentlinkedhashmap/Weighers.java
 (added)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/util/concurrentlinkedhashmap/Weighers.java
 Wed Feb 22 18:31:39 2012
@@ -0,0 +1,245 @@
+/*****************************************************************
+ *   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.
+ ****************************************************************/
+/*
+ * Copyright 2010 Google Inc. All Rights Reserved.
+ *
+ * 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 org.apache.cayenne.util.concurrentlinkedhashmap;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A common set of {@link Weigher} implementations.
+ * 
+ * @author [email protected] (Ben Manes)
+ * @see <a href="http://code.google.com/p/concurrentlinkedhashmap/";>
+ *      http://code.google.com/p/concurrentlinkedhashmap/</a>
+ */
+final class Weighers {
+
+    private Weighers() {
+        throw new AssertionError();
+    }
+
+    /**
+     * A weigher where a value has a weight of <tt>1</tt>. A map bounded with 
this weigher
+     * will evict when the number of key-value pairs exceeds the capacity.
+     * 
+     * @return A weigher where a value takes one unit of capacity.
+     */
+    @SuppressWarnings({
+        "unchecked"
+    })
+    public static <V> Weigher<V> singleton() {
+        return (Weigher<V>) SingletonWeigher.INSTANCE;
+    }
+
+    /**
+     * A weigher where the value is a byte array and its weight is the number 
of bytes. A
+     * map bounded with this weigher will evict when the number of bytes 
exceeds the
+     * capacity rather than the number of key-value pairs in the map. This 
allows for
+     * restricting the capacity based on the memory-consumption and is 
primarily for usage
+     * by dedicated caching servers that hold the serialized data.
+     * <p>
+     * A value with a weight of <tt>0</tt> will be rejected by the map. If a 
value with
+     * this weight can occur then the caller should eagerly evaluate the value 
and treat
+     * it as a removal operation. Alternatively, a custom weigher may be 
specified on the
+     * map to assign an empty value a positive weight.
+     * 
+     * @return A weigher where each byte takes one unit of capacity.
+     */
+    public static Weigher<byte[]> byteArray() {
+        return ByteArrayWeigher.INSTANCE;
+    }
+
+    /**
+     * A weigher where the value is a {@link Iterable} and its weight is the 
number of
+     * elements. This weigher only should be used when the alternative
+     * {@link #collection()} weigher cannot be, as evaluation takes O(n) time. 
A map
+     * bounded with this weigher will evict when the total number of elements 
exceeds the
+     * capacity rather than the number of key-value pairs in the map.
+     * <p>
+     * A value with a weight of <tt>0</tt> will be rejected by the map. If a 
value with
+     * this weight can occur then the caller should eagerly evaluate the value 
and treat
+     * it as a removal operation. Alternatively, a custom weigher may be 
specified on the
+     * map to assign an empty value a positive weight.
+     * 
+     * @return A weigher where each element takes one unit of capacity.
+     */
+    @SuppressWarnings({
+        "unchecked"
+    })
+    public static <E> Weigher<? super Iterable<E>> iterable() {
+        return (Weigher<Iterable<E>>) (Weigher<?>) IterableWeigher.INSTANCE;
+    }
+
+    /**
+     * A weigher where the value is a {@link Collection} and its weight is the 
number of
+     * elements. A map bounded with this weigher will evict when the total 
number of
+     * elements exceeds the capacity rather than the number of key-value pairs 
in the map.
+     * <p>
+     * A value with a weight of <tt>0</tt> will be rejected by the map. If a 
value with
+     * this weight can occur then the caller should eagerly evaluate the value 
and treat
+     * it as a removal operation. Alternatively, a custom weigher may be 
specified on the
+     * map to assign an empty value a positive weight.
+     * 
+     * @return A weigher where each element takes one unit of capacity.
+     */
+    @SuppressWarnings({
+        "unchecked"
+    })
+    public static <E> Weigher<? super Collection<E>> collection() {
+        return (Weigher<Collection<E>>) (Weigher<?>) 
CollectionWeigher.INSTANCE;
+    }
+
+    /**
+     * A weigher where the value is a {@link List} and its weight is the 
number of
+     * elements. A map bounded with this weigher will evict when the total 
number of
+     * elements exceeds the capacity rather than the number of key-value pairs 
in the map.
+     * <p>
+     * A value with a weight of <tt>0</tt> will be rejected by the map. If a 
value with
+     * this weight can occur then the caller should eagerly evaluate the value 
and treat
+     * it as a removal operation. Alternatively, a custom weigher may be 
specified on the
+     * map to assign an empty value a positive weight.
+     * 
+     * @return A weigher where each element takes one unit of capacity.
+     */
+    @SuppressWarnings({
+        "unchecked"
+    })
+    public static <E> Weigher<? super List<E>> list() {
+        return (Weigher<List<E>>) (Weigher<?>) ListWeigher.INSTANCE;
+    }
+
+    /**
+     * A weigher where the value is a {@link Set} and its weight is the number 
of
+     * elements. A map bounded with this weigher will evict when the total 
number of
+     * elements exceeds the capacity rather than the number of key-value pairs 
in the map.
+     * <p>
+     * A value with a weight of <tt>0</tt> will be rejected by the map. If a 
value with
+     * this weight can occur then the caller should eagerly evaluate the value 
and treat
+     * it as a removal operation. Alternatively, a custom weigher may be 
specified on the
+     * map to assign an empty value a positive weight.
+     * 
+     * @return A weigher where each element takes one unit of capacity.
+     */
+    @SuppressWarnings({
+        "unchecked"
+    })
+    public static <E> Weigher<? super Set<E>> set() {
+        return (Weigher<Set<E>>) (Weigher<?>) SetWeigher.INSTANCE;
+    }
+
+    /**
+     * A weigher where the value is a {@link Map} and its weight is the number 
of entries.
+     * A map bounded with this weigher will evict when the total number of 
entries across
+     * all values exceeds the capacity rather than the number of key-value 
pairs in the
+     * map.
+     * <p>
+     * A value with a weight of <tt>0</tt> will be rejected by the map. If a 
value with
+     * this weight can occur then the caller should eagerly evaluate the value 
and treat
+     * it as a removal operation. Alternatively, a custom weigher may be 
specified on the
+     * map to assign an empty value a positive weight.
+     * 
+     * @return A weigher where each entry takes one unit of capacity.
+     */
+    @SuppressWarnings({
+        "unchecked"
+    })
+    public static <A, B> Weigher<? super Map<A, B>> map() {
+        return (Weigher<Map<A, B>>) (Weigher<?>) MapWeigher.INSTANCE;
+    }
+
+    private enum SingletonWeigher implements Weigher<Object> {
+        INSTANCE;
+
+        public int weightOf(Object value) {
+            return 1;
+        }
+    }
+
+    private enum ByteArrayWeigher implements Weigher<byte[]> {
+        INSTANCE;
+
+        public int weightOf(byte[] value) {
+            return value.length;
+        }
+    }
+
+    private enum IterableWeigher implements Weigher<Iterable<?>> {
+        INSTANCE;
+
+        public int weightOf(Iterable<?> values) {
+            if (values instanceof Collection<?>) {
+                return ((Collection<?>) values).size();
+            }
+            int size = 0;
+            for (Object value : values) {
+                size++;
+            }
+            return size;
+        }
+    }
+
+    private enum CollectionWeigher implements Weigher<Collection<?>> {
+        INSTANCE;
+
+        public int weightOf(Collection<?> values) {
+            return values.size();
+        }
+    }
+
+    private enum ListWeigher implements Weigher<List<?>> {
+        INSTANCE;
+
+        public int weightOf(List<?> values) {
+            return values.size();
+        }
+    }
+
+    private enum SetWeigher implements Weigher<Set<?>> {
+        INSTANCE;
+
+        public int weightOf(Set<?> values) {
+            return values.size();
+        }
+    }
+
+    private enum MapWeigher implements Weigher<Map<?, ?>> {
+        INSTANCE;
+
+        public int weightOf(Map<?, ?> values) {
+            return values.size();
+        }
+    }
+}

Added: 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java?rev=1292435&view=auto
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
 (added)
+++ 
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/util/concurrentlinkedhashmap/ConcurrentLinkedHashMapTest.java
 Wed Feb 22 18:31:39 2012
@@ -0,0 +1,68 @@
+/*****************************************************************
+ *   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.cayenne.util.concurrentlinkedhashmap;
+
+import junit.framework.TestCase;
+
+public class ConcurrentLinkedHashMapTest extends TestCase {
+
+    public void testPutGet() {
+        ConcurrentLinkedHashMap<String, Object> m = new 
ConcurrentLinkedHashMap.Builder<String, Object>()
+                .maximumWeightedCapacity(10)
+                .build();
+
+        assertEquals(0, m.size());
+        m.put("k1", 100);
+        assertEquals(1, m.size());
+        assertNull(m.get("nosuchkey"));
+        assertEquals(100, m.get("k1"));
+
+        m.put("k2", 200);
+        assertEquals(2, m.size());
+        assertEquals(200, m.get("k2"));
+    }
+
+    public void testLRU() {
+        ConcurrentLinkedHashMap<String, Object> m = new 
ConcurrentLinkedHashMap.Builder<String, Object>()
+                .maximumWeightedCapacity(5)
+                .build();
+
+        assertEquals(0, m.size());
+        m.put("k1", 100);
+        assertEquals(1, m.size());
+        m.put("k2", 101);
+        assertEquals(2, m.size());
+        m.put("k3", 102);
+        assertEquals(3, m.size());
+        m.put("k4", 103);
+        assertEquals(4, m.size());
+        m.put("k5", 104);
+        assertEquals(5, m.size());
+        m.put("k6", 105);
+        assertEquals(5, m.size());
+        m.put("k7", 106);
+        assertEquals(5, m.size());
+        m.put("k8", 107);
+        assertEquals(5, m.size());
+
+        m.remove("k6");
+        assertEquals(4, m.size());
+
+    }
+}

Modified: 
cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/LICENSE.txt
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/LICENSE.txt?rev=1292435&r1=1292434&r2=1292435&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/LICENSE.txt
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/LICENSE.txt
 Wed Feb 22 18:31:39 2012
@@ -794,4 +794,22 @@ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, E
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=====================================================================
+concurrentlinkedhashmap License
+
+
+Copyright 2010 Google Inc. All Rights Reserved.
+
+ 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.

Modified: 
cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/NOTICE.txt
URL: 
http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/NOTICE.txt?rev=1292435&r1=1292434&r2=1292435&view=diff
==============================================================================
--- 
cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/NOTICE.txt
 (original)
+++ 
cayenne/main/trunk/framework/cayenne-legal-unpublished/src/main/resources/META-INF/cayenne/NOTICE.txt
 Wed Feb 22 18:31:39 2012
@@ -17,6 +17,9 @@ originally developed at The ObjectStyle 
 3. This software includes VPP library developed by FoundryLogic, LLC.
 (http://vpp.sourceforge.net/)
 
+4. This software includes concurrentlinkedhashmap library developed by Google 
Inc.
+(http://code.google.com/p/concurrentlinkedhashmap/)
+
 
 GUI TOOLS
 


Reply via email to