1 18 package org.apache.activemq.util; 19 20 import java.beans.PropertyEditorSupport ; 21 import java.util.regex.Matcher ; 22 import java.util.regex.Pattern ; 23 24 29 public class MemoryIntPropertyEditor extends PropertyEditorSupport { 30 public void setAsText(String text) throws IllegalArgumentException { 31 32 Pattern p = Pattern.compile("^\\s*(\\d+)\\s*(b)?\\s*$",Pattern.CASE_INSENSITIVE); 33 Matcher m = p.matcher(text); 34 if (m.matches()) { 35 setValue(new Integer (Integer.parseInt(m.group(1)))); 36 return; 37 } 38 39 p = Pattern.compile("^\\s*(\\d+)\\s*k(b)?\\s*$",Pattern.CASE_INSENSITIVE); 40 m = p.matcher(text); 41 if (m.matches()) { 42 setValue(new Integer (Integer.parseInt(m.group(1)) * 1024)); 43 return; 44 } 45 46 p = Pattern.compile("^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE); 47 m = p.matcher(text); 48 if (m.matches()) { 49 setValue(new Integer (Integer.parseInt(m.group(1)) * 1024 * 1024 )); 50 return; 51 } 52 53 p = Pattern.compile("^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE); 54 m = p.matcher(text); 55 if (m.matches()) { 56 setValue(new Integer (Integer.parseInt(m.group(1)) * 1024 * 1024 * 1024 )); 57 return; 58 } 59 60 throw new IllegalArgumentException ( 61 "Could convert not to a memory size: " + text); 62 } 63 64 public String getAsText() { 65 Integer value = (Integer ) getValue(); 66 return (value != null ? value.toString() : ""); 67 } 68 69 } 70 | Popular Tags |