1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.util;
14
15 import java.awt.Component;
16 import java.awt.Font;
17 import java.awt.FontMetrics;
18 import java.io.File;
19
20
21
22
23
24
25 public class StringUtils {
26
27
28
29
30
31
32 public static String normaliseSize(long size) {
33 if (size < 1024) {
34 return Long.toString(size);
35 } else if (size < (1024 * 1024)) {
36 return Long.toString(size / 1024) + "kB";
37 } else if (size < (1024 * 1024 * 1024)) {
38 return Long.toString(size / (1024 * 1024)) + "MB";
39 } else if (size < (1024 * 1024 * 1024 * 1024)) {
40 return Long.toString(size / (1024 * 1024 * 1024)) + "GB";
41 } else {
42 return Long.toString(size / (1024 * 1024 * 1024 * 1024)) + "TB";
43 }
44 }
45
46
47
48
49
50
51
52
53
54
55 public static String calculateTitle(Component parent, int width, File leftFile, File rightFile, Font titleFont) {
56 String left = leftFile.getAbsolutePath();
57 String right = rightFile.getAbsolutePath();
58 return calculateTitle(parent, width, left, right, titleFont);
59 }
60
61
62
63
64
65
66
67
68
69
70 public static String calculateTitle(Component parent, int width, String left, String right, Font titleFont) {
71 FontMetrics fontMetrics = parent.getFontMetrics(titleFont);
72
73 String newTitle = left + " vs " + right;
74 if (width > fontMetrics.stringWidth(newTitle)) {
75 return newTitle;
76 } else if (left.equals(right)) {
77 return "Comparing with itself: " + left;
78 } else {
79 return calculateShortestTitle(left, right);
80 }
81 }
82
83
84
85
86
87
88
89 public static String calculateShortestTitle(String left, String right) {
90 int start = 0;
91 if (left.startsWith(right)) {
92 char s = File.separatorChar;
93 int i = right.lastIndexOf(s);
94 if (i >= 0) {
95 return "..." + left.substring(i) + " vs ..." + right.substring(i);
96 } else {
97 return left + " vs " + right;
98 }
99 }
100
101 if (right.startsWith(left)) {
102 char s = File.separatorChar;
103 int i = left.lastIndexOf(s);
104 if (i >= 0) {
105 return "..." + left.substring(i) + " vs ..." + right.substring(i);
106 } else {
107 return left + " vs " + right;
108 }
109 }
110
111 while ((start < left.length()) && (start < right.length()) && (left.charAt(start) == right.charAt(start))) {
112 start++;
113 }
114 int end = 0;
115 int leftEnd = left.length() - 1;
116 int rightEnd = right.length() - 1;
117
118 while ((leftEnd >= 0) && (rightEnd >= 0) && (left.charAt(leftEnd) == right.charAt(rightEnd))) {
119 end++;
120 leftEnd--;
121 rightEnd--;
122 }
123 StringBuffer nt = new StringBuffer();
124 if (start > 0) {
125 nt.append("...");
126 }
127 nt.append(extractPath(left, start, leftEnd));
128 if (leftEnd < left.length() - 1) {
129 nt.append("...");
130 }
131 nt.append(" vs ");
132 if (start > 0) {
133 nt.append("...");
134 }
135 nt.append(extractPath(right, start, rightEnd));
136 if (rightEnd < right.length() - 1) {
137 nt.append("...");
138 }
139 return nt.toString();
140 }
141
142
143
144
145
146
147
148
149 protected static String extractPath(String str, int start, int end) {
150 char s = File.separatorChar;
151 while ((start > 0) && (str.charAt(start - 1) != s)) {
152 start = start - 1;
153 }
154 while ((end < str.length()) && (str.charAt(end) != s)) {
155 end = end + 1;
156 }
157 if (start <= end) {
158 return str.substring(start, end);
159 } else {
160 return "";
161 }
162 }
163
164
165
166
167
168
169 public static String collapseWhitespace(String s) {
170 StringBuffer result = new StringBuffer(s.length());
171 int l = s.length();
172 boolean whitespace = false;
173 for (int i = 0; i < l; i++) {
174 char c = s.charAt(i);
175 if (Character.isWhitespace(c)) {
176 if (!whitespace) {
177 result.append(c);
178 whitespace = true;
179 }
180 } else {
181 result.append(c);
182 whitespace = false;
183 }
184 }
185 return result.toString();
186 }
187
188
189
190
191
192
193 public static String removeWhitespaceX(String s) {
194 StringBuffer result = new StringBuffer(s.length());
195 int l = s.length();
196 for (int i = 0; i < l; i++) {
197 char c = s.charAt(i);
198 if (!Character.isWhitespace(c)) {
199 result.append(c);
200 }
201 }
202 return result.toString();
203 }
204 }