View Javadoc
1   package com.atlassian.activeobjects.internal;
2   
3   import static com.atlassian.activeobjects.ao.ConverterUtils.toLowerCase;
4   import static com.google.common.base.Preconditions.checkNotNull;
5   
6   public final class SimplePrefix implements Prefix {
7       private static final String DEFAULT_SEPARATOR = "_";
8       private final String prefix;
9       private final String separator;
10  
11      public SimplePrefix(String prefix) {
12          this(prefix, DEFAULT_SEPARATOR);
13      }
14  
15      public SimplePrefix(String prefix, String separator) {
16          this.prefix = checkNotNull(prefix);
17          this.separator = checkNotNull(separator);
18      }
19  
20      public String prepend(String string) {
21          return new StringBuilder().append(prefix).append(separator).append(string).toString();
22      }
23  
24      /**
25       * Checks whether the string parameter starts with the prefix. This method is {@code null} safe and will return
26       * {@code false} if the {@code string} parameter is {@code null}.
27       * When checking case insensitively, the default locale will be used to lower case both the prefix and the
28       * {@code string} parameter before comparing.
29       *
30       * @param string        checks whether {@code this} starts the String
31       * @param caseSensitive whether or not we're case sensitive
32       * @return {@code true} of {@code string} starts with the prefix
33       */
34      public boolean isStarting(String string, boolean caseSensitive) {
35          return string != null &&
36                  transform(string, caseSensitive).startsWith(transform(prefix + separator, caseSensitive));
37      }
38  
39      private String transform(String s, boolean caseSensitive) {
40          return caseSensitive ? s : toLowerCase(s);
41      }
42  }