当前位置: 代码迷 >> Java Web开发 >> hibernate中一个抽象类MatchMode,源码记录
  详细解决方案

hibernate中一个抽象类MatchMode,源码记录

热度:205   发布时间:2011-04-14 10:50:12.0
hibernate中一个抽象类MatchMode,源码记录
该类给大家借鉴的地方,如果某个方法不能实现,而且又不想把继承类和继承层次搞得很复杂,可以使用类似MatchMode的实现方法。

public abstract class MatchMode implements Serializable {
    private final String name;
    private static final Map INSTANCES = new HashMap();

    protected MatchMode(String name) {
        this.name=name;
    }
    public String toString() {
        return name;
    }

    /**
     * Match the entire string to the pattern
     */
    public static final MatchMode EXACT = new MatchMode("EXACT") {
        public String toMatchString(String pattern) {
            return pattern;
        }
    };

    /**
     * Match the start of the string to the pattern
     */
    public static final MatchMode START = new MatchMode("START") {
        public String toMatchString(String pattern) {
            return pattern + '%';
        }
    };

    /**
     * Match the end of the string to the pattern
     */
    public static final MatchMode END = new MatchMode("END") {
        public String toMatchString(String pattern) {
            return '%' + pattern;
        }
    };

    /**
     * Match the pattern anywhere in the string
     */
    public static final MatchMode ANYWHERE = new MatchMode("ANYWHERE") {
        public String toMatchString(String pattern) {
            return '%' + pattern + '%';
        }
    };

    static {
        INSTANCES.put( EXACT.name, EXACT );
        INSTANCES.put( END.name, END );
        INSTANCES.put( START.name, START );
        INSTANCES.put( ANYWHERE.name, ANYWHERE );
    }

    private Object readResolve() {
        return INSTANCES.get(name);
    }

    /**
     * convert the pattern, by appending/prepending "%"
     */
    public abstract String toMatchString(String pattern);

}



[ 本帖最后由 西鄙人 于 2011-4-14 10:54 编辑 ]
搜索更多相关主题的帖子: return  

----------------解决方案--------------------------------------------------------
  相关解决方案