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