Skip to content

Commit

Permalink
Make exec return Iterable instead of List
Browse files Browse the repository at this point in the history
  • Loading branch information
pron committed Aug 30, 2015
1 parent 5e0477c commit 609b428
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions capsule/src/main/java/Capsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import static java.util.Arrays.asList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.RandomAccess;
import java.util.concurrent.atomic.AtomicReference;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
Expand Down Expand Up @@ -4462,16 +4463,23 @@ private static <K, V> Map<K, List<V>> multiputAll(Map<K, List<V>> map, Map<K, Li
return map;
}

private static <T> T first(List<T> c) {
if (c == null || c.isEmpty())
private static boolean isEmpty(Iterable<?> c) {
if (c instanceof Collection)
return ((Collection<?>) c).isEmpty();
final Iterator<?> it = c.iterator();
return !it.hasNext();
}

private static <T> T first(Iterable<T> c) {
if (c == null || isEmpty(c))
throw new IllegalArgumentException("Not found");
return c.get(0);
return (c instanceof List && c instanceof RandomAccess) ? ((List<T>)c).get(0) : c.iterator().next();
}

private static <T> T firstOrNull(List<T> c) {
if (c == null || c.isEmpty())
private static <T> T firstOrNull(Iterable<T> c) {
if (c == null || isEmpty(c))
return null;
return c.get(0);
return (c instanceof List && c instanceof RandomAccess) ? ((List<T>)c).get(0) : c.iterator().next();
}

private static <T> T only(List<T> c) {
Expand Down Expand Up @@ -4718,7 +4726,7 @@ private static void close(Object c) {
* @param cmd the command
* @return the lines output by the command
*/
protected static List<String> exec(String... cmd) throws IOException {
protected static Iterable<String> exec(String... cmd) throws IOException {
return exec(-1, cmd);
}

Expand All @@ -4731,7 +4739,7 @@ protected static List<String> exec(String... cmd) throws IOException {
* @param cmd the command
* @return the lines output by the command
*/
protected static List<String> exec(int numLines, String... cmd) throws IOException {
protected static Iterable<String> exec(int numLines, String... cmd) throws IOException {
return exec(numLines, new ProcessBuilder(asList(cmd)));
}

Expand All @@ -4743,7 +4751,7 @@ protected static List<String> exec(int numLines, String... cmd) throws IOExcepti
* @param pb the {@link ProcessBuilder} that will be used to launch the command
* @return the lines output by the command
*/
protected static List<String> exec(ProcessBuilder pb) throws IOException {
protected static Iterable<String> exec(ProcessBuilder pb) throws IOException {
return exec(-1, pb);
}

Expand All @@ -4756,7 +4764,7 @@ protected static List<String> exec(ProcessBuilder pb) throws IOException {
* @param pb the {@link ProcessBuilder} that will be used to launch the command
* @return the lines output by the command
*/
protected static List<String> exec(int numLines, ProcessBuilder pb) throws IOException {
protected static Iterable<String> exec(int numLines, ProcessBuilder pb) throws IOException {
final List<String> lines = new ArrayList<>();
final Process p = pb.start();

Expand Down

0 comments on commit 609b428

Please sign in to comment.