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

br.com.objectos.io.flat.TextTransformation Maven / Gradle / Ivy

/*
 * 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.flat;

import java.util.Set;

import br.com.objectos.io.flat.annotation.TextOption;

/**
 * @author [email protected] (Marcio Endo)
 */
enum TextTransformation {

  TRIM {
    @Override
    public String apply(String text, int length) {
      return text.trim();
    }
  },

  TRUNCATE {
    @Override
    public String apply(String text, int length) {
      String res = text;

      int textLength = text.length();
      if (textLength > length) {
        res = text.substring(0, length);
      }

      return res;
    }
  },

  LEFT_ALIGN {
    @Override
    public String apply(String text, int length) {
      String res = text;

      int textLength = text.length();
      if (textLength < length) {
        int diff = length - textLength;
        StringBuilder helper = new StringBuilder(text);
        for (int i = 0; i < diff; i++) {
          helper.append(' ');
        }
        res = helper.toString();
      }

      return res;
    }

    @Override
    public boolean matches(Set optionSet) {
      return !optionSet.contains(TextOption.RIGHT_ALIGN);
    }
  },

  RIGHT_ALIGN {
    @Override
    public String apply(String text, int length) {
      String res = text;

      int textLength = text.length();
      if (textLength < length) {
        int diff = length - textLength;
        StringBuilder helper = new StringBuilder(text).reverse();
        for (int i = 0; i < diff; i++) {
          helper.append(' ');
        }
        res = helper.reverse().toString();
      }

      return res;
    }

    @Override
    public boolean matches(Set optionSet) {
      return optionSet.contains(TextOption.RIGHT_ALIGN);
    }
  },

  UPPERCASE {
    @Override
    public String apply(String text, int length) {
      return text.toUpperCase();
    }

    @Override
    public boolean matches(Set optionSet) {
      return optionSet.contains(TextOption.UPPERCASE);
    }
  };

  public boolean matches(Set optionSet) {
    return true;
  }

  public abstract String apply(String text, int length);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy