my version
[IRC.git] / Robust / Transactions / jcarderbenchmarks / mysrc / com / enea / jcarder / util / OptionFormatter.java
1 /*
2  * JCarder -- cards Java programs to keep threads disentangled
3  *
4  * Copyright (C) 2006-2007 Enea AB
5  * Copyright (C) 2007 Ulrik Svensson
6  * Copyright (C) 2007 Joel Rosdahl
7  *
8  * This program is made available under the GNU GPL version 2, with a special
9  * exception for linking with JUnit. See the accompanying file LICENSE.txt for
10  * details.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  */
16
17 package com.enea.jcarder.util;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /**
23  * This is a helper class for an OptionParser.
24  */
25 class OptionFormatter {
26     private final int mOptionIndent;
27     private final int mDescrIndent;
28     private final int mMaxWidth;
29
30     public OptionFormatter(int optionIndent, int descrIndent, int maxWidth) {
31         mOptionIndent = optionIndent;
32         mDescrIndent = descrIndent;
33         mMaxWidth = maxWidth;
34     }
35
36     public void format(StringBuilder sb, String option, String descr) {
37         addSpace(sb, mOptionIndent);
38         sb.append(option);
39         final int firstIndent;
40         if (mOptionIndent + option.length() >= mDescrIndent) {
41             sb.append("\n");
42             firstIndent = mDescrIndent;
43         } else {
44             firstIndent = mDescrIndent - (mOptionIndent + option.length());
45         }
46         if (descr == null) {
47             sb.append('\n');
48         } else {
49             List<String> descrLines = wrapText(descr, mMaxWidth - mDescrIndent);
50             indentText(sb, descrLines, mDescrIndent, firstIndent);
51         }
52     }
53
54     static void addSpace(StringBuilder sb, int n) {
55         for (int i = 0; i < n; ++i) {
56             sb.append(' ');
57         }
58     }
59
60     static List<String> wrapText(String text, int maxWidth) {
61         ArrayList<String> result = new ArrayList<String>();
62         int pos = 0;
63         StringBuilder sb = new StringBuilder();
64
65         for (String word : text.split("\\s+")) {
66             if (sb.length() > 0 && word.length() + 1 > maxWidth - sb.length()) {
67                 result.add(sb.toString());
68                 sb.setLength(0);
69                 sb.append(word);
70                 pos = word.length();
71             } else {
72                 if (sb.length() > 0) {
73                     sb.append(' ');
74                 }
75                 sb.append(word);
76                 pos += word.length();
77             }
78         }
79         if (sb.length() > 0) {
80             result.add(sb.toString());
81         }
82
83         return result;
84     }
85
86     static void indentText(StringBuilder sb,
87                            List<String> lines,
88                            int indent,
89                            int firstIndent) {
90         boolean first = true;
91         addSpace(sb, firstIndent);
92         for (String line : lines) {
93             if (first) {
94                 first = false;
95             } else {
96                 addSpace(sb, indent);
97             }
98             sb.append(line);
99             sb.append('\n');
100         }
101     }
102 }