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
010import net.mdatools.modelant.core.api.Operation;
011
012/**
013 * Convert a string to a constant name by:<ol>
014 * <li>Abbreviate each word in the name, except the last one to its first letter in upper case.
015 * <li>Add the whole last word to the abbreviated name, with first letter in upper case
016 * <li>Restrict the name up to <b>length</b> characters
017 * </ol>
018 * @see FormatAbbreviated
019 * @author Rusi Popov (popovr@mdatools.net)
020 */
021public class FormatMaxLength implements Operation<String> {
022
023  private final int length;
024
025  /**
026   * @param length is the maximum name size where to fit the abbreviated name
027   */
028  public FormatMaxLength(int length) {
029    this.length = length;
030  }
031
032
033  /**
034   * @param name is a string to construct abbreviated constant name
035   * @return the a
036   */
037  public final String execute(String name) throws RuntimeException, IllegalArgumentException {
038    String result;
039    int limit;
040
041    limit = Math.min( length, name.length() );
042
043    result = name.substring( 0, limit );
044    return result;
045  }
046}