react-native-navigation的迁移库

Parser.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.reactnativenavigation.params.parsers;
  2. import android.os.Bundle;
  3. import com.reactnativenavigation.params.StyleParams;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. public class Parser {
  8. static boolean hasKey(Bundle bundle, String key) {
  9. return bundle.keySet().contains(key);
  10. }
  11. static void assertKeyExists(Bundle bundle, String key) {
  12. if (!hasKey(bundle, key)) {
  13. throw new KeyDoesNotExistsException(key);
  14. }
  15. }
  16. private static class KeyDoesNotExistsException extends RuntimeException {
  17. KeyDoesNotExistsException(String key) {
  18. super(key);
  19. }
  20. }
  21. interface ParseStrategy<T> {
  22. T parse(Bundle params);
  23. }
  24. <T> List<T> parseBundle(Bundle params, ParseStrategy<T> strategy) {
  25. ArrayList<T> result = new ArrayList<>(Collections.nCopies(params.keySet().size(), (T) null));
  26. for (String key : params.keySet()) {
  27. result.set(Integer.parseInt(key), strategy.parse(params.getBundle(key)));
  28. }
  29. return result;
  30. }
  31. protected StyleParams.Color getColor(Bundle bundle, String key, StyleParams.Color defaultColor) {
  32. StyleParams.Color color = StyleParams.Color.parse(bundle, key);
  33. return color.hasColor() || defaultColor == null ? color : defaultColor;
  34. }
  35. }