⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collectioneditor.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one *  or more contributor license agreements.  See the NOTICE file *  distributed with this work for additional information *  regarding copyright ownership.  The ASF licenses this file *  to you under the Apache License, Version 2.0 (the *  "License"); you may not use this file except in compliance *  with the License.  You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, *  software distributed under the License is distributed on an *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *  KIND, either express or implied.  See the License for the *  specific language governing permissions and limitations *  under the License. * */package org.apache.mina.integration.beans;import java.beans.PropertyEditor;import java.util.ArrayList;import java.util.Collection;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * A {@link PropertyEditor} which converts a {@link String} into * a {@link Collection} and vice versa. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Revision: 601229 $, $Date: 2007-12-05 08:13:18 +0100 (Wed, 05 Dec 2007) $ */public class CollectionEditor extends AbstractPropertyEditor {    static final Pattern ELEMENT = Pattern.compile(            "([,\\s]+)|" + // Delimiter            "(?<=\")((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^\"])*)(?=\")|" +            "(?<=')((?:\\\\\"|\\\\'|\\\\\\\\|\\\\ |[^'])*)(?=')|" +            "((?:[^\\\\\\s'\",]|\\\\ |\\\\\"|\\\\')+)");        private final Class<?> elementType;        public CollectionEditor(Class<?> elementType) {        if (elementType == null) {            throw new NullPointerException("elementType");        }                this.elementType = elementType;        getElementEditor();        setTrimText(false);    }    private PropertyEditor getElementEditor() {        PropertyEditor e = PropertyEditorFactory.getInstance(elementType);        if (e == null) {            throw new IllegalArgumentException(                    "No " + PropertyEditor.class.getSimpleName() +                     " found for " + elementType.getSimpleName() + '.');        }        return e;    }    @Override    @SuppressWarnings("unchecked")    protected final String toText(Object value) {        StringBuilder buf = new StringBuilder();        for (Object v: (Collection) value) {            if (v == null) {                v = defaultElement();            }                        PropertyEditor e = PropertyEditorFactory.getInstance(v);            if (e == null) {                throw new IllegalArgumentException(                        "No " + PropertyEditor.class.getSimpleName() +                         " found for " + v.getClass().getSimpleName() + '.');            }                        e.setValue(v);            // TODO normalize.            String s = e.getAsText();            buf.append(s);            buf.append(", ");        }                // Remove the last delimiter.        if (buf.length() >= 2) {            buf.setLength(buf.length() - 2);        }        return buf.toString();    }    @Override    protected final Object toValue(String text) throws IllegalArgumentException {        PropertyEditor e = getElementEditor();        Collection<Object> answer = newCollection();        Matcher m = ELEMENT.matcher(text);        boolean matchedDelimiter = true;        while (m.find()) {            if (m.group(1) != null) {                matchedDelimiter = true;                continue;            }                        if (!matchedDelimiter) {                throw new IllegalArgumentException("No delimiter between elements: " + text);            }            // TODO escape here.            e.setAsText(m.group());            answer.add(e.getValue());                        matchedDelimiter = false;            if (m.group(2) != null || m.group(3) != null) {                // Skip the last '"'.                m.region(m.end() + 1, m.regionEnd());            }        }                return answer;    }        protected Collection<Object> newCollection() {        return new ArrayList<Object>();    }        protected Object defaultElement() {        PropertyEditor e = PropertyEditorFactory.getInstance(elementType);        if (e == null) {            return null;        }                if (e instanceof AbstractPropertyEditor) {            return ((AbstractPropertyEditor) e).defaultValue();        }        return null;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -