001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015import static java.lang.Character.*;
016import static org.apache.juneau.common.internal.StringUtils.*;
017
018/**
019 * Converts property names to underscore-lower-case format.
020 *
021 * <h5 class='section'>Example:</h5>
022 * <ul>
023 *    <li><js>"fooBar"</js> -&gt; <js>"foo_bar"</js>
024 *    <li><js>"fooBarURL"</js> -&gt; <js>"foo_bar_url"</js>
025 *    <li><js>"FooBarURL"</js> -&gt; <js>"foo_bar_url"</js>
026 * </ul>
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029
030 * </ul>
031 */
032public final class PropertyNamerULC implements PropertyNamer {
033
034   /** Reusable instance. */
035   public static final PropertyNamer INSTANCE = new PropertyNamerULC();
036
037   @Override /* PropertyNamer */
038   public String getPropertyName(String name) {
039      if (isEmpty(name))
040         return name;
041
042      int numUCs = 0;
043      boolean isPrevUC = isUpperCase(name.charAt(0));
044      for (int i = 1; i < name.length(); i++) {
045         char c = name.charAt(i);
046         if (isUpperCase(c)) {
047            if (! isPrevUC)
048               numUCs++;
049            isPrevUC = true;
050         } else {
051            isPrevUC = false;
052         }
053      }
054
055      char[] name2 = new char[name.length() + numUCs];
056      isPrevUC = isUpperCase(name.charAt(0));
057      int ni = 0;
058      for (int i = 0; i < name.length(); i++) {
059         char c = name.charAt(i);
060         if (isUpperCase(c)) {
061            if (! isPrevUC)
062               name2[ni++] = '_';
063            isPrevUC = true;
064            name2[ni++] = toLowerCase(c);
065         } else {
066            isPrevUC = false;
067            name2[ni++] = c;
068         }
069      }
070
071      return new String(name2);
072   }
073}