View Javadoc

1   
2   package org.paneris.jammyjoes.util;
3   
4   import java.text.NumberFormat;
5   import java.util.Locale;
6   
7   import org.melati.util.ContextUtil;
8   import org.melati.util.HTMLUtils;
9   import org.melati.util.UTF8URLEncoder;
10  
11  /**
12   * A place where useful Static methods can be put.
13   */
14  
15  public class JammyJoesContextUtil {
16  
17    ContextUtil _melatiUtil = new ContextUtil(); 
18    
19      public Integer increment(Integer a) {
20          return _melatiUtil.increment(a);
21      }
22  
23      public static String getPriceDisplayWithoutPound(Double price) {
24        if (price == null) return "";
25        double a = price.doubleValue();
26        String s = NumberFormat.getCurrencyInstance(Locale.UK).format(a);
27        if (s.length() > 1) s = s.substring(1,s.length());
28        return s;
29      }
30  
31      public static String getInteger(Double price) {
32        if (price == null) return "";
33        return price.intValue()+"";
34      }
35  
36      public static String getNonZeroInteger(Double price) {
37        if (price == null) return "";
38        if (price.doubleValue() == 0) return "";
39        return price.intValue()+"";
40      }
41  
42      public static String getNonZeroPriceDisplay(Double price) {
43        if (price == null) return "";
44        if (price.doubleValue() == 0) return "";
45        return getPriceDisplay(price);
46      }
47  
48      public static String getPriceDisplay(Double price) {
49        if (price == null) return "";
50        double a = price.doubleValue();
51        if (a < 1) {
52          return new String((new Double(a*100)).intValue() + "p");
53        } else {
54          return new String(NumberFormat.getCurrencyInstance(Locale.UK).format(a));
55        }
56      }
57  
58    public static String getPercentDisplay(Double percent) {
59      if (percent == null) return "";
60      double a = percent.doubleValue();
61      return new String(new Double((new Double(a*100)).intValue() / 100) + "%");
62    }
63  
64    public static String trimTo(String s, int length) {
65      s = HTMLUtils.entitied(s,false, null);
66      if (s.length() > length) return s.substring(0,length) + "...";
67      return s;
68    }
69  
70    public static String CRLFtoBR(String str) {
71      int begin=0, end=0;
72      String returnStr="";
73      while(true) {
74        end=str.indexOf("\r\n", begin);
75        if(end==-1) return returnStr+=str.substring(begin);
76        returnStr+=str.substring(begin, end)+"<br>";
77        begin=end+1;
78      }
79    }
80    
81    public static String getUrlEncoded(String s) {
82      return UTF8URLEncoder.encode(s);
83    }
84    
85    public static int getPositionOfSlash(String s, int count) {
86      int c = 0;
87      int position = 0;
88      while (c < count && position > -1) {
89        position = s.indexOf("/",position);
90        position++;
91        c++;
92      }
93      return position;
94    }
95    
96    public static String getPathBeforeAge(String s) {
97      int p = getPositionOfSlash(s, 3);  
98      if (p > -1) s = s.substring(0,p);
99      return s;
100   }
101   
102   public static String getPathAfterAge(String s) {
103     int p = getPositionOfSlash(s, 4);
104     if (p > 1) s = s.substring(p-1, s.length());
105     p = getPositionOfSlash(s, 8);
106     if (p > 0) s = s.substring(0, p);
107     return s;
108   }
109   
110   public static void main(String[] args) {
111      System.out.println("path before age: " + getPathBeforeAge("//Dolls+houses/2_2/25_9999/house//////8/"));
112      System.out.println("path after age: " + getPathAfterAge("//Dolls+houses/2_2/25_9999/house//////8/"));
113   }
114 }