View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * String utilities
22   * 
23   * @author Daniel Sendula
24   */
25  public class StringUtils {
26  
27      /**
28       * Returns file size of given long integer
29       * @param size file size
30       * @return string representation of file size.
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       * Calculates title when two files are compared
48       * @param parent parent component font metrics to be taken from
49       * @param width width of space for the title
50       * @param leftFile left file 
51       * @param rightFile right file
52       * @param titleFont font to be used
53       * @return string
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       * Calculates title when two files are compared
63       * @param parent parent component font metrics to be taken from
64       * @param width width of space for the title
65       * @param left left file name
66       * @param right right file name
67       * @param titleFont font to be used
68       * @return string
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       * Calculates shortest title when two files are compared
85       * @param left left file name
86       * @param right right file name
87       * @return string
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         // TODO
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      * Extracts the path from given string and between two indexes
144      * @param str string
145      * @param start start index
146      * @param end end index
147      * @return path
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      * Removes all whitespace from the string
166      * @param s string
167      * @return new string without the whitespace
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      * Removes all whitespace from the string
190      * @param s string
191      * @return new string without the whitespace
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 }