br.com.objectos.io.flat.TextField 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.io.IOException;
import java.util.Objects;
import java.util.Set;
import br.com.objectos.io.flat.annotation.TextOption;
import com.google.common.collect.ImmutableSet;
/**
* @author [email protected] (Marcio Endo)
*/
class TextField extends Field {
private final String value;
private final int length;
private final Set optionSet;
private TextField(String value, int length, Set optionSet) {
this.value = value;
this.length = length;
this.optionSet = optionSet;
}
public static TextField get(String value, int length, TextOption... options) {
Objects.requireNonNull(value, "value");
Set optionSet = ImmutableSet.copyOf(options);
return new TextField(value, length, optionSet);
}
@Override
void tryToWriteTo(FlatAppendable out) throws IOException {
String text = value;
for (TextTransformation transformation : TextTransformation.values()) {
if (transformation.matches(optionSet)) {
text = transformation.apply(text, length);
}
}
out.append(text);
}
}