Author: britter
Date: Sun Jan 12 10:22:00 2014
New Revision: 1557509

URL: http://svn.apache.org/r1557509
Log:
LANG-834: Validate: add inclusiveBetween and exclusiveBetween overloads for 
primitives types. Initial patch provided by Sebb.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
    
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1557509&r1=1557508&r2=1557509&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sun Jan 12 
10:22:00 2014
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.3" date="TBA" description="Bugfix and Feature release">
+    <action issue="LANG-834" type="add" dev="britter">Validate: add 
inclusiveBetween and exclusiveBetween overloads for primitives types</action>
     <action issue="LANG-900" type="add" dev="britter" due-to="Duncan 
Jones">New RandomUtils class</action>
     <action issue="LANG-915" type="fix" dev="britter" due-to="Sergio 
Fernández">Wrong locale handling in LocaleUtils.toLocale()</action>
   </release>

Modified: 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java?rev=1557509&r1=1557508&r2=1557509&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java 
(original)
+++ 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java 
Sun Jan 12 10:22:00 2014
@@ -926,6 +926,94 @@ public class Validate {
         }
     }
 
+    /**
+    * Validate that the specified primitive value falls between the two
+    * inclusive values specified; otherwise, throws an exception.
+    *
+    * <pre>Validate.inclusiveBetween(0, 2, 1);</pre>
+    *
+    * @param start the inclusive start value
+    * @param end   the inclusive end value
+    * @param value the value to validate
+    * @throws IllegalArgumentException if the value falls outside the 
boundaries (inclusive)
+    *
+    * @since 3.3
+    */
+    @SuppressWarnings("boxing")
+    public static void inclusiveBetween(long start, long end, long value) {
+        // TODO when breaking BC, consider returning value
+        if (value < start || value > end) {
+            throw new 
IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, 
value, start, end));
+        }
+    }
+
+    /**
+    * Validate that the specified primitive value falls between the two
+    * inclusive values specified; otherwise, throws an exception with the
+    * specified message.
+    *
+    * <pre>Validate.inclusiveBetween(0, 2, 1, "Not in range");</pre>
+    *
+    * @param start the inclusive start value
+    * @param end   the inclusive end value
+    * @param value the value to validate
+    * @param message the exception message if invalid, not null
+    *
+    * @throws IllegalArgumentException if the value falls outside the 
boundaries
+    *
+    * @since 3.3
+    */
+    public static void inclusiveBetween(long start, long end, long value, 
String message) {
+        // TODO when breaking BC, consider returning value
+        if (value < start || value > end) {
+            throw new IllegalArgumentException(String.format(message));
+        }
+    }
+
+    /**
+    * Validate that the specified primitive value falls between the two
+    * inclusive values specified; otherwise, throws an exception.
+    *
+    * <pre>Validate.inclusiveBetween(0.1, 2.1, 1.1);</pre>
+    *
+    * @param start the inclusive start value
+    * @param end   the inclusive end value
+    * @param value the value to validate
+    * @throws IllegalArgumentException if the value falls outside the 
boundaries (inclusive)
+    *
+    * @since 3.3
+    */
+    @SuppressWarnings("boxing")
+    public static void inclusiveBetween(double start, double end, double 
value) {
+        // TODO when breaking BC, consider returning value
+        if (value < start || value > end) {
+            throw new 
IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, 
value, start, end));
+        }
+    }
+
+    /**
+    * Validate that the specified primitive value falls between the two
+    * inclusive values specified; otherwise, throws an exception with the
+    * specified message.
+    *
+    * <pre>Validate.inclusiveBetween(0.1, 2.1, 1.1, "Not in range");</pre>
+    *
+    * @param start the inclusive start value
+    * @param end   the inclusive end value
+    * @param value the value to validate
+    * @param message the exception message if invalid, not null
+    *
+    * @throws IllegalArgumentException if the value falls outside the 
boundaries
+    *
+    * @since 3.3
+    */
+    public static void inclusiveBetween(double start, double end, double 
value, String message) {
+        // TODO when breaking BC, consider returning value
+        if (value < start || value > end) {
+            throw new IllegalArgumentException(String.format(message));
+        }
+    }
+
     // exclusiveBetween
     
//---------------------------------------------------------------------------------
 
@@ -976,6 +1064,94 @@ public class Validate {
         }
     }
 
+    /**
+    * Validate that the specified primitive value falls between the two
+    * exclusive values specified; otherwise, throws an exception.
+    *
+    * <pre>Validate.exclusiveBetween(0, 2, 1);</pre>
+    *
+    * @param start the exclusive start value
+    * @param end   the exclusive end value
+    * @param value the value to validate
+    * @throws IllegalArgumentException if the value falls out of the boundaries
+    *
+    * @since 3.3
+    */
+    @SuppressWarnings("boxing")
+    public static void exclusiveBetween(long start, long end, long value) {
+        // TODO when breaking BC, consider returning value
+        if (value <= start || value >= end) {
+            throw new 
IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, 
value, start, end));
+        }
+    }
+
+    /**
+    * Validate that the specified primitive value falls between the two
+    * exclusive values specified; otherwise, throws an exception with the
+    * specified message.
+    *
+    * <pre>Validate.exclusiveBetween(0, 2, 1, "Not in range");</pre>
+    *
+    * @param start the exclusive start value
+    * @param end   the exclusive end value
+    * @param value the value to validate
+    * @param message the exception message if invalid, not null
+    *
+    * @throws IllegalArgumentException if the value falls outside the 
boundaries
+    *
+    * @since 3.3
+    */
+    public static void exclusiveBetween(long start, long end, long value, 
String message) {
+        // TODO when breaking BC, consider returning value
+        if (value <= start || value >= end) {
+            throw new IllegalArgumentException(String.format(message));
+        }
+    }
+
+    /**
+    * Validate that the specified primitive value falls between the two
+    * exclusive values specified; otherwise, throws an exception.
+    *
+    * <pre>Validate.exclusiveBetween(0.1, 2.1, 1.1);</pre>
+    *
+    * @param start the exclusive start value
+    * @param end   the exclusive end value
+    * @param value the value to validate
+    * @throws IllegalArgumentException if the value falls out of the boundaries
+    *
+    * @since 3.3
+    */
+    @SuppressWarnings("boxing")
+    public static void exclusiveBetween(double start, double end, double 
value) {
+        // TODO when breaking BC, consider returning value
+        if (value <= start || value >= end) {
+            throw new 
IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, 
value, start, end));
+        }
+    }
+
+    /**
+    * Validate that the specified primitive value falls between the two
+    * exclusive values specified; otherwise, throws an exception with the
+    * specified message.
+    *
+    * <pre>Validate.exclusiveBetween(0.1, 2.1, 1.1, "Not in range");</pre>
+    *
+    * @param start the exclusive start value
+    * @param end   the exclusive end value
+    * @param value the value to validate
+    * @param message the exception message if invalid, not null
+    *
+    * @throws IllegalArgumentException if the value falls outside the 
boundaries
+    *
+    * @since 3.3
+    */
+    public static void exclusiveBetween(double start, double end, double 
value, String message) {
+        // TODO when breaking BC, consider returning value
+        if (value <= start || value >= end) {
+            throw new IllegalArgumentException(String.format(message));
+        }
+    }
+
     // isInstanceOf
     
//---------------------------------------------------------------------------------
 

Modified: 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java?rev=1557509&r1=1557508&r2=1557509&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
 (original)
+++ 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
 Sun Jan 12 10:22:00 2014
@@ -835,10 +835,8 @@ public class ValidateTest  {
     public void testInclusiveBetween()
     {
         Validate.inclusiveBetween("a", "c", "b");
-        Validate.inclusiveBetween(0, 2, 1);
-        Validate.inclusiveBetween(0, 2, 2);
         try {
-            Validate.inclusiveBetween(0, 5, 6);
+            Validate.inclusiveBetween("0", "5", "6");
             fail("Expecting IllegalArgumentException");
         } catch (final IllegalArgumentException e) {
             assertEquals("The value 6 is not in the specified inclusive range 
of 0 to 5", e.getMessage());
@@ -849,6 +847,30 @@ public class ValidateTest  {
     public void testInclusiveBetween_withMessage()
     {
         Validate.inclusiveBetween("a", "c", "b", "Error");
+        try {
+            Validate.inclusiveBetween("0", "5", "6", "Error");
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Error", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testInclusiveBetweenLong()
+    {
+        Validate.inclusiveBetween(0, 2, 1);
+        Validate.inclusiveBetween(0, 2, 2);
+        try {
+            Validate.inclusiveBetween(0, 5, 6);
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("The value 6 is not in the specified inclusive range 
of 0 to 5", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testInclusiveBetweenLong_withMessage()
+    {
         Validate.inclusiveBetween(0, 2, 1, "Error");
         Validate.inclusiveBetween(0, 2, 2, "Error");
         try {
@@ -858,20 +880,45 @@ public class ValidateTest  {
             assertEquals("Error", e.getMessage());
         }
     }
+
+    @Test
+    public void testInclusiveBetweenDouble()
+    {
+        Validate.inclusiveBetween(0.1, 2.1, 1.1);
+        Validate.inclusiveBetween(0.1, 2.1, 2.1);
+        try {
+            Validate.inclusiveBetween(0.1, 5.1, 6.1);
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("The value 6.1 is not in the specified inclusive 
range of 0.1 to 5.1", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testInclusiveBetweenDouble_withMessage()
+    {
+        Validate.inclusiveBetween(0.1, 2.1, 1.1, "Error");
+        Validate.inclusiveBetween(0.1, 2.1, 2.1, "Error");
+        try {
+            Validate.inclusiveBetween(0.1, 5.1, 6.1, "Error");
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Error", e.getMessage());
+        }
+    }
     
     @Test
     public void testExclusiveBetween()
     {
         Validate.exclusiveBetween("a", "c", "b");
-        Validate.exclusiveBetween(0, 2, 1);
         try {
-            Validate.exclusiveBetween(0, 5, 6);
+            Validate.exclusiveBetween("0", "5", "6");
             fail("Expecting IllegalArgumentException");
         } catch (final IllegalArgumentException e) {
             assertEquals("The value 6 is not in the specified exclusive range 
of 0 to 5", e.getMessage());
         }
         try {
-            Validate.exclusiveBetween(0, 5, 5);
+            Validate.exclusiveBetween("0", "5", "5");
             fail("Expecting IllegalArgumentException");
         } catch (final IllegalArgumentException e) {
             assertEquals("The value 5 is not in the specified exclusive range 
of 0 to 5", e.getMessage());
@@ -882,6 +929,41 @@ public class ValidateTest  {
     public void testExclusiveBetween_withMessage()
     {
         Validate.exclusiveBetween("a", "c", "b", "Error");
+        try {
+            Validate.exclusiveBetween("0", "5", "6", "Error");
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Error", e.getMessage());
+        }
+        try {
+            Validate.exclusiveBetween("0", "5", "5", "Error");
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Error", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testExclusiveBetweenLong()
+    {
+        Validate.exclusiveBetween(0, 2, 1);
+        try {
+            Validate.exclusiveBetween(0, 5, 6);
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("The value 6 is not in the specified exclusive range 
of 0 to 5", e.getMessage());
+        }
+        try {
+            Validate.exclusiveBetween(0, 5, 5);
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("The value 5 is not in the specified exclusive range 
of 0 to 5", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testExclusiveBetweenLong_withMessage()
+    {
         Validate.exclusiveBetween(0, 2, 1, "Error");
         try {
             Validate.exclusiveBetween(0, 5, 6, "Error");
@@ -898,6 +980,42 @@ public class ValidateTest  {
     }
 
     @Test
+    public void testExclusiveBetweenDouble()
+    {
+        Validate.exclusiveBetween(0.1, 2.1, 1.1);
+        try {
+            Validate.exclusiveBetween(0.1, 5.1, 6.1);
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("The value 6.1 is not in the specified exclusive 
range of 0.1 to 5.1", e.getMessage());
+        }
+        try {
+            Validate.exclusiveBetween(0.1, 5.1, 5.1);
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("The value 5.1 is not in the specified exclusive 
range of 0.1 to 5.1", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testExclusiveBetweenDouble_withMessage()
+    {
+        Validate.exclusiveBetween(0.1, 2.1, 1.1, "Error");
+        try {
+            Validate.exclusiveBetween(0.1, 5.1, 6.1, "Error");
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Error", e.getMessage());
+        }
+        try {
+            Validate.exclusiveBetween(0.1, 5.1, 5.1, "Error");
+            fail("Expecting IllegalArgumentException");
+        } catch (final IllegalArgumentException e) {
+            assertEquals("Error", e.getMessage());
+        }
+    }
+
+    @Test
     public void testIsInstanceOf() {
         Validate.isInstanceOf(String.class, "hi");
         Validate.isInstanceOf(Integer.class, 1);


Reply via email to