br.com.objectos.io.flat.Line 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.flat;
import java.util.Arrays;
/**
* @author [email protected] (Marcio Endo)
*/
public class Line {
private final int number;
private final String text;
public Line(int number, String text) {
this.number = number;
this.text = text;
}
public int number() {
return number;
}
public String text() {
return text;
}
@Override
public String toString() {
return String.format("%05d: %s", number, text);
}
int length() {
return text.length();
}
String substring(int index, int endIndex) {
int length = endIndex - index;
if (text == null) {
return blank(length);
}
if (index >= text.length()) {
return blank(length);
}
if (endIndex > text.length()) {
String substring = text.substring(index);
return substring + blank(length - substring.length());
}
return text.substring(index, endIndex);
}
private String blank(int size) {
char[] array = new char[size];
Arrays.fill(array, ' ');
return new String(array);
}
}