PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sys/java/fan/sys/Facets.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 139 lines | 106 code | 17 blank | 16 comment | 26 complexity | 4d6bbc6cf5f39d1accfb6bb9d27df6bd MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 4 Jul 06 Brian Frank Creation
  7. // 4 Sep 07 Brian Frank Rework for new design
  8. //
  9. package fan.sys;
  10. import java.util.Iterator;
  11. import java.util.HashMap;
  12. import java.util.Map.Entry;
  13. import fanx.fcode.*;
  14. import fanx.serial.*;
  15. /**
  16. * Facets manages facet meta-data as a Str:Obj map.
  17. */
  18. public final class Facets
  19. {
  20. public static Facets empty()
  21. {
  22. Facets x = emptyVal;
  23. if (x == null) x = emptyVal = new Facets(new HashMap());
  24. return x;
  25. }
  26. public static Facets makeTransient()
  27. {
  28. Facets x = transientVal;
  29. if (x == null)
  30. {
  31. HashMap m = new HashMap();
  32. m.put(Sys.TransientType, "");
  33. x = transientVal = new Facets(m);
  34. }
  35. return x;
  36. }
  37. static Facets mapFacets(Pod pod, FAttrs.FFacet[] ffacets)
  38. {
  39. if (ffacets == null || ffacets.length == 0) return empty();
  40. HashMap map = new HashMap(ffacets.length*3);
  41. for (int i=0; i<ffacets.length; ++i)
  42. {
  43. FAttrs.FFacet ff = ffacets[i];
  44. Type t = pod.type(ff.type);
  45. if (!t.isJava()) map.put(t, ff.val);
  46. }
  47. return new Facets(map);
  48. }
  49. private Facets(HashMap map) { this.map = map; }
  50. final synchronized List list()
  51. {
  52. if (list == null)
  53. {
  54. list = new List(Sys.FacetType, map.size());
  55. Iterator it = map.keySet().iterator();
  56. while (it.hasNext())
  57. {
  58. Type type = (Type)it.next();
  59. list.add(get(type, true));
  60. }
  61. list = (List)list.toImmutable();
  62. }
  63. return list;
  64. }
  65. final synchronized Facet get(Type type, boolean checked)
  66. {
  67. Object val = map.get(type);
  68. if (val instanceof Facet) return (Facet)val;
  69. if (val instanceof String)
  70. {
  71. Facet f = decode(type, (String)val);
  72. map.put(type, f);
  73. return f;
  74. }
  75. if (checked) throw UnknownFacetErr.make(type.qname());
  76. return null;
  77. }
  78. final Facet decode(Type type, String s)
  79. {
  80. try
  81. {
  82. // if no string use make/defVal
  83. if (s.length() == 0) return (Facet)type.make();
  84. // decode using normal Fantom serialization
  85. return (Facet)ObjDecoder.decode(s);
  86. }
  87. catch (Throwable e)
  88. {
  89. String msg = "ERROR: Cannot decode facet " + type + ": " + s;
  90. System.out.println(msg);
  91. e.printStackTrace();
  92. map.remove(type);
  93. throw IOErr.make(msg);
  94. }
  95. }
  96. final Facets dup()
  97. {
  98. return new Facets((HashMap)map.clone());
  99. }
  100. final void inherit(Facets facets)
  101. {
  102. if (facets.map.size() == 0) return;
  103. list = null;
  104. Iterator it = facets.map.entrySet().iterator();
  105. while (it.hasNext())
  106. {
  107. Entry entry = (Entry)it.next();
  108. Type key = (Type)entry.getKey();
  109. // if already mapped skipped
  110. if (map.get(key) != null) continue;
  111. // if not an inherited facet skip it
  112. FacetMeta meta = (FacetMeta)key.facet(Sys.FacetMetaType, false);
  113. if (meta == null || !meta.inherited) continue;
  114. // inherit
  115. map.put(key, entry.getValue());
  116. }
  117. }
  118. private static Facets emptyVal;
  119. private static Facets transientVal;
  120. private HashMap map; // Type : String/Facet, lazy decoding
  121. private List list; // Facet[]
  122. }