All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.citation.Author Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.citation;



import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 Author object.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library 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 // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class Author { private String mRawName; // Un-parsed name private String mFirstName; private String mMiddleName; private String mLastName; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public Author() { } //--------------------------------------------------------------------------- public Author(String inValue) { mRawName = inValue; parse(inValue); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public String toString() { return (StringUtil.isSet(mRawName) ? mRawName : buildName()); } //--------------------------------------------------------------------------- public Author setFirstName(String inValue) { mFirstName = inValue; return this; } //--------------------------------------------------------------------------- public String getFirstName() { return mFirstName; } //--------------------------------------------------------------------------- public Character getFirstInitial() { return StringUtil.isSet(mFirstName) ? Character.toUpperCase(mFirstName.charAt(0)) : null; } //--------------------------------------------------------------------------- public Author setMiddleName(String inValue) { mMiddleName = inValue; return this; } //--------------------------------------------------------------------------- public String getMiddleName() { return mMiddleName; } //--------------------------------------------------------------------------- public Character getMiddleInitial() { return StringUtil.isSet(mMiddleName) ? Character.toUpperCase(mMiddleName.charAt(0)) : null; } //--------------------------------------------------------------------------- public Author setLastName(String inValue) { mLastName = inValue; return this; } //--------------------------------------------------------------------------- public String getLastName() { return mLastName; } //########################################################################### // PRIVATE METHODS //########################################################################### //--------------------------------------------------------------------------- public String buildName() { StringBuilderPlus buffer = new StringBuilderPlus(getLastName()); if (StringUtil.isSet(getFirstName())) { buffer.append(", " + getFirstName()); if (StringUtil.isSet(getMiddleName())) { buffer.append(" " + getMiddleName()); } } return buffer.toString(); } //--------------------------------------------------------------------------- private void parse(String inFullName) { if (StringUtil.isSet(inFullName)) { String[] pieces = inFullName.split("\\s+"); if (pieces.length > 1) { if (pieces[0].endsWith(",")) { // LastName, firstName mLastName = pieces[0].substring(0, pieces[0].length() - 1); mFirstName = pieces[1]; if (pieces.length > 2) { StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" "); for (int i = 2; i < pieces.length; i++) { buffer.delimitedAppend(pieces[i]); } mMiddleName = buffer.toString(); } } else if (pieces[0].length() > 1 && (1 == pieces[1].length() || (pieces[1].length() > 1 && pieces[1].charAt(1) == '.'))) { // LastName FirstInitial mLastName = pieces[0]; mFirstName = pieces[1]; if (pieces.length > 2) { StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" "); for (int i = 2; i < pieces.length; i++) { buffer.delimitedAppend(pieces[i]); } mMiddleName = buffer.toString(); } } else { // FirstName LastName mLastName = pieces[pieces.length - 1]; mFirstName = pieces[0]; if (pieces.length > 2) { StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" "); for (int i = 1; i < pieces.length - 1; i++) { buffer.delimitedAppend(pieces[i]); } mMiddleName = buffer.toString(); } } } else if (pieces[0].contains(",")) { pieces = pieces[0].split(","); mLastName = pieces[0]; mFirstName = pieces[1]; } if (StringUtil.isSet(mFirstName)) { if (mFirstName.length() > 1 && mFirstName.charAt(1) == '.') { if (mFirstName.length() > 2) { mMiddleName = mFirstName.substring(2); } mFirstName = mFirstName.substring(0, 1); } } if (StringUtil.isSet(mMiddleName) && mMiddleName.endsWith(".")) { mMiddleName = mMiddleName.substring(0, mMiddleName.length() - 1); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy