myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i...@apache.org
Subject svn commit: r638970 - in /myfaces/tobago/trunk/core/src: main/java/org/apache/myfaces/tobago/component/ test/java/org/apache/myfaces/tobago/component/
Date Wed, 19 Mar 2008 18:47:12 GMT
Author: idus
Date: Wed Mar 19 11:47:06 2008
New Revision: 638970

URL: http://svn.apache.org/viewvc?rev=638970&view=rev
Log:
TOBAGO-639: Avoid usage of implicit regular expressions

Modified:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PercentLayoutToken.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PixelLayoutToken.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/RelativeLayoutToken.java
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java?rev=638970&r1=638969&r2=638970&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
(original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
Wed Mar 19 11:47:06 2008
@@ -33,10 +33,6 @@
 
   private static final Log LOG = LogFactory.getLog(LayoutTokens.class);
 
-  private static final String PIXEL_TOKEN_SUFFIX = "px";
-  private static final String PERCENT_TOKEN_SUFFIX = "%";
-  private static final String RELATIVE_TOKEN_SUFFIX = "*";
-
   private List<LayoutToken> tokens = new ArrayList<LayoutToken>();
 
   public int getSize() {
@@ -114,11 +110,11 @@
       } else if (token.equals("minimum")) {
         return new MinimumLayoutToken();
       } else if (isPixelToken(token)) {
-        return new PixelLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")));
+        return new PixelLayoutToken(Integer.parseInt(removeSuffix(token, PixelLayoutToken.SUFFIX)));
       } else if (isPercentToken(token)) {
-        return new PercentLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")));
+        return new PercentLayoutToken(Integer.parseInt(removeSuffix(token, PercentLayoutToken.SUFFIX)));
       } else if (isRelativeToken(token)) {
-        return new RelativeLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")));
+        return new RelativeLayoutToken(Integer.parseInt(removeSuffix(token, RelativeLayoutToken.SUFFIX)));
       } else {
         LOG.error("Ignoring unknown layout token '" + token + "'");
       }
@@ -129,20 +125,24 @@
   }
 
   static boolean isPixelToken(String token) {
-    return isNumberAndSuffix(token, PIXEL_TOKEN_SUFFIX);
+    return isNumberAndSuffix(token, PixelLayoutToken.SUFFIX);
   }
 
   static boolean isPercentToken(String token) {
-    return isNumberAndSuffix(token, PERCENT_TOKEN_SUFFIX);
+    return isNumberAndSuffix(token, PercentLayoutToken.SUFFIX);
   }
 
   static boolean isRelativeToken(String token) {
-    return isNumberAndSuffix(token, RELATIVE_TOKEN_SUFFIX);
+    return isNumberAndSuffix(token, RelativeLayoutToken.SUFFIX);
   }
 
   static boolean isNumberAndSuffix(String token, String suffix) {
     return token.endsWith(suffix)
-        && NumberUtils.isDigits(token.substring(0, token.length() - suffix.length()));
+        && NumberUtils.isDigits(removeSuffix(token, suffix));
+  }
+
+  private static String removeSuffix(String token, String suffix) {
+    return token.substring(0, token.length() - suffix.length());
   }
 
   public String toString() {

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PercentLayoutToken.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PercentLayoutToken.java?rev=638970&r1=638969&r2=638970&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PercentLayoutToken.java
(original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PercentLayoutToken.java
Wed Mar 19 11:47:06 2008
@@ -22,8 +22,10 @@
  * Time: 4:29:28 PM
  */
 public class PercentLayoutToken extends LayoutToken {
-  private int percent = 0;
 
+  static final String SUFFIX = "%";
+
+  private int percent = 0;
 
   public PercentLayoutToken(int percent) {
     this.percent = percent;
@@ -34,6 +36,27 @@
   }
 
   public String toString() {
-    return percent + "%";
+    return percent + SUFFIX;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    PercentLayoutToken that = (PercentLayoutToken) o;
+
+    if (percent != that.percent) {
+      return false;
+    }
+
+    return true;
+  }
+
+  public int hashCode() {
+    return percent;
   }
 }

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PixelLayoutToken.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PixelLayoutToken.java?rev=638970&r1=638969&r2=638970&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PixelLayoutToken.java
(original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/PixelLayoutToken.java
Wed Mar 19 11:47:06 2008
@@ -19,8 +19,10 @@
 
 
 public class PixelLayoutToken extends LayoutToken {
-  private int pixel = 0;
 
+  static final String SUFFIX = "px";
+
+  private int pixel = 0;
 
   public PixelLayoutToken(int pixel) {
     this.pixel = pixel;
@@ -31,6 +33,27 @@
   }
 
   public String toString() {
-    return pixel + "px";
+    return pixel + SUFFIX;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    PixelLayoutToken that = (PixelLayoutToken) o;
+
+    if (pixel != that.pixel) {
+      return false;
+    }
+
+    return true;
+  }
+
+  public int hashCode() {
+    return pixel;
   }
 }

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/RelativeLayoutToken.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/RelativeLayoutToken.java?rev=638970&r1=638969&r2=638970&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/RelativeLayoutToken.java
(original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/RelativeLayoutToken.java
Wed Mar 19 11:47:06 2008
@@ -22,20 +22,44 @@
  * Time: 1:34:11 PM
  */
 public class RelativeLayoutToken extends LayoutToken {
+
+  static final String SUFFIX = "*";
+
   public static final String DEFAULT_TOKEN_STRING = "1*";
   public static final RelativeLayoutToken DEFAULT_INSTANCE = new RelativeLayoutToken(1);
+
   private int factor = 1;
 
   public RelativeLayoutToken(int factor) {
     this.factor = factor;
   }
 
-
   public int getFactor() {
     return factor;
   }
 
   public String toString() {
-    return factor + "*";
+    return factor + SUFFIX;
+  }
+
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    RelativeLayoutToken that = (RelativeLayoutToken) o;
+
+    if (factor != that.factor) {
+      return false;
+    }
+
+    return true;
+  }
+
+  public int hashCode() {
+    return factor;
   }
 }

Modified: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java?rev=638970&r1=638969&r2=638970&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
(original)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
Wed Mar 19 11:47:06 2008
@@ -38,4 +38,11 @@
     assertFalse(LayoutTokens.isNumberAndSuffix("acm", "cm"));
     assertFalse(LayoutTokens.isNumberAndSuffix("cm", "cm"));
   }
+
+  public void testParseToken() {
+    assertEquals(RelativeLayoutToken.DEFAULT_INSTANCE, LayoutTokens.parseToken("*"));
+    assertEquals(new RelativeLayoutToken(3), LayoutTokens.parseToken("3*"));
+    assertEquals(new PercentLayoutToken(33), LayoutTokens.parseToken("33%"));
+    assertEquals(new PixelLayoutToken(120), LayoutTokens.parseToken("120px"));
+  }
 }



Mime
View raw message