Skip to main content
summaryrefslogtreecommitdiffstats
blob: de18159c7e7cf15e308ed4f986f2d29d69b79c8c (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
/*******************************************************************************
 * 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;

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

   private Strings() {
      // Utility class
   }

   public static boolean isValid(CharSequence value) {
      return value != null && value.length() > 0;
   }

   public static String emptyString() {
      return EMPTY_STRING;
   }

   /**
    * This method 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) {
      if (isValid(stringWithAmp)) {
         return stringWithAmp.replace("&", "&&");
      } else {
         return null;
      }
   }

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

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

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

}

Back to the top