fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. public class Main
  8. {
  9.  
  10. public static boolean belongsToAllowedTypes(Object value) {
  11. if (value == null) {
  12. return true;
  13. } else if (value instanceof Boolean) {
  14. return true;
  15. } else if (value instanceof String) {
  16. return true;
  17. } else if (value instanceof Number) {
  18. return true;
  19. } else if (value instanceof byte[]) {
  20. return true;
  21. } else if (value instanceof Set) {
  22. return containsAllowedTypes(((Set<Object>) value).iterator());
  23. } else if (value instanceof List) {
  24. return containsAllowedTypes(((List<Object>) value).iterator());
  25. } else if (value instanceof Map) {
  26. return containsAllowedTypes(((Map<String, Object>) value).values().iterator());
  27. } else {
  28. return false;
  29. }
  30. }
  31.  
  32. public static boolean containsAllowedTypes(Iterator iterator) {
  33. boolean instanceOfAllowedTypes = true;
  34. while (iterator.hasNext() && instanceOfAllowedTypes) {
  35. instanceOfAllowedTypes = belongsToAllowedTypes(iterator.next());
  36. }
  37. return instanceOfAllowedTypes;
  38.  
  39. }
  40.  
  41. public static void main (String[] args) throws java.lang.Exception
  42. {
  43. List stringlist = new ArrayList<>();
  44. stringlist.add("astring");
  45. System.out.println(belongsToAllowedTypes(stringlist));
  46. }
  47. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
true