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

br.com.objectos.io.xls.compiler.BooleanSheetMethodFormat Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 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.io.xls.compiler;

import java.util.Optional;

import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.io.xls.annotation.BooleanFormat;

import com.google.common.base.Strings;
import com.squareup.javapoet.MethodSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
class BooleanSheetMethodFormat extends SheetMethodFormat {

  private final String trueValue;
  private final String falseValue;

  private BooleanSheetMethodFormat(String trueValue, String falseValue) {
    this.trueValue = trueValue;
    this.falseValue = falseValue;
  }

  public static SheetMethodFormat get(MethodInfo methodInfo) {
    String trueValue = "";
    String falseValue = "";

    Optional maybeFormat = methodInfo.annotationInfo(BooleanFormat.class);
    if (maybeFormat.isPresent()) {
      AnnotationInfo format = maybeFormat.get();
      trueValue = format.stringValue("trueValue", "");
      falseValue = format.stringValue("falseValue", "");
    }

    if (trueValue.equals(falseValue)) {
      methodInfo.compilationError("trueValue() must be different than falseValue()");
    }

    return new BooleanSheetMethodFormat(trueValue, falseValue);
  }

  @Override
  public void toXls(MethodSpec.Builder toXls) {
    String trueVal = clean(trueValue);
    String falseVal = clean(falseValue);

    if ("".equals(trueVal)) {
      toXls.addCode(".falseAsText($S)", falseVal);
    } else if ("".equals(falseVal)) {
      toXls.addCode(".trueAsText($S)", falseVal);
    } else {
      toXls.addCode(".asText($S, $S)", trueVal, falseVal);
    }
  }

  private String clean(String value) {
    return Strings.nullToEmpty(value).trim();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy