alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (NameModifierImpl.java)

This example Java source code file (NameModifierImpl.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

a\', illegalargumentexception, namemodifierimpl, string, stringbuffer

The NameModifierImpl.java Java example source code

/*
 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.tools.corba.se.idl.toJavaPortable ;

import com.sun.tools.corba.se.idl.toJavaPortable.NameModifier ;

public class NameModifierImpl implements NameModifier {
    private String prefix ;
    private String suffix ;

    public NameModifierImpl( )
    {
        this.prefix = null ;
        this.suffix = null ;
    }

    public NameModifierImpl( String prefix, String suffix )
    {
        this.prefix = prefix ;
        this.suffix = suffix ;
    }

    /** Construct a NameModifier from a pattern of the form xxx%xxx.
    * The pattern must consist of characters chosen from the
    * set [A-Za-z0-9%$_]. In addition, the pattern must contain
    * exactly one % character.  Finally, if % is not the first char in
    * the pattern, the pattern must not start with a number.
    * <p>
    * The semantics of makeName are very simply: just replace the
    * % character with the base in the pattern and return the result.
    */
    public NameModifierImpl( String pattern )
    {
        int first = pattern.indexOf( '%' ) ;
        int last  = pattern.lastIndexOf( '%' ) ;

        if (first != last)
            throw new IllegalArgumentException(
                Util.getMessage( "NameModifier.TooManyPercent" ) ) ;

        if (first == -1)
            throw new IllegalArgumentException(
                Util.getMessage( "NameModifier.NoPercent" ) ) ;

        for (int ctr = 0; ctr<pattern.length(); ctr++) {
            char ch = pattern.charAt( ctr ) ;
            if (invalidChar( ch, ctr==0 )) {
                char[] chars = new char[] { ch } ;
                throw new IllegalArgumentException(
                    Util.getMessage( "NameModifier.InvalidChar",
                        new String( chars )) ) ;
            }
        }

        // at this point, 0 <= first && first < pattern.length()
        prefix = pattern.substring( 0, first ) ;
        suffix = pattern.substring( first+1 ) ;
    }

    /** Return true if ch is invalid as a character in an
    * identifier.  If ch is a number, it is invalid only if
    * isFirst is true.
    */
    private boolean invalidChar( char ch, boolean isFirst )
    {
        if (('A'<=ch) && (ch<='Z'))
            return false ;
        else if (('a'<=ch) && (ch<='z'))
            return false ;
        else if (('0'<=ch) && (ch<='9'))
            return isFirst ;
        else if (ch=='%')
            return false ;
        else if (ch=='$')
            return false ;
        else if (ch=='_')
            return false ;
        else
            return true ;
    }

    public String makeName( String base )
    {
        StringBuffer sb = new StringBuffer() ;

        if (prefix != null)
            sb.append( prefix ) ;

        sb.append( base ) ;

        if (suffix != null)
            sb.append( suffix ) ;

        return sb.toString() ;
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java NameModifierImpl.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.