I tried to go for a strange "Strange loop" (
http://en.wikipedia.org/wiki/Strange_loop)...but I ended up with a recursive
loop of doom. Please forgive the quick thrown-together implementation =)
(2 passes, please =)
Given a "normal" string, this app would loop through and resolve the acronym
with the full break out and put the acronym in parenthesis after it. Given
recursive acronyms, all hell breaks loose.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Lets make that String understandable to management....
*/
public class Main {
public static void main(String[] args) {
//replace var test with cli/arg input
String test = "In addition to java you can write plugins for the "
+ "GNU tool Nagios using PHP";
while (AcronymFinder.hasAcronym(test)) {
test = AcronymFinder.explodeAcronym(test);
System.out.println(test);
}
}
private static class AcronymFinder {
private static Pattern p = Pattern.compile("([a-zA-Z]*)[\\s|\\.]");
/**
* Takes in a String and checks it against the acronym dictionary,
* resolving any acronyms in the process
*
* @param raw
* @return
*/
public static String explodeAcronym(String raw) {
String out = null;
Matcher m = p.matcher(raw);
StringBuilder sb = new StringBuilder();
while (m.find()) {
try {
Acronyms a = Acronyms.valueOf(m.group(1));
sb.append(a.resolve());
sb.append(" (" + m.group(1) + ")"); //the parens
prevents the acronym from getting picked up twice, due to regex impl
} catch (IllegalArgumentException e) {
sb.append(m.group(1));
}
sb.append(" ");
}
if (sb.length() > 0) {
out = sb.toString();
} else {
out = raw;
}
return out;
}
public static boolean hasAcronym(String raw) {
Matcher m = p.matcher(raw);
while (m.find()) {
try {
Acronyms a = Acronyms.valueOf(m.group(1));
return true;
} catch (IllegalArgumentException e) {
}
}
return false;
}
}
private enum Acronyms {
GNU("GNU is Not Unix"),
Nagios("Nagios Ain't Gonna Insist On Sainthood"),
PHP("PHP Hypertext Preprocessor");
private String full;
private Acronyms(String f) {
full = f;
}
public String resolve() {
return full;
}
}
}
On Wed, Aug 4, 2010 at 5:01 AM, Kevin Wright <[email protected]>wrote:
> my thinking:
>
> `http:` is the label (followed by a comment that the pre-processor strips
> out)
> `do { ... } while (...)` is the labelled statement
> `continue <label>` attempts to transfer control back to the "continue
> target". In this case, the labelled do/while loop
>
> a `do {statement} while (expression)` loops will check its expression only
> once the statement has executed normally (which it hasn't here)
> So... the loop will begin again, and continue infinitely, or until an
> exception is thrown or the JVM is terminated.
>
> No stack involved, at the level of compiled machine code it'll be jump
> instructions all the way down...
>
>
> The language spec covering all this is here:
> http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
> ( or in pdf here:
> http://java.sun.com/docs/books/jls/download/langspec-3.0.pdf )
>
> For anyone still labouring under the delusion that Java is a simple
> language, it's a heavyweight document; running to 650 pages in almost 8MB
> but you only really need chapter 14 for this question :)
>
>
> On 4 August 2010 05:43, Kirk <[email protected]> wrote:
>
>> I'll be speaking there so I don't need a pass but thought it might be fun
>> to put in a puzzler.
>>
>> public class StrangeLoop {
>> public static void main(String[] args) {
>> http://www.thestrangeloop.com
>> do {
>> System.out.println("Strange Loop");
>> continue http;
>> } while (false);
>> }
>> }
>>
>> Puzzler, infinite loop or normal termination?
>>
>> Regards,
>> Kirk
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "The Java Posse" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<javaposse%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/javaposse?hl=en.
>>
>>
>
>
> --
> Kevin Wright
>
> mail/google talk: [email protected]
> wave: [email protected]
> skype: kev.lee.wright
> twitter: @thecoda
>
> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.