X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FStringUtils.java;h=6044655b80c327eb3a237d82d72a6e46156284da;hb=5585b4b0ed1f5dd85887ed6b76352947ce1be133;hp=3e004a6f74257f066c883023a115a12e49ab39d4;hpb=4d7f98a6dd54d9863ba449ec79dcd95d25ed863d;p=jalview.git diff --git a/src/jalview/util/StringUtils.java b/src/jalview/util/StringUtils.java index 3e004a6..6044655 100644 --- a/src/jalview/util/StringUtils.java +++ b/src/jalview/util/StringUtils.java @@ -1,3 +1,23 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.util; import java.util.ArrayList; @@ -228,4 +248,55 @@ public class StringUtils } return "" + separator; } + + /** + * Converts a list to a string with a delimiter before each term except the + * first. Returns an empty string given a null or zero-length argument. This + * can be replaced with StringJoiner in Java 8. + * + * @param terms + * @param delim + * @return + */ + public static String listToDelimitedString(List terms, + String delim) + { + StringBuilder sb = new StringBuilder(32); + if (terms != null && !terms.isEmpty()) + { + boolean appended = false; + for (String term : terms) + { + if (appended) + { + sb.append(delim); + } + appended = true; + sb.append(term); + } + } + return sb.toString(); + } + + /** + * Convenience method to parse a string to an integer, returning 0 if the + * input is null or not a valid integer + * + * @param s + * @return + */ + public static int parseInt(String s) + { + int result = 0; + if (s != null && s.length() > 0) + { + try + { + result = Integer.parseInt(s); + } catch (NumberFormatException ex) + { + } + } + return result; + } }