All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.css.Naming Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.css;

import java.util.Set;

import javax.lang.model.SourceVersion;

import br.com.objectos.code.Code;
import br.com.objectos.core.util.ImmutableSet;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeVariableName;

/**
 * @author [email protected] (Marcio Endo)
 */
class Naming {

  private static final Set ObjectMethodSet = ImmutableSet. builder()
      .add("wait")
      .build();

  public static final TypeVariableName SELF = TypeVariableName.get("SELF");

  private static final ClassName AbstractRuleSetBuilder = ClassName.get(AbstractRuleSetBuilder.class);
  public static final ClassName RuleSetBuilder = ClassName.get("br.com.objectos.css", "RuleSetBuilder");
  private static final TypeName RuleSetBuilder_SELF = ParameterizedTypeName.get(RuleSetBuilder, SELF);

  public static final TypeName AbstractRuleSetBuilder_SELF = ParameterizedTypeName.get(AbstractRuleSetBuilder, SELF);
  public static final TypeVariableName SELF_extends_RuleSetBuilder = TypeVariableName.get("SELF", RuleSetBuilder_SELF);

  private Naming() {
  }

  public static String auxClassName(String name) {
    return new StringBuilder()
        .append(Code.upperCaseFirstChar(name))
        .append("Builder")
        .toString();
  }

  public static String enumNameToIdentifier(String name) {
    if (name.startsWith("_")) {
      return name;
    }

    StringBuilder res = new StringBuilder();

    String[] parts = name.toLowerCase().split("_");
    res.append(parts[0]);

    if (parts.length > 1) {
      for (int i = 1; i < parts.length; i++) {
        String part = parts[i];
        res.append(Code.upperCaseFirstChar(part));
      }
    }

    if (SourceVersion.isKeyword(res) || ObjectMethodSet.contains(res.toString())) {
      res.append("Value");
    }

    return res.toString();
  }

  public static String methodNameToProperty(String name) {
    StringBuilder res = new StringBuilder();

    char[] charArray = name.toCharArray();
    for (char ch : charArray) {
      if (Character.isUpperCase(ch)) {
        res.append('-');
        res.append(Character.toLowerCase(ch));
      } else {
        res.append(ch);
      }
    }

    return res.toString();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy