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 dashed-upper-case-start 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 PropertyNamerDUCS implements PropertyNamer {
033
034   /** Reusable instance. */
035   public static final PropertyNamer INSTANCE = new PropertyNamerDUCS();
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 = true;
057      int ni = 0;
058      for (int i = 0; i < name.length(); i++) {
059         char c = name.charAt(i);
060         if (i == 0) {
061            name2[ni++] = toUpperCase(c);
062         } else {
063            if (isUpperCase(c)) {
064               if (! isPrevUC) {
065                  name2[ni++] = '-';
066                  name2[ni++] = toUpperCase(c);
067               } else {
068                  name2[ni++] = toLowerCase(c);
069               }
070               isPrevUC = true;
071            } else {
072               isPrevUC = false;
073               name2[ni++] = c;
074            }
075         }
076      }
077
078      return new String(name2);
079   }
080}