br.com.objectos.io.flat.FlatEnumField 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 static br.com.objectos.io.flat.TextTransformation.LEFT_ALIGN;
import static br.com.objectos.io.flat.TextTransformation.TRUNCATE;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Set;
import com.google.common.base.Preconditions;
/**
* @author [email protected] (Marcio Endo)
*/
class FlatEnumField extends Field {
private static final Set SET = EnumSet.of(TRUNCATE, LEFT_ALIGN);
private final FlatEnum value;
private final int length;
private FlatEnumField(FlatEnum value, int length) {
this.value = value;
this.length = length;
}
public static FlatEnumField get(Enum> value, int length) {
Preconditions.checkArgument(value instanceof FlatEnum, "Enum must implement FlatEnum interface");
FlatEnum flatEnum = (FlatEnum) value;
return new FlatEnumField(flatEnum, length);
}
@Override
void tryToWriteTo(FlatAppendable out) throws IOException {
String text = value.flatValue();
for (TextTransformation transformation : SET) {
text = transformation.apply(text, length);
}
out.append(text);
}
}