com.feilong.core.util.comparator.RegexGroupNumberComparator.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.util.comparator.RegexGroupNumberComparator.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed 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 com.feilong.core.util.comparator;

import java.io.Serializable;
import java.util.Comparator;

import com.feilong.core.util.RegexUtil;

/**
 * ? number group .
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * <p>
 * ? "ppt-coreContent2","ppt-coreContent13","ppt-coreContent12",<br>
 * ?, ppt-coreContent<b>13</b>  ppt-coreContent<b>2</b> ??
 * </p>
 * 
 * <p>
 * ?,?
 * </p>
 * 
 * <pre class="code">
 * Collections.sort(includedFileUrlList, new RegexGroupNumberComparator(".*ppt-coreContent(\\d*).png"));
 * </pre>
 * 
 * </blockquote>
 * 
 * <p>
 * ???? <span style="color:red">group ?int </span> 
 * </p>
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see "org.apache.commons.io.comparator.NameFileComparator"
 * @see "org.apache.commons.io.comparator.DirectoryFileComparator"
 * @see "org.apache.commons.io.comparator.DefaultFileComparator"
 * @see "org.apache.commons.io.comparator.ExtensionFileComparator"
 * @see "org.apache.commons.io.comparator.PathFileComparator"
 * @see "org.apache.commons.io.comparator.SizeFileComparator"
 * @since 1.4.0
 */
public class RegexGroupNumberComparator implements Comparator<String>, Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -3400735378238717278L;

    /** ???,????, ".*ppt-coreContent(\\d*).png". */
    private final String regexPattern;

    /**
     * The Constructor.
     * <p style="color:red">
     * ????group ?int 
     * </p>
     * 
     * <p>
     *  ? "ppt-coreContent2","ppt-coreContent13","ppt-coreContent12",?, ppt-coreContent13 ppt-coreContent2??
     * </p>
     * 
     * <p>
     * ?,?
     * </p>
     * 
     * <pre class="code">
     * Collections.sort(includedFileUrlList, new RegexGroupNumberComparator(".*ppt-coreContent(\\d*).png"));
     * </pre>
     * 
     * @param regexPattern
     *            ???,????, ".*ppt-coreContent(\\d*).png"
     */
    public RegexGroupNumberComparator(String regexPattern) {
        this.regexPattern = regexPattern;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    @Override
    public int compare(String s1, String s2) {
        String group1 = RegexUtil.group(regexPattern, s1, 1);
        String group2 = RegexUtil.group(regexPattern, s2, 1);

        Integer parseInt = Integer.parseInt(group1);
        Integer parseInt2 = Integer.parseInt(group2);
        return parseInt.compareTo(parseInt2);
    }
}