可以优化,但它的工作解决方案:
公共类 ParagraphsAndFigures {
public static ArrayList<PageContent> generatePages(List<Paragraph> paragraphs, int L) {
ArrayList<PageContent> pages = new ArrayList<PageContent>();
for (int i = 0; i < paragraphs.size() * 2; i++) {
pages.add(new PageContent());
}
int page = 0;
for (Paragraph paragraph : paragraphs) {
do {
int cur = pages.get(page).linesReserved;
int next = pages.get(page + 1).linesReserved;
if (cur + paragraph.size < L) {
cur += paragraph.size;
if (paragraph.figure != null) {
if (pages.get(page + 1).hasPicture()) {
if (next + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page + 1).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page + 1).linesReserved += paragraph.figure.size;
break; // next paragraph
} else {
page++;
continue;
}
}
if (pages.get(page).hasPicture()) {
if (cur + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page).linesReserved += paragraph.figure.size;
break; // next paragraph
} else {
if (next + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page + 1).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page + 1).linesReserved += paragraph.figure.size;
break; // next paragraph
}
page++;
continue;
}
}
if (page != 0 && pages.get(page - 1).hasPicture()) {
int prev = pages.get(page - 1).linesReserved;
if (prev + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page - 1).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page - 1).linesReserved += paragraph.figure.size;
break; // next paragraph
} else {
if (cur + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page).linesReserved += paragraph.figure.size;
break; // next paragraph
}
if (next + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page + 1).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page + 1).linesReserved += paragraph.figure.size;
break; // next paragraph
}
page++;
}
}
if (page != 0) {
int prev = pages.get(page - 1).linesReserved;
if ( prev + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page - 1).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page - 1).linesReserved += paragraph.figure.size;
break; // next paragraph
}
}
if (cur + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page).linesReserved += paragraph.figure.size;
break; // next paragraph
}
if (next + paragraph.figure.size < L) {
pages.get(page).texts.add(paragraph);
pages.get(page + 1).texts.add(paragraph.figure);
pages.get(page).linesReserved += paragraph.size;
pages.get(page + 1).linesReserved += paragraph.figure.size;
break; // next paragraph
}
page++;
}
}
page++;
} while (true);
}
return pages;
}
}
And tests:
public class ParagraphsAndFiguresTest {
@Test
public void pageGeneration1() throws Exception {
// given
ArrayList paragraphs = new ArrayList();
paragraphs.add(new Paragraph(20,21));
paragraphs.add(new Paragraph(22,23));
paragraphs.add(new Paragraph(24,25));
// when
ArrayList<PageContent> pageContents = ParagraphsAndFigures.generatePages(paragraphs, 50);
// then
assertThat(transformToList(pageContents), is(asList("20", "21", "p0" ,"22" ,"23", "p1" ,"24" ,"25", "p2")));
}
@Test
public void pageGeneration2() throws Exception {
// given
ArrayList<Paragraph> paragraphs = new ArrayList<Paragraph>();
paragraphs.add(new Paragraph(10,11));
paragraphs.add(new Paragraph(28,21));
paragraphs.add(new Paragraph(22,23));
// when
ArrayList<PageContent> pageContents = ParagraphsAndFigures.generatePages(paragraphs, 50);
// then
assertThat(transformToList(pageContents), is(asList("10", "11" ,"28", "p0" ,"21", "22" , "p1" ,"23", "p2")));
}
@Test
public void pageGeneration3() throws Exception {
// given
ArrayList<Paragraph> paragraphs = new ArrayList<Paragraph>();
paragraphs.add(new Paragraph(10,11));
paragraphs.add(new Paragraph(12,30));
paragraphs.add(new Paragraph(13,19));
// when
ArrayList<PageContent> pageContents = ParagraphsAndFigures.generatePages(paragraphs, 50);
// then
assertThat(transformToList(pageContents), is(asList("10", "11" ,"12", "13", "p0" ,"30", "19" , "p1" )));
}
@Test
public void pageGeneration4() throws Exception {
// given
ArrayList<Paragraph> paragraphs = new ArrayList<Paragraph>();
paragraphs.add(new Paragraph(10,11));
paragraphs.add(new Paragraph(30,12));
paragraphs.add(new Paragraph(13,16));
// when
ArrayList<PageContent> pageContents = ParagraphsAndFigures.generatePages(paragraphs, 50);
// then
assertThat(transformToList(pageContents), is(asList("10", "11" ,"12", "16", "p0" ,"30", "13" ,"p1" )));
}
@Test
public void pageGeneration5() throws Exception {
// given
ArrayList<Paragraph> paragraphs = new ArrayList<Paragraph>();
paragraphs.add(new Paragraph(31,32));
paragraphs.add(new Paragraph(17,21));
paragraphs.add(new Paragraph(30,35));
// when
ArrayList<PageContent> pageContents = ParagraphsAndFigures.generatePages(paragraphs, 50);
// then
assertThat(transformToList(pageContents), is(asList("31", "p0", "32", "17", "p1", "21", "p2", "30", "p3", "35", "p4")));
}
private List<String> transformToList(ArrayList<PageContent> pageContents) {
List<String> result = new ArrayList<String>();
for (int i = 0; i < pageContents.size(); i++) {
PageContent pageContent = pageContents.get(i);
if (!pageContent.texts.isEmpty()) {
for (Text text : pageContent.texts) {
result.add(String.valueOf(text.size));
}
result.add("p"+i);
}
}
return result;
}
}
And structures:
public class PageContent {
int linesReserved;
Collection texts = new ArrayList();
public boolean hasPicture() {
for (Text text : texts) {
if (text instanceof Figure) {
return true;
}
}
return false;
}
}
public class Text {
protected int size;
}
public class Figure extends Text{
}
public class Paragraph extends Text {
public Paragraph(int size, int fSIze) {
this.size = size;
this.figure = new Figure();
this.figure.size = fSIze;
}
Figure figure;
}