[Mulgara-svn] r1790 - trunk/src/jar/util/java/org/mulgara/util/functional
pag at mulgara.org
pag at mulgara.org
Thu Sep 24 23:10:48 UTC 2009
Author: pag
Date: 2009-09-24 16:10:48 -0700 (Thu, 24 Sep 2009)
New Revision: 1790
Modified:
trunk/src/jar/util/java/org/mulgara/util/functional/C.java
Log:
Added string join() methods for lists
Modified: trunk/src/jar/util/java/org/mulgara/util/functional/C.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/functional/C.java 2009-09-24 21:33:53 UTC (rev 1789)
+++ trunk/src/jar/util/java/org/mulgara/util/functional/C.java 2009-09-24 23:10:48 UTC (rev 1790)
@@ -112,6 +112,7 @@
* @return The first element in the list.
* @throws NoSuchElementException If the list is empty.
*/
+ @SuppressWarnings("unchecked")
public static final <T1> T1 head(List<T1> arg) throws NoSuchElementException {
if (arg instanceof LinkedList) return ((LinkedList<T1>)arg).getFirst();
if (arg.size() == 0) throw new NoSuchElementException("Empty list");
@@ -134,6 +135,7 @@
* @param arg The list.
* @return The first element in the list, or <code>null</code> if the list is empty.
*/
+ @SuppressWarnings("unchecked")
public static final <T1> T1 headN(List<T1> arg) {
return arg.isEmpty() ? null : (arg instanceof LinkedList) ? ((LinkedList<T1>)arg).getFirst() : arg.get(0);
}
@@ -156,6 +158,7 @@
* @return The last element in the list.
* @throws IndexOutOfBoundsException If the list is empty.
*/
+ @SuppressWarnings("unchecked")
public static final <T1> T1 tail(List<T1> arg) throws NoSuchElementException {
if (arg instanceof LinkedList) return ((LinkedList<T1>)arg).getLast();
if (arg.size() == 0) throw new NoSuchElementException("Empty list");
@@ -200,6 +203,7 @@
* @return The first element in the collection.
* @throws NoSuchElementException If the collection is empty.
*/
+ @SuppressWarnings("unchecked")
public static final <T1> T1 first(Collection<T1> arg) throws NoSuchElementException {
if (arg instanceof LinkedList) return ((LinkedList<T1>)arg).getFirst();
if (arg.isEmpty()) throw new NoSuchElementException("Empty Collection");
@@ -229,6 +233,38 @@
}
/**
+ * Method to join the elements of a list into a string.
+ * @param <T> The type of element in the list.
+ * @param list The list to be converted to a string.
+ * @param separator The separator to use between elements of the list. May be <code>null</code>.
+ * @return The final string.
+ */
+ public static final <T> String join(List<T> list, String separator) {
+ return join(list, null, separator, null);
+ }
+
+ /**
+ * General method to join the elements of a list into a string.
+ * @param <T> The type of element in the list.
+ * @param list The list to be converted to a string.
+ * @param start The start of the string. May be <code>null</code>.
+ * @param separator The separator to use between elements of the list. May be <code>null</code>.
+ * @param end The end of the string. May be <code>null</code>.
+ * @return The final string.
+ */
+ public static final <T> String join(List<T> list, String start, String separator, String end) {
+ StringBuilder s = start == null ? new StringBuilder() : new StringBuilder(start);
+ boolean first = true;
+ for (T elt: list) {
+ if (!first && separator != null) s.append(separator);
+ else first = false;
+ s.append(elt);
+ }
+ if (end != null) s.append(end);
+ return s.toString();
+ }
+
+ /**
* Inserts an element into an ordered list in a given order.
* @param <T> The type of the element to be inserted. Must be comparable on itself.
* @param list The list to insert into. This must already be ordered in the same order as this insert.
More information about the Mulgara-svn
mailing list