/** * Converts class name to property name using JavaBean decapitalization. * * @param name The class name * @param suffixes The suffix to remove * @return The decapitalized name */ public static String decapitalizeWithoutSuffix(String name, String... suffixes) { String decapitalized = decapitalize(name); return trimSuffix(decapitalized, suffixes); }
/** * The camel case version of the string with the first letter in lower case. * * @param str The string * @return The new string in camel case */ public static String camelCase(String str) { return camelCase(str, true); }
/** * Converts camel case to hyphenated, lowercase form. * * @param name The name * @return The hyphenated string */ public static String hyphenate(String name) { return hyphenate(name, true); }
@Override protected String getFallbackFormat(Argument argument) { return NameUtils.hyphenate(NameUtils.capitalize(argument.getName()), false); } }
/** * Format a string according to a convention. * * @param convention The string convention to use * @param str The string to format * @return The formatted string based on the convention */ public static String format(StringConvention convention, String str) { switch (convention) { case CAMEL_CASE: return NameUtils.camelCase(str); case HYPHENATED: return NameUtils.hyphenate(str); case UNDER_SCORE_SEPARATED_LOWER_CASE: return NameUtils.underscoreSeparate(str.toLowerCase(Locale.ENGLISH)); case UNDER_SCORE_SEPARATED: return NameUtils.environmentName(str); case CAMEL_CASE_CAPITALIZED: return NameUtils.camelCase(str, false); case RAW: default: return str; } } }
/** * <p>Returns the hyphenated equivalent for this convention for the given class. Eg. BookShopController -> book-shop</p> * * @param type The type * @return The property name equivalent */ public String asHyphenatedName(Class type) { String shortName = NameUtils.trimSuffix(type.getSimpleName(), suffix); return NameUtils.hyphenate(shortName); }
/** * Get the equivalent property name for the given setter. * * @param setterName The setter * @return The property name */ public static String getPropertyNameForSetter(String setterName) { if (isSetterName(setterName)) { return decapitalize(setterName.substring(PREFIX_LENTGH)); } return setterName; }
/** * Get the equivalent property name for the given getter. * * @param getterName The getter * @return The property name */ public static String getPropertyNameForGetter(String getterName) { if (isGetterName(getterName)) { int prefixLength = 0; if (getterName.startsWith("get")) { prefixLength = PREFIX_LENTGH; } if (getterName.startsWith("is")) { prefixLength = IS_LENTGH; } return decapitalize(getterName.substring(prefixLength)); } return getterName; }
/** * Resolve the {@link MediaType} for the given file name. Defaults * to text/plain. * * @param filename The file name * @return The {@link MediaType} */ public static MediaType forFilename(String filename) { if (StringUtils.isNotEmpty(filename)) { return forExtension(NameUtils.extension(filename)).orElse(MediaType.TEXT_PLAIN_TYPE); } return MediaType.TEXT_PLAIN_TYPE; }
/** * Creates a new argument for the given type and name. * * @param type The type * @param <T> The generic type * @return The argument instance */ @UsedByGeneratedCode static <T> Argument<T> of( Class<T> type) { return new DefaultArgument<>(type, NameUtils.decapitalize(type.getSimpleName()), AnnotationMetadata.EMPTY_METADATA, Argument.ZERO_ARGUMENTS); }
/** * Default constructor. */ TypeConvention() { this.suffix = NameUtils.capitalize(name().toLowerCase(Locale.ENGLISH)); }
private void registerMicronautChannelHandlers(ChannelPipeline pipeline) { int i = 0; for (ChannelHandler outboundHandlerAdapter : outboundHandlers) { String name; if (outboundHandlerAdapter instanceof Named) { name = ((Named) outboundHandlerAdapter).getName(); } else { name = NettyHttpServer.MICRONAUT_HANDLER + NettyHttpServer.OUTBOUND_KEY + ++i; } pipeline.addAfter(NettyHttpServer.HTTP_CODEC, name, outboundHandlerAdapter); } }
/** * Converts camel case to hyphenated, lowercase form. * * @param name The name * @param lowerCase Whether the result should be converted to lower case * @return The hyphenated string */ public static String hyphenate(String name, boolean lowerCase) { if (isHyphenatedLowerCase(name)) { return name.replace('_', '-'); } else { char separatorChar = '-'; return separateCamelCase(name.replace('_', '-'), lowerCase, separatorChar); } }
/** * Returns the underscore separated version of the given camel case string. * * @param camelCase The camel case name * @return The underscore separated version */ public static String underscoreSeparate(String camelCase) { return separateCamelCase(camelCase.replace('-', '_'), false, '_'); }
/** * <p>Returns the property name equivalent for this convention for the given class. Eg. BookController -> book</p> * * @param type The type * @return The property name equivalent */ public String asPropertyName(Class type) { return NameUtils.decapitalizeWithoutSuffix(type.getSimpleName(), suffix); }
/** * Is the method a setter. * * @param name The method name * @param args The arguments * @return True if it is */ public static boolean isSetter(String name, Class[] args) { if (StringUtils.isEmpty(name) || args == null) { return false; } if (args.length != 1) { return false; } return NameUtils.isSetterName(name); }
@Override protected String getFallbackFormat(Argument argument) { return NameUtils.hyphenate(argument.getName()); } }
@Override protected String getFallbackFormat(Argument argument) { return NameUtils.hyphenate(NameUtils.capitalize(argument.getName()), false); } }
/** * Returns the underscore separated version of the given camel case string. * * @param camelCase The camel case name * @return The underscore separated version */ public static String environmentName(String camelCase) { return separateCamelCase(camelCase.replace('-', '_').replace('.', '_'), false, '_') .toUpperCase(Locale.ENGLISH); }
/** * @param argument The argument * @return The fallback format */ protected String getFallbackFormat(Argument argument) { return NameUtils.hyphenate(argument.getName()); }