GT-3396 - File Chooser - review fixes

This commit is contained in:
dragonmacher 2019-12-16 14:46:41 -05:00
parent 171914f49e
commit 6918ef7b45
13 changed files with 797 additions and 466 deletions

View file

@ -15,10 +15,10 @@
*/
package ghidra.util;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@ -555,9 +555,9 @@ public class StringUtilities {
length *= -1;
}
int numFillers = length - source.length();
StringBuffer buffer = new StringBuffer();
for (int f = 0; f < numFillers; f++) {
int n = length - source.length();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < n; i++) {
buffer.append(filler);
}
@ -756,23 +756,6 @@ public class StringUtilities {
return new String(bytes);
}
/**
* Turn the given data into an attractive string, with the separator of your choosing
*
* @param collection the data from which a string will be generated
* @param separator the string used to separate elements
* @return a string representation of the given list
*/
public static String toString(Collection<?> collection, String separator) {
if (collection == null) {
return null;
}
String asString =
collection.stream().map(o -> o.toString()).collect(Collectors.joining(separator));
return "[ " + asString + " ]";
}
public static String toStringWithIndent(Object o) {
if (o == null) {
return "null";
@ -783,19 +766,6 @@ public class StringUtilities {
return indented;
}
/**
* Reverse the characters in the given string
*
* @param s the string to reverse
* @return the reversed string
*/
public static String reverse(String s) {
if (s == null) {
return null;
}
return new StringBuilder(s).reverse().toString();
}
/**
* Merge two strings into one.
* If one string contains the other, then the largest is returned.

View file

@ -16,8 +16,11 @@
package util;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.apache.commons.lang3.StringUtils;
import ghidra.util.SystemUtilities;
import ghidra.util.datastruct.FixedSizeStack;
@ -47,7 +50,7 @@ import ghidra.util.datastruct.FixedSizeStack;
public class HistoryList<T> {
private final FixedSizeStack<T> historyStack;
private final Consumer<T> itemSelectedCallback;
private final BiConsumer<T, T> itemSelectedCallback;
private int historyIndex;
private boolean isBroadcasting;
@ -64,6 +67,20 @@ public class HistoryList<T> {
* going back or forward
*/
public HistoryList(int size, Consumer<T> itemSelectedCallback) {
this(size, asBiConsumer(itemSelectedCallback));
}
/**
* The sized passed here limits the size of the list, with the oldest items being dropped
* as the list grows. The given callback will be called when {@link #goBack()} or
* {@link #goForward()} are called.
*
* @param size the max number of items to keep in the list
* @param itemSelectedCallback the function to call when the client selects an item by
* going back or forward. This callback will be passed the newly selected item as
* the first argument and the previously selected item as the second argument.
*/
public HistoryList(int size, BiConsumer<T, T> itemSelectedCallback) {
Objects.requireNonNull(itemSelectedCallback, "Item selected callback cannot be null");
if (size < 1) {
@ -74,6 +91,10 @@ public class HistoryList<T> {
this.historyStack = new FixedSizeStack<>(size);
}
private static <T> BiConsumer<T, T> asBiConsumer(Consumer<T> consumer) {
return (t, ignored) -> consumer.accept(t);
}
//==================================================================================================
// Interface Methods
//==================================================================================================
@ -170,9 +191,10 @@ public class HistoryList<T> {
return;
}
T leaving = getCurrentHistoryItem();
T t = historyStack.get(--historyIndex);
dropNull();
broadcast(t);
broadcast(t, leaving);
}
/**
@ -198,8 +220,9 @@ public class HistoryList<T> {
return;
}
T leaving = getCurrentHistoryItem();
T t = historyStack.get(++historyIndex);
broadcast(t);
broadcast(t, leaving);
}
/**
@ -342,10 +365,10 @@ public class HistoryList<T> {
historyStack.remove(itemIndex);
}
private void broadcast(T t) {
private void broadcast(T t, T leaving) {
try {
isBroadcasting = true;
itemSelectedCallback.accept(t);
itemSelectedCallback.accept(t, leaving);
}
finally {
isBroadcasting = false;
@ -362,6 +385,9 @@ public class HistoryList<T> {
@Override
public String toString() {
String key = " items: ";
String newlinePad = StringUtils.repeat(' ', key.length());
StringBuilder buffy = new StringBuilder();
for (int i = 0; i < historyStack.size(); i++) {
T t = historyStack.get(i);
@ -377,13 +403,13 @@ public class HistoryList<T> {
}
if (i != historyStack.size() - 1) {
buffy.append(',').append(' ');
buffy.append(',').append('\n').append(newlinePad);
}
}
//@formatter:off
return "{\n" +
"\titems: " + buffy.toString() + "\n" +
key + buffy.toString() + "\n" +
"}";
//@formatter:on
}

View file

@ -248,18 +248,6 @@ public class StringUtilitiesTest {
StringUtilities.trimMiddle(overString, max);
}
@Test
public void testReverse() {
String hello = "hello";
String reversed = StringUtilities.reverse(hello);
assertEquals("olleh", reversed);
}
@Test
public void testReverseNull() {
assertNull(StringUtilities.reverse(null));
}
@Test
public void testGetLastWord() {
assertEquals("word", StringUtilities.getLastWord("/This/is/my/last/word", "/"));