1c: Implemented FrameDecorator
This commit is contained in:
parent
ebab952bd9
commit
70c90c1a21
43
ueb05_news_example/src/decorator/FrameDecorator.java
Normal file
43
ueb05_news_example/src/decorator/FrameDecorator.java
Normal file
@ -0,0 +1,43 @@
|
||||
package decorator;
|
||||
|
||||
import model.Item;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class FrameDecorator extends StringDecorator {
|
||||
public FrameDecorator(Item item) {
|
||||
super(item);
|
||||
}
|
||||
|
||||
public String getOverview() {
|
||||
return wrapWithFrame(wrappee.getOverview());
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return wrapWithFrame(wrappee.getDetails());
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return wrapWithFrame(wrappee.getTitle());
|
||||
}
|
||||
|
||||
private String wrapWithFrame(String content) {
|
||||
String[] lines = content.split("\n");
|
||||
int maxLength = Arrays.stream(lines).map(String::length).reduce(Integer::max).orElse(0);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("-".repeat(maxLength + 2)).append("\n");
|
||||
for (String line : lines) {
|
||||
int spaces = maxLength - line.length();
|
||||
int leadingSpaces = spaces / 2;
|
||||
int trailingSpaces = spaces - leadingSpaces;
|
||||
|
||||
builder.append("|")
|
||||
.append(" ".repeat(leadingSpaces))
|
||||
.append(line)
|
||||
.append(" ".repeat(trailingSpaces))
|
||||
.append("|\n");
|
||||
}
|
||||
builder.append("-".repeat(maxLength + 2)).append("\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user