Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae68a07a9d1afa1a9fb3bc9288b8145153ab6f1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***************************************************************************
 * Copyright (c) 2004-2007 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.internal.cdo.util;

import org.eclipse.net4j.util.StringUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Eike Stepper
 */
public final class GenUtil
{
  private GenUtil()
  {
  }

  /**
   * @see GenFeatureImpl#getUpperName
   */
  public static String getFeatureUpperName(String featureName)
  {
    return format(featureName, '_', null, false, true).toUpperCase();
  }

  /**
   * @see GenFeatureImpl#getGetAccessor
   */
  public static String getFeatureGetterName(String featureName, boolean isBooleanType)
  {
    String capName = StringUtil.cap(featureName);
    // if (isMapEntryFeature())
    // return "getTyped" + capName;
    String result = isBooleanType ? "is" + capName : "get" + ("Class".equals(capName) ? "Class_" : capName);

    // if (isListType() && !isFeatureMapType() && !isMapType() &&
    // getGenModel().isArrayAccessors())
    // {
    // result += "List";
    // }

    // GenClass rootImplementsInterface =
    // getGenModel().getRootImplementsInterfaceGenClass();
    // GenClass context = getContext();
    // if (rootImplementsInterface != null &&
    // !rootImplementsInterface.isEObject())
    // {
    // for (GenOperation genOperation :
    // rootImplementsInterface.getAllGenOperations())
    // {
    // if (genOperation.getName().equals(result) &&
    // genOperation.getGenParameters().isEmpty() &&
    // !genOperation.getType(context).equals(getType(context)))
    // {
    // result = result + "_";
    // break;
    // }
    // }
    // }

    return result;
  }

  /**
   * Formats a name by parsing it into words separated by underscores and/or
   * mixed-casing and then recombining them using the specified separator. A
   * prefix can also be given to be recognized as a separate word or to be
   * trimmed. Leading underscores can be ignored or can cause a leading
   * separator to be prepended.
   * 
   * @since 2.2
   */
  public static String format(String name, char separator, String prefix, boolean includePrefix,
      boolean includeLeadingSeparator)
  {
    String leadingSeparators = includeLeadingSeparator ? getLeadingSeparators(name, '_') : null;
    if (leadingSeparators != null)
    {
      name = name.substring(leadingSeparators.length());
    }

    List<String> parsedName = new ArrayList<String>();
    if (prefix != null && name.startsWith(prefix) && name.length() > prefix.length()
        && Character.isUpperCase(name.charAt(prefix.length())))
    {
      name = name.substring(prefix.length());
      if (includePrefix)
      {
        parsedName = parseName(prefix, '_');
      }
    }

    if (name.length() != 0) parsedName.addAll(parseName(name, '_'));

    StringBuilder result = new StringBuilder();

    for (Iterator<String> nameIter = parsedName.iterator(); nameIter.hasNext();)
    {
      String nameComponent = nameIter.next();
      result.append(nameComponent);

      if (nameIter.hasNext() && nameComponent.length() > 1)
      {
        result.append(separator);
      }
    }

    if (result.length() == 0 && prefix != null)
    {
      result.append(prefix);
    }
    return leadingSeparators != null ? "_" + result.toString() : result.toString();
  }

  /**
   * This method breaks sourceName into words delimited by separator and/or
   * mixed-case naming.
   */
  public static List<String> parseName(String sourceName, char separator)
  {
    List<String> result = new ArrayList<String>();
    if (sourceName != null)
    {
      StringBuilder currentWord = new StringBuilder();
      boolean lastIsLower = false;
      for (int index = 0, length = sourceName.length(); index < length; ++index)
      {
        char curChar = sourceName.charAt(index);
        if (Character.isUpperCase(curChar) || !lastIsLower && Character.isDigit(curChar) || curChar == separator)
        {
          if (lastIsLower && currentWord.length() > 1 || curChar == separator && currentWord.length() > 0)
          {
            result.add(currentWord.toString());
            currentWord = new StringBuilder();
          }
          lastIsLower = false;
        }
        else
        {
          if (!lastIsLower)
          {
            int currentWordLength = currentWord.length();
            if (currentWordLength > 1)
            {
              char lastChar = currentWord.charAt(--currentWordLength);
              currentWord.setLength(currentWordLength);
              result.add(currentWord.toString());
              currentWord = new StringBuilder();
              currentWord.append(lastChar);
            }
          }
          lastIsLower = true;
        }

        if (curChar != separator)
        {
          currentWord.append(curChar);
        }
      }

      result.add(currentWord.toString());
    }
    return result;
  }

  private static String getLeadingSeparators(String name, char separator)
  {
    int i = 0;
    for (int len = name.length(); i < len && name.charAt(i) == separator; i++)
    {
      // the for loop's condition finds the separator
    }
    return i != 0 ? name.substring(0, i) : null;
  }
}

Back to the top