Skip to content

Commit 30e11bb

Browse files
convert PrefixTools to Java
1 parent 31da3f7 commit 30e11bb

1 file changed

Lines changed: 67 additions & 69 deletions

File tree

sqldev/src/main/java/org/utplsql/sqldev/model/PrefixTools.java

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,79 +13,77 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.utplsql.sqldev.model
16+
package org.utplsql.sqldev.model;
1717

18-
import java.util.List
18+
import java.util.List;
1919

20-
// converted to Xtend based on Java code on https://www.geeksforgeeks.org/longest-common-prefix-using-binary-search/
21-
class PrefixTools {
22-
def static int findMinLength(String[] arr, int n) {
23-
var int min = Integer.MAX_VALUE
24-
for (var int i = 0; i < n; i++) {
25-
if ({
26-
val _rdIndx_arr = i
27-
arr.get(_rdIndx_arr)
28-
}.length() < min) {
29-
min = {
30-
val _rdIndx_arr = i
31-
arr.get(_rdIndx_arr)
32-
}.length()
33-
}
34-
}
35-
return min
36-
}
20+
//converted to Xtend based on Java code on https://www.geeksforgeeks.org/longest-common-prefix-using-binary-search/
21+
//converted back to Java with some amendments
22+
public class PrefixTools {
23+
24+
// do not instantiate this class
25+
private PrefixTools() {
26+
super();
27+
}
28+
29+
public static int findMinLength(final String[] arr, final int n) {
30+
int min = Integer.MAX_VALUE;
31+
for (int i=0; i < n; i++) {
32+
if (arr[i].length() < min) {
33+
min = arr[i].length();
34+
}
35+
}
36+
return min;
37+
}
3738

38-
def static boolean allContainsPrefix(String[] arr, int n, String str, int start, int end) {
39-
for (var int i = 0; i < n; i++) {
40-
var String arr_i = {
41-
val _rdIndx_arr = i
42-
arr.get(_rdIndx_arr)
43-
}
44-
for (var int j = start; j <= end; j++) {
45-
if (arr_i.charAt(j) !== str.charAt(j)) {
46-
return false
47-
}
48-
}
49-
}
50-
return true
51-
}
39+
public static boolean allContainsPrefix(final String[] arr, final int n, final String str, final int start, final int end) {
40+
for (int i=0; i < n; i++) {
41+
String item = arr[i];
42+
for (int j = start; j <= end; j++) {
43+
if (item.charAt(j) != str.charAt(j)) {
44+
return false;
45+
}
46+
}
47+
}
48+
return true;
49+
}
5250

53-
def static String commonPrefix(String[] arr, int n) {
54-
var int index = findMinLength(arr, n)
55-
var String prefix = ""
56-
var int low = 0
57-
var int high = index
58-
while (low <= high) {
59-
var int mid = low + (high - low) / 2
60-
if (allContainsPrefix(arr, n, arr.get(0), low, mid)) {
61-
prefix = prefix + arr.get(0).substring(low, mid + 1)
62-
low = mid + 1
63-
} else {
64-
high = mid - 1
65-
}
66-
}
67-
return prefix
68-
}
51+
public static String commonPrefix(final String[] arr, final int n) {
52+
int index = findMinLength(arr, n);
53+
StringBuilder prefix = new StringBuilder();
54+
int low = 0;
55+
int high = index; // index-1 is wrong
56+
while (low <= high) {
57+
int mid = low + (high - low) / 2;
58+
if (allContainsPrefix(arr, n, arr[0], low, mid)) {
59+
prefix.append(arr[0].substring(low, mid + 1));
60+
low = mid + 1;
61+
} else {
62+
high = mid - 1;
63+
}
64+
}
65+
return prefix.toString();
66+
}
6967

70-
def static String commonPrefix(List<String> list) {
71-
try {
72-
if (list.size === 0) {
73-
return ""
74-
} else if (list.size === 1) {
75-
val pos = list.get(0).lastIndexOf(".");
76-
if (pos > 0) {
77-
return list.get(0).substring(0, pos + 1)
78-
} else {
79-
return ""
80-
}
81-
} else {
82-
var String[] testArray = newArrayOfSize(list.size)
83-
var prefix = commonPrefix(list.toArray(testArray), list.size)
84-
return prefix
85-
}
86-
} catch (Exception e) {
87-
return ""
88-
}
89-
}
68+
public static String commonPrefix(final List<String> list) {
69+
try {
70+
if (list.size() == 0) {
71+
return "";
72+
} else if (list.size() == 1) {
73+
final int pos = list.get(0).lastIndexOf(".");
74+
if (pos > 0) {
75+
return list.get(0).substring(0, pos + 1);
76+
} else {
77+
return "";
78+
}
79+
} else {
80+
final String[] testArray = new String[list.size()];
81+
final String prefix = commonPrefix(list.toArray(testArray), list.size());
82+
return prefix;
83+
}
9084

85+
} catch (final Exception e) {
86+
return "";
87+
}
88+
}
9189
}

0 commit comments

Comments
 (0)