ingo@1115: /*
ingo@1115: * Copyright (c) 2010 by Intevation GmbH
ingo@1115: *
ingo@1115: * This program is free software under the LGPL (>=v2.1)
ingo@1115: * Read the file LGPL.txt coming with the software for details
ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist.
ingo@1115: */
ingo@1115:
sascha@439: package de.intevation.gnv.utils;
sascha@439:
sascha@439: /**
ingo@806: * Helper class which supports some methods for working with strings.
ingo@806: *
sascha@780: * @author Sascha L. Teichmann
sascha@439: */
sascha@439: public final class StringUtils
sascha@439: {
sascha@439: private StringUtils() {
sascha@439: }
sascha@439:
ingo@806: /**
ingo@806: * Append a string to a string array.
ingo@806: *
ingo@806: * @param haystack String array.
ingo@806: * @param straw String to append.
ingo@806: * @return the new string array.
ingo@806: */
sascha@439: public static final String [] append(String [] haystack, String straw) {
sascha@439: if (haystack == null) {
sascha@439: return new String [] { straw };
sascha@439: }
sascha@442: String [] nhaystack = new String[haystack.length + 1];
sascha@439: System.arraycopy(haystack, 0, nhaystack, 0, haystack.length);
sascha@439: nhaystack[haystack.length] = straw;
sascha@439: return nhaystack;
sascha@439: }
sascha@439:
ingo@806: /**
ingo@806: * Checks the existence of a string in a given string array.
ingo@806: *
ingo@806: * @param haystack String array.
ingo@806: * @param needle String for being checked.
ingo@806: * @return true, if the string is contained in haystack - else false.
ingo@806: */
sascha@439: public static final boolean contains(String [] haystack, String needle) {
sascha@439: if (haystack == null) {
sascha@439: return false;
sascha@439: }
sascha@439:
sascha@439: if (needle == null) {
sascha@439: for (int i = haystack.length - 1; i >= 0; --i) {
sascha@439: if (haystack[i] == null) {
sascha@439: return true;
sascha@439: }
sascha@439: }
sascha@439: }
sascha@439: else {
sascha@439: for (int i = haystack.length - 1; i >= 0; --i) {
sascha@439: String straw = haystack[i];
sascha@439: if (straw != null && straw.equals(needle)) {
sascha@439: return true;
sascha@439: }
sascha@439: }
sascha@439: }
sascha@439:
sascha@439: return false;
sascha@439: }
sascha@439: }
sascha@798: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :