001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.util;
018    
019    import java.beans.PropertyEditorSupport;
020    import java.util.regex.Matcher;
021    import java.util.regex.Pattern;
022    
023    /**
024     * Used by xbean to set integers.
025     * <p/>
026     * <b>Important: </b> Do not use this for other purposes than xbean, as property editors
027     * are not thread safe, and they are slow to use.
028     * <p/>
029     * Converts string values like "20 Mb", "1024kb", and "1g" to int values in
030     * bytes.
031     */
032    public class MemoryIntPropertyEditor extends PropertyEditorSupport {
033        public void setAsText(String text) throws IllegalArgumentException {
034    
035            Pattern p = Pattern.compile("^\\s*(\\d+)\\s*(b)?\\s*$", Pattern.CASE_INSENSITIVE);
036            Matcher m = p.matcher(text);
037            if (m.matches()) {
038                setValue(Integer.valueOf(Integer.parseInt(m.group(1))));
039                return;
040            }
041    
042            p = Pattern.compile("^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE);
043            m = p.matcher(text);
044            if (m.matches()) {
045                setValue(Integer.valueOf(Integer.parseInt(m.group(1)) * 1024));
046                return;
047            }
048    
049            p = Pattern.compile("^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE);
050            m = p.matcher(text);
051            if (m.matches()) {
052                setValue(Integer.valueOf(Integer.parseInt(m.group(1)) * 1024 * 1024));
053                return;
054            }
055    
056            p = Pattern.compile("^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE);
057            m = p.matcher(text);
058            if (m.matches()) {
059                setValue(Integer.valueOf(Integer.parseInt(m.group(1)) * 1024 * 1024 * 1024));
060                return;
061            }
062    
063            throw new IllegalArgumentException("Could convert not to a memory size: " + text);
064        }
065    
066        public String getAsText() {
067            Integer value = (Integer)getValue();
068            return value != null ? value.toString() : "";
069        }
070    
071    }