You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2010/11/09 22:37:28 UTC

svn commit: r1033246 - in /wicket/trunk: wicket-util/src/main/java/org/apache/wicket/util/lang/ wicket-util/src/main/java/org/apache/wicket/util/value/ wicket-util/src/test/java/org/apache/wicket/util/value/ wicket/src/main/java/org/apache/wicket/marku...

Author: ivaynberg
Date: Tue Nov  9 21:37:27 2010
New Revision: 1033246

URL: http://svn.apache.org/viewvc?rev=1033246&view=rev
Log:

Issue: WICKET-3153

Added:
    wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/
    wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/LongValueTest.java   (with props)
Modified:
    wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Bytes.java
    wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java

Modified: wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Bytes.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Bytes.java?rev=1033246&r1=1033245&r2=1033246&view=diff
==============================================================================
--- wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Bytes.java (original)
+++ wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Bytes.java Tue Nov  9 21:37:27 2010
@@ -424,4 +424,21 @@ public final class Bytes extends LongVal
 	{
 		return StringValue.valueOf(value, locale) + units;
 	}
+
+	/**
+	 * Compares this <code>Bytes</code> with another <code>Bytes</code> instance.
+	 * 
+	 * @param other
+	 *            the <code>Bytes</code> instance to compare with
+	 * @return <code>true</code> if this <code>Bytes</code> is greater than the given
+	 *         <code>Bytes</code> instance
+	 */
+	public boolean greaterThan(Bytes other)
+	{
+		if (this == other || other == null)
+		{
+			return false;
+		}
+		return bytes() > other.bytes();
+	}
 }

Modified: wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java?rev=1033246&r1=1033245&r2=1033246&view=diff
==============================================================================
--- wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java (original)
+++ wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/value/LongValue.java Tue Nov  9 21:37:27 2010
@@ -18,6 +18,7 @@ package org.apache.wicket.util.value;
 
 import java.io.Serializable;
 
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Primitives;
 
 
@@ -214,4 +215,51 @@ public class LongValue implements Compar
 	{
 		return String.valueOf(value);
 	}
+
+	/**
+	 * Returns the max of the two long values.
+	 * 
+	 * @param <T>
+	 * @param lhs
+	 * @param rhs
+	 * @throws IllegalArgumentException
+	 *             if either argument is {@code null}
+	 * @return max value
+	 */
+	public static <T extends LongValue> T max(T lhs, T rhs)
+	{
+		Args.notNull(lhs, "lhs");
+		Args.notNull(rhs, "rhs");
+		if (lhs.compareTo(rhs) > 0)
+		{
+			return lhs;
+		}
+		return rhs;
+	}
+
+	/**
+	 * Null-safe version of {@link LongValue#max}. Nulls are considered less then any concrete
+	 * value.
+	 * 
+	 * @param <T>
+	 * @param lhs
+	 * @param rhs
+	 * @return max of two values or {@code null} if they are both null
+	 */
+	public static <T extends LongValue> T maxNullSafe(T lhs, T rhs)
+	{
+		if (lhs == rhs)
+		{
+			return lhs;
+		}
+		else if (lhs == null)
+		{
+			return rhs;
+		}
+		else if (rhs == null)
+		{
+			return lhs;
+		}
+		return max(lhs, rhs);
+	}
 }

Added: wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/LongValueTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/LongValueTest.java?rev=1033246&view=auto
==============================================================================
--- wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/LongValueTest.java (added)
+++ wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/LongValueTest.java Tue Nov  9 21:37:27 2010
@@ -0,0 +1,76 @@
+/*
+ * 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.util.value;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+/**
+ * {@link LongValue} tests
+ * 
+ * @author igor
+ */
+public class LongValueTest
+{
+	/** Test {@link LongValue#max(LongValue, LongValue)} */
+	@Test
+	public void max()
+	{
+		LongValue v1 = new LongValue(1);
+		LongValue v2 = new LongValue(2);
+
+		assertEquals(v2, LongValue.max(v1, v2));
+		assertEquals(v2, LongValue.max(v2, v1));
+
+		try
+		{
+			LongValue.max(v1, null);
+			fail("Should have failed on null arg");
+		}
+		catch (IllegalArgumentException e)
+		{
+			// expected
+		}
+
+		try
+		{
+			LongValue.max(null, v1);
+			fail("Should have failed on null arg");
+		}
+		catch (IllegalArgumentException e)
+		{
+			// expected
+		}
+	}
+
+	/** Test {@link LongValue#maxNullSafe(LongValue, LongValue)} */
+	@Test
+	public void maxNullSafe()
+	{
+		LongValue v1 = new LongValue(1);
+		LongValue v2 = new LongValue(2);
+
+		assertEquals(v2, LongValue.maxNullSafe(v1, v2));
+		assertEquals(v2, LongValue.maxNullSafe(v2, v1));
+		assertEquals(v2, LongValue.maxNullSafe(null, v2));
+		assertEquals(v2, LongValue.maxNullSafe(v2, null));
+		assertNull(LongValue.maxNullSafe(null, null));
+	}
+}

Propchange: wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/value/LongValueTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=1033246&r1=1033245&r2=1033246&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java Tue Nov  9 21:37:27 2010
@@ -53,6 +53,7 @@ import org.apache.wicket.util.string.Str
 import org.apache.wicket.util.string.interpolator.MapVariableInterpolator;
 import org.apache.wicket.util.upload.FileUploadBase.SizeLimitExceededException;
 import org.apache.wicket.util.upload.FileUploadException;
+import org.apache.wicket.util.value.LongValue;
 import org.apache.wicket.util.visit.ClassVisitFilter;
 import org.apache.wicket.util.visit.IVisit;
 import org.apache.wicket.util.visit.IVisitor;
@@ -619,30 +620,37 @@ public class Form<T> extends WebMarkupCo
 	 * Gets the maximum size for uploads. If null, the setting
 	 * {@link IApplicationSettings#getDefaultMaximumUploadSize()} is used.
 	 * 
+	 * 
 	 * @return the maximum size
 	 */
-	public Bytes getMaxSize()
+	public final Bytes getMaxSize()
 	{
-		Bytes maxSize = this.maxSize;
-		if (maxSize == null)
+		/*
+		 * NOTE: This method should remain final otherwise it will be impossible to set a default
+		 * max size smaller then the one specified in applications settings because the inner form
+		 * will return the default unless it is specifically set in the traversal. With this method
+		 * remaining final we can tell when the value is explicitly set by the user.
+		 * 
+		 * If the value needs to be dynamic it can be set in oncofigure() instead of overriding this
+		 * method.
+		 */
+
+		final Bytes[] maxSize = new Bytes[] { this.maxSize };
+		if (maxSize[0] == null)
 		{
-			maxSize = visitChildren(Form.class, new IVisitor<Form<?>, Bytes>()
+			visitChildren(Form.class, new IVisitor<Form<?>, Bytes>()
 			{
 				public void component(Form<?> component, IVisit<Bytes> visit)
 				{
-					Bytes maxSize = component.getMaxSize();
-					if (maxSize != null)
-					{
-						visit.stop(maxSize);
-					}
+					maxSize[0] = LongValue.maxNullSafe(maxSize[0], component.maxSize);
 				}
 			});
 		}
-		if (maxSize == null)
+		if (maxSize[0] == null)
 		{
 			return getApplication().getApplicationSettings().getDefaultMaximumUploadSize();
 		}
-		return maxSize;
+		return maxSize[0];
 	}
 
 	/**
@@ -973,7 +981,7 @@ public class Form<T> extends WebMarkupCo
 	 * @param maxSize
 	 *            The maximum size
 	 */
-	public void setMaxSize(final Bytes maxSize)
+	public final void setMaxSize(final Bytes maxSize)
 	{
 		this.maxSize = maxSize;
 	}