001/* 002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved. 003 * 004 * This program and the accompanying materials are made available under the terms of the 005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at 006 * http://www.eclipse.org/legal/epl-v20.html 007 */ 008package net.mdatools.modelant.core.operation.format; 009 010/** 011 * Convert a string to a constant name by:<ol> 012 * <li>Abbreviate each word in the name, except the last one to its first letter in upper case. 013 * <li>Add the whole last word to the abbreviated name, with first letter in upper case 014 * <li>Restrict the name up to <b>length</b> characters 015 * </ol> 016 * @author Rusi Popov (popovr@mdatools.net) 017 */ 018public class FormatAbbreviated extends FormatWordsString { 019 020 public FormatAbbreviated() { 021 super( "" ); 022 } 023 024 /** 025 * @see net.mdatools.modelant.core.operation.format.FormatWordsString#prepare(java.lang.String) 026 */ 027 protected final String prepare(String source) { 028 return source; 029 } 030 031 /** 032 * @see net.mdatools.modelant.core.operation.format.FormatWordsString#formatFirstWord(java.lang.StringBuilder, java.lang.String, boolean) 033 */ 034 protected void formatFirstWord(StringBuilder result, String word, boolean nextWordExists) { 035 addWord( result, word, nextWordExists ); 036 } 037 038 /** 039 * Abbreviate the provided word or capitalize its first letter 040 * @param result 041 * @param word 042 * @param nextWordExists 043 */ 044 private void addWord(StringBuilder result, String word, boolean nextWordExists) { 045 if (nextWordExists) { 046 result.append( Character.toUpperCase( word.charAt( 0 )) ); 047 } else { 048 formatFirstCapitalAllLower( result, word ); 049 } 050 } 051 052 /** 053 * @see net.mdatools.modelant.core.operation.format.FormatWordsString#formatNextWord(java.lang.StringBuilder, java.lang.String, boolean) 054 */ 055 protected void formatNextWord(StringBuilder result, String word, boolean nextWordExists) { 056 addWord( result, word, nextWordExists ); 057 } 058}