stack/core/src/main/java/org/apache/usergrid/utils/Inflector.java [33:181]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class Inflector {

    protected static final Inflector INSTANCE = new Inflector();


    public static Inflector getInstance() {
        return INSTANCE;
    }


    protected class Rule {

        protected final String expression;
        protected final Pattern expressionPattern;
        protected final String replacement;


        protected Rule( String expression, String replacement ) {
            this.expression = expression;
            this.replacement = replacement != null ? replacement : "";
            expressionPattern = Pattern.compile( this.expression, Pattern.CASE_INSENSITIVE );
        }


        /**
         * Apply the rule against the input string, returning the modified string or null if the rule didn't apply (and
         * no modifications were made)
         *
         * @param input the input string
         *
         * @return the modified string if this rule applied, or null if the input was not modified by this rule
         */
        protected String apply( String input ) {
            Matcher matcher = expressionPattern.matcher( input );
            if ( !matcher.find() ) {
                return null;
            }
            return matcher.replaceAll( replacement );
        }


        @Override
        public int hashCode() {
            return expression.hashCode();
        }


        @Override
        public boolean equals( Object obj ) {
            if ( obj == this ) {
                return true;
            }
            if ( ( obj != null ) && ( obj.getClass() == this.getClass() ) ) {
                final Rule that = ( Rule ) obj;
                if ( expression.equalsIgnoreCase( that.expression ) ) {
                    return true;
                }
            }
            return false;
        }


        @Override
        public String toString() {
            return expression + ", " + replacement;
        }
    }


    private final LinkedList<Rule> plurals = new LinkedList<Rule>();
    private final LinkedList<Rule> singulars = new LinkedList<Rule>();
    /**
     * The lowercase words that are to be excluded and not processed. This map can be modified by the users via {@link
     * #getUncountables()}.
     */
    private final Set<String> uncountables = new HashSet<String>();


    public Inflector() {
        initialize();
    }


    protected Inflector( Inflector original ) {
        plurals.addAll( original.plurals );
        singulars.addAll( original.singulars );
        uncountables.addAll( original.uncountables );
    }


    @Override
    @SuppressWarnings("all")
    public Inflector clone() {
        return new Inflector( this );
    }

    // ------------------------------------------------------------------------------------------------
    // Usage functions
    // ------------------------------------------------------------------------------------------------


    /**
     * Returns the plural form of the word in the string. <p> Examples: <p/>
     * <pre>
     *   inflector.pluralize(&quot;post&quot;)               #=&gt; &quot;posts&quot;
     *   inflector.pluralize(&quot;octopus&quot;)            #=&gt; &quot;octopi&quot;
     *   inflector.pluralize(&quot;sheep&quot;)              #=&gt; &quot;sheep&quot;
     *   inflector.pluralize(&quot;words&quot;)              #=&gt; &quot;words&quot;
     *   inflector.pluralize(&quot;the blue mailman&quot;)   #=&gt; &quot;the blue mailmen&quot;
     *   inflector.pluralize(&quot;CamelOctopus&quot;)       #=&gt; &quot;CamelOctopi&quot;
     * </pre>
     * <p/> </p> <p> Note that if the {@link Object#toString()} is called on the supplied object, so this method works
     * for non-strings, too. </p>
     *
     * @param word the word that is to be pluralized.
     *
     * @return the pluralized form of the word, or the word itself if it could not be pluralized
     *
     * @see #singularize(Object)
     */
    public String pluralize( Object word ) {
        if ( word == null ) {
            return null;
        }
        String wordStr = word.toString().trim();
        if ( wordStr.length() == 0 ) {
            return wordStr;
        }
        if ( isUncountable( wordStr ) ) {
            return wordStr;
        }
        for ( Rule rule : plurals ) {
            String result = rule.apply( wordStr );
            if ( result != null ) {
                return result;
            }
        }
        return wordStr;
    }


    public String pluralize( Object word, int count ) {
        if ( word == null ) {
            return null;
        }
        if ( ( count == 1 ) || ( count == -1 ) ) {
            return word.toString();
        }
        return pluralize( word );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



stack/corepersistence/collection/src/main/java/org/apache/usergrid/persistence/collection/serialization/impl/util/Inflector.java [35:183]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public class Inflector {

    protected static final Inflector INSTANCE = new Inflector();


    public static Inflector getInstance() {
        return INSTANCE;
    }


    protected class Rule {

        protected final String expression;
        protected final Pattern expressionPattern;
        protected final String replacement;


        protected Rule( String expression, String replacement ) {
            this.expression = expression;
            this.replacement = replacement != null ? replacement : "";
            expressionPattern = Pattern.compile( this.expression, Pattern.CASE_INSENSITIVE );
        }


        /**
         * Apply the rule against the input string, returning the modified string or null if the rule didn't apply (and
         * no modifications were made)
         *
         * @param input the input string
         *
         * @return the modified string if this rule applied, or null if the input was not modified by this rule
         */
        protected String apply( String input ) {
            Matcher matcher = expressionPattern.matcher( input );
            if ( !matcher.find() ) {
                return null;
            }
            return matcher.replaceAll( replacement );
        }


        @Override
        public int hashCode() {
            return expression.hashCode();
        }


        @Override
        public boolean equals( Object obj ) {
            if ( obj == this ) {
                return true;
            }
            if ( ( obj != null ) && ( obj.getClass() == this.getClass() ) ) {
                final Rule that = ( Rule ) obj;
                if ( expression.equalsIgnoreCase( that.expression ) ) {
                    return true;
                }
            }
            return false;
        }


        @Override
        public String toString() {
            return expression + ", " + replacement;
        }
    }


    private final LinkedList<Rule> plurals = new LinkedList<Rule>();
    private final LinkedList<Rule> singulars = new LinkedList<Rule>();
    /**
     * The lowercase words that are to be excluded and not processed. This map can be modified by the users via {@link
     * #getUncountables()}.
     */
    private final Set<String> uncountables = new HashSet<String>();


    public Inflector() {
        initialize();
    }


    protected Inflector( Inflector original ) {
        plurals.addAll( original.plurals );
        singulars.addAll( original.singulars );
        uncountables.addAll( original.uncountables );
    }


    @Override
    @SuppressWarnings("all")
    public Inflector clone() {
        return new Inflector( this );
    }

    // ------------------------------------------------------------------------------------------------
    // Usage functions
    // ------------------------------------------------------------------------------------------------


    /**
     * Returns the plural form of the word in the string. <p> Examples: <p/>
     * <pre>
     *   inflector.pluralize(&quot;post&quot;)               #=&gt; &quot;posts&quot;
     *   inflector.pluralize(&quot;octopus&quot;)            #=&gt; &quot;octopi&quot;
     *   inflector.pluralize(&quot;sheep&quot;)              #=&gt; &quot;sheep&quot;
     *   inflector.pluralize(&quot;words&quot;)              #=&gt; &quot;words&quot;
     *   inflector.pluralize(&quot;the blue mailman&quot;)   #=&gt; &quot;the blue mailmen&quot;
     *   inflector.pluralize(&quot;CamelOctopus&quot;)       #=&gt; &quot;CamelOctopi&quot;
     * </pre>
     * <p/> </p> <p> Note that if the {@link Object#toString()} is called on the supplied object, so this method works
     * for non-strings, too. </p>
     *
     * @param word the word that is to be pluralized.
     *
     * @return the pluralized form of the word, or the word itself if it could not be pluralized
     *
     * @see #singularize(Object)
     */
    public String pluralize( Object word ) {
        if ( word == null ) {
            return null;
        }
        String wordStr = word.toString().trim();
        if ( wordStr.length() == 0 ) {
            return wordStr;
        }
        if ( isUncountable( wordStr ) ) {
            return wordStr;
        }
        for ( Rule rule : plurals ) {
            String result = rule.apply( wordStr );
            if ( result != null ) {
                return result;
            }
        }
        return wordStr;
    }


    public String pluralize( Object word, int count ) {
        if ( word == null ) {
            return null;
        }
        if ( ( count == 1 ) || ( count == -1 ) ) {
            return word.toString();
        }
        return pluralize( word );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



