aboutsummaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/base/CaseFormat.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava/src/com/google/common/base/CaseFormat.java')
-rw-r--r--guava/src/com/google/common/base/CaseFormat.java162
1 files changed, 83 insertions, 79 deletions
diff --git a/guava/src/com/google/common/base/CaseFormat.java b/guava/src/com/google/common/base/CaseFormat.java
index b8b02c9..8ef7c5c 100644
--- a/guava/src/com/google/common/base/CaseFormat.java
+++ b/guava/src/com/google/common/base/CaseFormat.java
@@ -16,8 +16,6 @@
package com.google.common.base;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.annotations.GwtCompatible;
/**
@@ -31,74 +29,27 @@ public enum CaseFormat {
/**
* Hyphenated variable naming convention, e.g., "lower-hyphen".
*/
- LOWER_HYPHEN(CharMatcher.is('-'), "-") {
- @Override String normalizeWord(String word) {
- return Ascii.toLowerCase(word);
- }
- @Override String convert(CaseFormat format, String s) {
- if (format == LOWER_UNDERSCORE) {
- return s.replace('-', '_');
- }
- if (format == UPPER_UNDERSCORE) {
- return Ascii.toUpperCase(s.replace('-', '_'));
- }
- return super.convert(format, s);
- }
- },
+ LOWER_HYPHEN(CharMatcher.is('-'), "-"),
/**
* C++ variable naming convention, e.g., "lower_underscore".
*/
- LOWER_UNDERSCORE(CharMatcher.is('_'), "_") {
- @Override String normalizeWord(String word) {
- return Ascii.toLowerCase(word);
- }
- @Override String convert(CaseFormat format, String s) {
- if (format == LOWER_HYPHEN) {
- return s.replace('_', '-');
- }
- if (format == UPPER_UNDERSCORE) {
- return Ascii.toUpperCase(s);
- }
- return super.convert(format, s);
- }
- },
+ LOWER_UNDERSCORE(CharMatcher.is('_'), "_"),
/**
* Java variable naming convention, e.g., "lowerCamel".
*/
- LOWER_CAMEL(CharMatcher.inRange('A', 'Z'), "") {
- @Override String normalizeWord(String word) {
- return firstCharOnlyToUpper(word);
- }
- },
+ LOWER_CAMEL(CharMatcher.inRange('A', 'Z'), ""),
/**
* Java and C++ class naming convention, e.g., "UpperCamel".
*/
- UPPER_CAMEL(CharMatcher.inRange('A', 'Z'), "") {
- @Override String normalizeWord(String word) {
- return firstCharOnlyToUpper(word);
- }
- },
+ UPPER_CAMEL(CharMatcher.inRange('A', 'Z'), ""),
/**
* Java and C++ constant naming convention, e.g., "UPPER_UNDERSCORE".
*/
- UPPER_UNDERSCORE(CharMatcher.is('_'), "_") {
- @Override String normalizeWord(String word) {
- return Ascii.toUpperCase(word);
- }
- @Override String convert(CaseFormat format, String s) {
- if (format == LOWER_HYPHEN) {
- return Ascii.toLowerCase(s.replace('_', '-'));
- }
- if (format == LOWER_UNDERSCORE) {
- return Ascii.toLowerCase(s);
- }
- return super.convert(format, s);
- }
- };
+ UPPER_UNDERSCORE(CharMatcher.is('_'), "_");
private final CharMatcher wordBoundary;
private final String wordSeparator;
@@ -109,21 +60,51 @@ public enum CaseFormat {
}
/**
- * Converts the specified {@code String str} from this format to the specified {@code format}. A
- * "best effort" approach is taken; if {@code str} does not conform to the assumed format, then
- * the behavior of this method is undefined but we make a reasonable effort at converting anyway.
+ * Converts the specified {@code String s} from this format to the specified {@code format}. A
+ * "best effort" approach is taken; if {@code s} does not conform to the assumed format, then the
+ * behavior of this method is undefined but we make a reasonable effort at converting anyway.
*/
- public final String to(CaseFormat format, String str) {
- checkNotNull(format);
- checkNotNull(str);
- return (format == this) ? str : convert(format, str);
- }
+ public String to(CaseFormat format, String s) {
+ if (format == null) {
+ throw new NullPointerException();
+ }
+ if (s == null) {
+ throw new NullPointerException();
+ }
- /**
- * Enum values can override for performance reasons.
- */
- String convert(CaseFormat format, String s) {
- // deal with camel conversion
+ if (format == this) {
+ return s;
+ }
+
+ /* optimize cases where no camel conversion is required */
+ switch (this) {
+ case LOWER_HYPHEN:
+ switch (format) {
+ case LOWER_UNDERSCORE:
+ return s.replace('-', '_');
+ case UPPER_UNDERSCORE:
+ return Ascii.toUpperCase(s.replace('-', '_'));
+ }
+ break;
+ case LOWER_UNDERSCORE:
+ switch (format) {
+ case LOWER_HYPHEN:
+ return s.replace('_', '-');
+ case UPPER_UNDERSCORE:
+ return Ascii.toUpperCase(s);
+ }
+ break;
+ case UPPER_UNDERSCORE:
+ switch (format) {
+ case LOWER_HYPHEN:
+ return Ascii.toLowerCase(s.replace('_', '-'));
+ case LOWER_UNDERSCORE:
+ return Ascii.toLowerCase(s);
+ }
+ break;
+ }
+
+ // otherwise, deal with camel conversion
StringBuilder out = null;
int i = 0;
int j = -1;
@@ -138,23 +119,46 @@ public enum CaseFormat {
out.append(format.wordSeparator);
i = j + wordSeparator.length();
}
- return (i == 0)
- ? format.normalizeFirstWord(s)
- : out.append(format.normalizeWord(s.substring(i))).toString();
+ if (i == 0) {
+ return format.normalizeFirstWord(s);
+ }
+ out.append(format.normalizeWord(s.substring(i)));
+ return out.toString();
}
- abstract String normalizeWord(String word);
-
private String normalizeFirstWord(String word) {
- return (this == LOWER_CAMEL) ? Ascii.toLowerCase(word) : normalizeWord(word);
+ switch (this) {
+ case LOWER_CAMEL:
+ return Ascii.toLowerCase(word);
+ default:
+ return normalizeWord(word);
+ }
+ }
+
+ private String normalizeWord(String word) {
+ switch (this) {
+ case LOWER_HYPHEN:
+ return Ascii.toLowerCase(word);
+ case LOWER_UNDERSCORE:
+ return Ascii.toLowerCase(word);
+ case LOWER_CAMEL:
+ return firstCharOnlyToUpper(word);
+ case UPPER_CAMEL:
+ return firstCharOnlyToUpper(word);
+ case UPPER_UNDERSCORE:
+ return Ascii.toUpperCase(word);
+ }
+ throw new RuntimeException("unknown case: " + this);
}
private static String firstCharOnlyToUpper(String word) {
- return (word.isEmpty())
- ? word
- : new StringBuilder(word.length())
- .append(Ascii.toUpperCase(word.charAt(0)))
- .append(Ascii.toLowerCase(word.substring(1)))
- .toString();
+ int length = word.length();
+ if (length == 0) {
+ return word;
+ }
+ return new StringBuilder(length)
+ .append(Ascii.toUpperCase(word.charAt(0)))
+ .append(Ascii.toLowerCase(word.substring(1)))
+ .toString();
}
}