1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 | package Torello.Java;
import Torello.Java.StringParse;
// NOTE NOTE NOTE:
//
//
// ************************************************************************************************
// ************************************************************************************************
// This method's output looks like this comment with 4 rows of stars (the one you are looking at)
// ************************************************************************************************
// ************************************************************************************************
class FourRowsStars
{
static String get(
// This is just the text that is inserted between the two rows of stars
final String title,
// Note that these are type "byte". This is the only way to ensure that the user
// reasonable values to this method. These are "line lengths" for a text file
// (actually a '.java' file). Anything larger for 127 for either of these is
// totally unreasonable.
final byte indentation,
final byte maxLineLength
)
{
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// These exception checks might seem exessive, but there is no need to worry...
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
//
// I have a very large Code Generator which outputs over 1,000 classes (albeit the number
// of Java Files which are actually produced is under 100). The entire thing runs in
// under 1 second, and worry about "super tight" and "super efficient" loops really should
// be the absolute furthest from your mind when writing such things!
//
// Make sure that you didn't "screw up" is 2, 3 or even 4 orders of magnitude more
// important than worrying if you can save a few milliseconds here and there! Code
// Generators (as far as I know), are not stored on servers that need to be efficient, they
// are only used in development cycles where efficiency is quite a bit less important.
if (title == null) throw new NullPointerException
("'title' cannot be null");
if (title.indexOf('\n') >= 0) throw new StringFormatException
("'title' contains a newline character");
if (indentation < 0) throw new IllegalArgumentException
("'indentation' cannot be negative");
if (maxLineLength < 0) throw new IllegalArgumentException
("'maxLineLength' cannot be negative");
if (indentation >= maxLineLength) throw new IllegalArgumentException
("'indentation' cannot be greater than or equal to 'maxLineLength'");
if ((title.length() + indentation + "// ".length()) > maxLineLength)
throw new IllegalArgumentException(
"'title' must fit within 'maxLineLength', including indentation and the two " +
"slash characters"
);
final String INDENT = StringParse.nChars(' ', indentation);
// note that the '-3' is for the "// " and the extra '-1' is for the '\n' character
final String STARS =
INDENT + "// " + StringParse.nChars('*', maxLineLength - indentation - 3 - 1) + '\n';
return
STARS + STARS +
INDENT + "// " + title + '\n' +
STARS + STARS;
}
}
|