Last active
March 30, 2017 00:28
-
-
Save Arinerron/14687f2ad508b4e11212cb67eb9a62e3 to your computer and use it in GitHub Desktop.
Need a dynamic programmatic file header generator? Try this out!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedHashMap; | |
import java.util.Map; | |
public class HeaderGenerator { | |
private Map<String, String> map = new LinkedHashMap<String, String>(); | |
private String separator = ": "; | |
private int indentation_size = 4; | |
private char indentation_char = ' '; | |
private int padding_size = 1; | |
private char border_char = '/'; | |
private int border_size = 2; | |
public void clear() { | |
map.clear(); | |
} | |
public String generate() { | |
int size = 0; | |
for (final String key : map.keySet()) { | |
final String val = map.get(key); | |
final int size_temp = (key + this.getSeparator() + val).length(); | |
if(size_temp > size) | |
size = size_temp; | |
} | |
final String x_border = new String(new char[size + (this.getBorderSize() * 2) + (this.getPaddingSize() * 2)]).replace('\0', this.getBorderChar()); | |
final String indentation = new String(new char[this.getIndentationSize()]).replace('\0', this.getIndentationChar()); | |
final StringBuilder builder = new StringBuilder(); | |
builder.append(indentation).append(x_border).append("\n"); | |
//for(int i = 0; i < this.getBorderSize(); i++) // this code is for y-border size | |
//builder.append(indentation).append(x_border).append("\n"); | |
builder.append(indentation); | |
for(int i = 0; i < this.getBorderSize(); i++) | |
builder.append(this.getBorderChar()); | |
for(int i = 0; i < size + (this.getPaddingSize() * 2); i++) | |
builder.append(" "); // filler | |
for(int i = 0; i < this.getBorderSize(); i++) | |
builder.append(this.getBorderChar()); | |
builder.append("\n"); | |
for (final String key : map.keySet()) { | |
final String val_ = map.get(key); | |
final String[] split = val_.split("\n"); | |
for(final String val : split) { | |
final int size_temp = (key + this.getSeparator() + val).length(); | |
builder.append(indentation); | |
for(int i = 0; i < this.getBorderSize(); i++) | |
builder.append(this.getBorderChar()); | |
for(int i = 0; i < this.getPaddingSize(); i++) | |
builder.append(" "); // padding_char | |
builder.append(key + this.getSeparator() + val); | |
final int left_over = size - size_temp; | |
for(int i = 0; i < left_over; i++) | |
builder.append(" "); // filler | |
for(int i = 0; i < this.getPaddingSize(); i++) | |
builder.append(" "); // padding_char | |
for(int i = 0; i < this.getBorderSize(); i++) | |
builder.append(this.getBorderChar()); | |
builder.append("\n"); | |
} | |
} | |
builder.append(indentation); | |
for(int i = 0; i < this.getBorderSize(); i++) | |
builder.append(this.getBorderChar()); | |
for(int i = 0; i < size + (this.getPaddingSize() * 2); i++) | |
builder.append(" "); // filler | |
for(int i = 0; i < this.getBorderSize(); i++) | |
builder.append(this.getBorderChar()); | |
builder.append("\n"); | |
builder.append(indentation).append(x_border); | |
return builder.toString(); | |
} | |
public boolean set(final String key, final String val) { | |
final boolean exists = map.containsKey(key); | |
map.put(key, val); | |
return exists; | |
} | |
public String get(final String key) { | |
return map.getOrDefault(key, null); | |
} | |
public Map<String, String> getMap() { | |
return map; | |
} | |
public String getSeparator() { | |
return this.separator; | |
} | |
public void setSeparator(final String separator) { | |
this.separator = separator; | |
} | |
public int getIndentationSize() { | |
return indentation_size; | |
} | |
public void setIndentationSize(final int indentation_size) { | |
this.indentation_size = indentation_size; | |
} | |
public char getIndentationChar() { | |
return indentation_char; | |
} | |
public void setIndentationChar(final char indentation_char) { | |
this.indentation_char = indentation_char; | |
} | |
public int getPaddingSize() { | |
return padding_size; | |
} | |
public void setPaddingSize(final int padding_size) { | |
this.padding_size = padding_size; | |
} | |
public char getBorderChar() { | |
return border_char; | |
} | |
public void setBorderChar(final char border_char) { | |
this.border_char = border_char; | |
} | |
public int getBorderSize() { | |
return this.border_size; | |
} | |
public void setBorderSize(final int border_size) { | |
this.border_size = border_size; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.arinerron.utils.appheadergenerator.HeaderGenerator; // The main package | |
import java.util.Date; // just for this example | |
public class Main { | |
public static void main(String[] args) { | |
HeaderGenerator gen = new HeaderGenerator(); // Create a new header generator instance | |
gen.clear(); // Remove all entries from the header | |
gen.set("Author", "Arinerron"); // Add a value and key | |
gen.set("Message", "Try this program out!"); // example | |
gen.set("Date", new Date().toString()); // yet another example | |
gen.setBorderChar('|'); // The character that the border is made up of | |
gen.setBorderSize(2); // The number of border characters on each side | |
gen.setPaddingSize(6); // The number of padding characters to the left of each line | |
gen.setPaddingChar(' '); // The character of the padding that is printed | |
String header = gen.generate(); // Create the header | |
System.out.println(header); // Check it out! :) | |
// I didn't document the `get` functions, but just assume that you can replace all of the `set` functions above with `get`. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment