Author: ehillenius Date: Thu Jun 7 18:48:43 2007 New Revision: 545366 URL: http://svn.apache.org/viewvc?view=rev&rev=545366 Log: meta data optimization and unit test
Added: incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/MetaDataTest.java Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/MetaDataKey.java Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/MetaDataKey.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/MetaDataKey.java?view=diff&rev=545366&r1=545365&r2=545366 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/MetaDataKey.java (original) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/MetaDataKey.java Thu Jun 7 18:48:43 2007 @@ -33,7 +33,7 @@ public abstract class MetaDataKey implements IClusterable { private static final long serialVersionUID = 1L; - + /** Type of data associated with this key */ private Class type; @@ -53,7 +53,7 @@ */ public boolean equals(Object obj) { - return getClass().isInstance(obj); + return obj != null && getClass().isInstance(obj); } /** @@ -81,7 +81,7 @@ * @param metaData * The array of metadata * @param object - * The object to set + * The object to set, null to remove * @return Any new metadata array (if it was reallocated) */ MetaDataEntry[] set(MetaDataEntry[] metaData, final Serializable object) @@ -95,12 +95,33 @@ MetaDataEntry m = metaData[i]; if (equals(m.key)) { - m.object = object; + if (object != null) + { + // set new value + m.object = object; + } + else + { + // remove value and schrink or null array + if (metaData.length > 1) + { + int l = metaData.length - 1; + MetaDataEntry[] newMetaData = new MetaDataEntry[l]; + System.arraycopy(metaData, 0, newMetaData, 0, i); + System.arraycopy(metaData, i + 1, newMetaData, i, l - i); + metaData = newMetaData; + } + else + { + metaData = null; + break; + } + } set = true; } } } - if (!set) + if (!set && object != null) { MetaDataEntry m = new MetaDataEntry(); m.key = this; @@ -133,7 +154,7 @@ */ void checkType(final Object object) { - if (object != null && !type.isAssignableFrom(object.getClass()) ) + if (object != null && !type.isAssignableFrom(object.getClass())) { throw new IllegalArgumentException("MetaDataKey " + getClass() + " requires argument of " + type + ", not " + object.getClass()); Added: incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/MetaDataTest.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/MetaDataTest.java?view=auto&rev=545366 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/MetaDataTest.java (added) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/MetaDataTest.java Thu Jun 7 18:48:43 2007 @@ -0,0 +1,99 @@ +/* + * 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.wicket; + +import junit.framework.TestCase; + +/** + * Some tests for meta data. + */ +public class MetaDataTest extends TestCase +{ + private static final MetaDataKey KEY1 = new MetaDataKey(String.class) + { + private static final long serialVersionUID = 1L; + }; + + private static final MetaDataKey KEY2 = new MetaDataKey(String.class) + { + private static final long serialVersionUID = 1L; + }; + + private static final MetaDataKey KEY3 = new MetaDataKey(String.class) + { + private static final long serialVersionUID = 1L; + }; + + private static final MetaDataKey KEY4 = new MetaDataKey(String.class) + { + private static final long serialVersionUID = 1L; + }; + + /** + * Construct. + */ + public MetaDataTest() + { + } + + /** + * Construct. + * + * @param name + */ + public MetaDataTest(String name) + { + super(name); + } + + /** + * Test bounds and basic operations. + */ + public void testMetaDataKey() + { + MetaDataEntry[] md = KEY1.set(null, "1"); + assertNotNull(md); + assertEquals(1, md.length); + md = KEY1.set(md, null); + assertNull(md); + md = KEY1.set(md, "1"); + md = KEY2.set(md, "2"); + md = KEY3.set(md, "3"); + md = KEY4.set(md, "4"); + assertEquals(4, md.length); + md = KEY3.set(md, null); + assertEquals(3, md.length); + assertEquals("1", KEY1.get(md)); + assertEquals("2", KEY2.get(md)); + assertEquals(null, KEY3.get(md)); + assertEquals("4", KEY4.get(md)); + md = KEY4.set(md, null); + assertEquals(2, md.length); + assertEquals("1", KEY1.get(md)); + assertEquals("2", KEY2.get(md)); + assertEquals(null, KEY3.get(md)); + assertEquals(null, KEY4.get(md)); + md = KEY1.set(md, null); + assertEquals(1, md.length); + assertEquals(null, KEY1.get(md)); + assertEquals("2", KEY2.get(md)); + assertEquals(null, KEY3.get(md)); + assertEquals(null, KEY4.get(md)); + md = KEY2.set(md, null); + assertNull(md); + } +}