Coverage Report - org.seasar.cubby.internal.util.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
92%
24/26
85%
24/28
4
 
 1  
 /*
 2  
  * Copyright 2004-2009 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.internal.util;
 17  
 
 18  
 /**
 19  
  * 文字列操作を行うためのユーティリティクラスです。
 20  
  * 
 21  
  * @author baba
 22  
  */
 23  0
 public class StringUtils {
 24  
 
 25  
         /**
 26  
          * 空かどうかを返します。
 27  
          * 
 28  
          * @param text
 29  
          *            文字列
 30  
          * @return 空かどうか
 31  
          */
 32  
         public static final boolean isEmpty(final String text) {
 33  606
                 return text == null || text.length() == 0;
 34  
         }
 35  
 
 36  
         /**
 37  
          * ケースインセンシティブで文字列同士が等しいかどうか返します。どちらもnullの場合は、<code>true</code>を返します。
 38  
          * 
 39  
          * @param target1
 40  
          *            文字列1
 41  
          * @param target2
 42  
          *            文字列2
 43  
          * @return ケースインセンシティブで文字列同士が等しいか
 44  
          */
 45  
         public static boolean equalsIgnoreCase(final String target1,
 46  
                         final String target2) {
 47  18
                 return (target1 == null) ? (target2 == null) : target1
 48  
                                 .equalsIgnoreCase(target2);
 49  
         }
 50  
 
 51  
         /**
 52  
          * ブランクかどうか返します。
 53  
          * 
 54  
          * @param str
 55  
          *            文字列
 56  
          * @return ブランクかどうか
 57  
          */
 58  
         public static boolean isBlank(final String str) {
 59  8
                 if (str == null || str.length() == 0) {
 60  2
                         return true;
 61  
                 }
 62  10
                 for (int i = 0; i < str.length(); i++) {
 63  8
                         if (!Character.isWhitespace(str.charAt(i))) {
 64  4
                                 return false;
 65  
                         }
 66  
                 }
 67  2
                 return true;
 68  
         }
 69  
 
 70  
         /**
 71  
          * ブランクではないかどうか返します。
 72  
          * 
 73  
          * @param str
 74  
          *            文字列
 75  
          * @return ブランクではないかどうか
 76  
          * @see #isBlank(String)
 77  
          */
 78  
         public static boolean isNotBlank(final String str) {
 79  4
                 return !isBlank(str);
 80  
         }
 81  
 
 82  
         /**
 83  
          * 文字列を置き換えます。
 84  
          * 
 85  
          * @param text
 86  
          *            テキスト
 87  
          * @param fromText
 88  
          *            置き換え対象のテキスト
 89  
          * @param toText
 90  
          *            置き換えるテキスト
 91  
          * @return 結果
 92  
          */
 93  
         public static final String replace(final String text,
 94  
                         final String fromText, final String toText) {
 95  
 
 96  576
                 if (text == null || fromText == null || toText == null) {
 97  0
                         return null;
 98  
                 }
 99  576
                 final StringBuilder builder = new StringBuilder(100);
 100  576
                 int pos = 0;
 101  576
                 int pos2 = 0;
 102  
                 while (true) {
 103  591
                         pos = text.indexOf(fromText, pos2);
 104  591
                         if (pos == 0) {
 105  3
                                 builder.append(toText);
 106  3
                                 pos2 = fromText.length();
 107  588
                         } else if (pos > 0) {
 108  12
                                 builder.append(text.substring(pos2, pos));
 109  12
                                 builder.append(toText);
 110  12
                                 pos2 = pos + fromText.length();
 111  
                         } else {
 112  576
                                 builder.append(text.substring(pos2));
 113  576
                                 break;
 114  
                         }
 115  
                 }
 116  576
                 return builder.toString();
 117  
         }
 118  
 
 119  
 }