File: 08-preserve-enums-methods.patch

package info (click to toggle)
guava-libraries 32.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,472 kB
  • sloc: java: 359,877; xml: 2,612; sh: 34; javascript: 12; makefile: 8
file content (68 lines) | stat: -rw-r--r-- 2,438 bytes parent folder | download
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
Description: Preserves the Enums.valueOfFunction() method that was removed in Guava 18.
Author: Emmanuel Bourg <ebourg@apache.org>
Forwarded: not-needed
--- a/guava/src/com/google/common/base/Enums.java
+++ b/guava/src/com/google/common/base/Enums.java
@@ -58,6 +58,62 @@
     }
   }
 
+
+  /**
+   * Returns a {@link Function} that maps an {@link Enum} name to the associated {@code Enum}
+   * constant. The {@code Function} will return {@code null} if the {@code Enum} constant
+   * does not exist.
+   *
+   * @param enumClass the {@link Class} of the {@code Enum} declaring the constant values
+   * @deprecated Use {@link Enums#stringConverter} instead. Note that the string converter has
+   *     slightly different behavior: it throws {@link IllegalArgumentException} if the enum
+   *     constant does not exist rather than returning {@code null}. It also converts {@code null}
+   *     to {@code null} rather than throwing {@link NullPointerException}. This method is
+   *     scheduled for removal in Guava 18.0.
+   */
+  @Deprecated
+  public static <T extends Enum<T>> Function<String, T> valueOfFunction(
+      Class<T> enumClass) {
+    return new ValueOfFunction<T>(enumClass);
+  }
+
+  /**
+   * A {@link Function} that maps an {@link Enum} name to the associated constant, or {@code null}
+   * if the constant does not exist.
+   */
+  private static final class ValueOfFunction<T extends Enum<T>>
+      implements Function<String, T>, Serializable {
+
+    private final Class<T> enumClass;
+
+    private ValueOfFunction(Class<T> enumClass) {
+      this.enumClass = checkNotNull(enumClass);
+    }
+
+    @Override
+    public T apply(String value) {
+      try {
+        return Enum.valueOf(enumClass, value);
+      } catch (IllegalArgumentException e) {
+        return null;
+      }
+    }
+
+    @Override public boolean equals(Object obj) {
+      return obj instanceof ValueOfFunction && enumClass.equals(((ValueOfFunction) obj).enumClass);
+    }
+
+    @Override public int hashCode() {
+      return enumClass.hashCode();
+    }
+
+    @Override public String toString() {
+      return "Enums.valueOf(" + enumClass + ")";
+    }
+
+    private static final long serialVersionUID = 0;
+  }
+
   /**
    * Returns an optional enum constant for the given type, using {@link Enum#valueOf}. If the
    * constant does not exist, {@link Optional#absent} is returned. A common use case is for parsing