| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StringUtils |
|
| 3.3333333333333335;3.333 |
| 1 | /* | |
| 2 | * Copyright (c) 2000 | |
| 3 | * Jon Schewe. All rights reserved | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 24 | * SUCH DAMAGE. | |
| 25 | * | |
| 26 | * I'd appreciate comments/suggestions on the code jpschewe@mtu.net | |
| 27 | */ | |
| 28 | package net.mtu.eggplant.util; | |
| 29 | ||
| 30 | /** | |
| 31 | * Some handy utilities for working with strings. | |
| 32 | * | |
| 33 | * @version $Revision$ | |
| 34 | */ | |
| 35 | public final class StringUtils { | |
| 36 | ||
| 37 | 0 | private StringUtils() { |
| 38 | 0 | } |
| 39 | ||
| 40 | /** | |
| 41 | * Replace all instances of <tt>search</tt> in <tt>source</tt> with | |
| 42 | * <tt>replace</tt>. | |
| 43 | * | |
| 44 | * @return new String | |
| 45 | * @pre (source != null) | |
| 46 | * @pre (search != null) | |
| 47 | * @pre (replace != null) | |
| 48 | **/ | |
| 49 | public static String searchAndReplace(final String source, | |
| 50 | final String search, | |
| 51 | final String replace) { | |
| 52 | final String newString; | |
| 53 | 7 | final int index = source.indexOf(search); |
| 54 | 7 | if (index != -1) { |
| 55 | 4 | final String front = source.substring(0, index); |
| 56 | 4 | final String end = source.substring(index + search.length()); |
| 57 | 8 | newString = new StringBuffer().append(front).append(replace).append( |
| 58 | 8 | searchAndReplace(end, search, replace)).toString(); |
| 59 | 4 | } else { |
| 60 | 3 | newString = source; |
| 61 | } | |
| 62 | ||
| 63 | 7 | return newString; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * If the string is longer than len, truncate it and append "..." | |
| 68 | * | |
| 69 | * @param name the string | |
| 70 | * @param len the max length for the string | |
| 71 | * @return the string never longer than len characters | |
| 72 | */ | |
| 73 | public static String trimString(final String name, | |
| 74 | final int len) { | |
| 75 | 0 | if (len <= 3) { |
| 76 | 0 | throw new IllegalArgumentException("Length must be longer than 3 otherwise all strings are just '...'"); |
| 77 | } | |
| 78 | 0 | if (null == name) { |
| 79 | 0 | return null; |
| 80 | 0 | } else if (name.length() > len) { |
| 81 | 0 | return name.substring(0, len - 3) |
| 82 | + "..."; | |
| 83 | } else { | |
| 84 | 0 | return name; |
| 85 | } | |
| 86 | } | |
| 87 | ||
| 88 | } |