Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8ff7e309736ae1655b97c13407df60fccab5fdd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

package org.eclipse.osee.framework.jdk.core.util;

import java.util.List;

/**
 * @author Jeff C. Phillips
 * @author Don Dunne
 * @author Karol M. Wilk
 */
public class Strings {
   private final static String EMPTY_STRING = "";

   private Strings() {
      // Utility class
   }

   /**
    * OTE pre-compile dependency. Left for binary compatibility for 0.9.8
    */
   public static boolean isValid(String value) {
      return value != null && value.length() > 0;
   }

   public static boolean isValid(CharSequence... values) {
      for (CharSequence value : values) {
         if (value == null || value.length() == 0) {
            return false;
         }
      }
      return true;
   }

   public static String emptyString() {
      return EMPTY_STRING;
   }

   /**
    * Adjusts '&'-containing strings to break the keyboard shortcut ("Accelerator") feature some widgets offer, where
    * &Test will make Alt+T a shortcut. This method breaks the accelerator by escaping ampersands.
    * 
    * @return a string with doubled ampersands.
    */
   public static String escapeAmpersands(String stringWithAmp) {
      return isValid(stringWithAmp) ? stringWithAmp.replace("&", "&&") : null;
   }

   public static String intern(String str) {
      return (str == null) ? null : str.intern();
   }

   /**
    * Will truncate string if necessary and add "..." to end if addDots and truncated
    */
   public static String truncate(String value, int length, boolean ellipsis) {
      if (value == null) {
         return emptyString();
      }
      String toReturn = value;
      if (Strings.isValid(value) && value.length() > length) {
         int len = ellipsis && length - 3 > 0 ? length - 3 : length;
         toReturn = value.substring(0, Math.min(length, len)) + (ellipsis ? "..." : emptyString());
      }
      return toReturn;
   }

   public static String truncate(String value, int length) {
      return truncate(value, length, false);
   }

   public static String unquote(String nameReference) {
      String toReturn = nameReference;
      if (toReturn != null) {
         toReturn = toReturn.trim();
         if (Strings.isValid(toReturn) && toReturn.startsWith("\"") && toReturn.endsWith("\"")) {
            toReturn = toReturn.substring(1, toReturn.length() - 1);
         }
      }
      return toReturn;
   }

   public static String quote(String nameReference) {
      String toReturn = nameReference;
      if (Strings.isValid(nameReference)) {
         toReturn = String.format("\"%s\"", nameReference);
      }
      return toReturn;
   }

   /**
    * <p>
    * Remove all <code>\n</code> and <code>\t</code>.
    * </p>
    * 
    * @param value
    * @return
    */
   public static String minimize(String value) {
      return isValid(value) ? value.replaceAll("\n|\t", "") : value;
   }

   /**
    * Provides a nicer list of items with an 'and' at the end. This could be done using iterator().
    * 
    * @param items Lists of form { apple, banana, orange } or { apple, banana }
    * @return string of form "apple, banana and orange" or "apple and banana" depending on size of list
    */
   public static String buildStatment(List<?> items) {
      StringBuilder niceList = new StringBuilder();
      if (items.size() >= 2) {
         int andIndex = items.size() - 2;
         for (int itemIndex = 0; itemIndex < items.size(); itemIndex++) {
            niceList.append(items.get(itemIndex));
            if (itemIndex == andIndex) {
               niceList.append(" and ");
            } else if (itemIndex < andIndex) {
               niceList.append(", ");
            }
         }
      } else {
         if (!items.isEmpty()) {
            niceList.append(items.get(0));
         }
      }
      return niceList.toString();
   }
}

Back to the top