PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/dotnet/fan/sys/Facets.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 105 lines | 79 code | 13 blank | 13 comment | 15 complexity | 1171e5e448c1ccbd2d2b0170acec5b0c 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. // 27 Sep 06 Andy Frank Creation
  7. // 4 Sep 07 Andy Frank Rework for new design
  8. //
  9. using System.Collections;
  10. using System.Runtime.CompilerServices;
  11. using Fanx.Fcode;
  12. using Fanx.Serial;
  13. namespace Fan.Sys
  14. {
  15. /// <summary>
  16. /// Facets manages facet meta-data as a string:Obj map.
  17. /// </summary>
  18. public sealed class Facets
  19. {
  20. public static Facets empty()
  21. {
  22. Facets e = m_empty;
  23. if (e != null) return e;
  24. return m_empty = new Facets(new Hashtable());
  25. }
  26. public static Facets mapFacets(Pod pod, FAttrs.FFacet[] ffacets)
  27. {
  28. if (ffacets == null || ffacets.Length == 0) return empty();
  29. Hashtable map = new Hashtable();
  30. for (int i=0; i<ffacets.Length; ++i)
  31. {
  32. FAttrs.FFacet ff = ffacets[i];
  33. Type t = pod.findType(ff.type);
  34. map[t] = ff.val;
  35. }
  36. return new Facets(map);
  37. }
  38. private Facets(Hashtable map) { this.m_map = map; }
  39. [MethodImpl(MethodImplOptions.Synchronized)]
  40. public List list()
  41. {
  42. if (m_list == null)
  43. {
  44. m_list = new List(Sys.FacetType, m_map.Count);
  45. IDictionaryEnumerator en = ((Hashtable)m_map.Clone()).GetEnumerator();
  46. while (en.MoveNext())
  47. {
  48. Type type = (Type)en.Key;
  49. m_list.add(get(type, true));
  50. }
  51. m_list = (List)m_list.toImmutable();
  52. }
  53. return m_list;
  54. }
  55. [MethodImpl(MethodImplOptions.Synchronized)]
  56. public Facet get(Type type, bool check)
  57. {
  58. object val = m_map[type];
  59. if (val is Facet) return (Facet)val;
  60. if (val is string)
  61. {
  62. Facet f = decode(type, (string)val);
  63. m_map[type] = f;
  64. return f;
  65. }
  66. if (check) throw UnknownFacetErr.make(type.qname()).val;
  67. return null;
  68. }
  69. [MethodImpl(MethodImplOptions.Synchronized)]
  70. public Facet decode(Type type, string s)
  71. {
  72. try
  73. {
  74. // if no string use make/defVal
  75. if (s.Length == 0) return (Facet)type.make();
  76. // decode using normal Fantom serialization
  77. return (Facet)ObjDecoder.decode(s);
  78. }
  79. catch (System.Exception e)
  80. {
  81. string msg = "ERROR: Cannot decode facet " + type + ": " + s;
  82. System.Console.WriteLine(msg);
  83. Err.dumpStack(e);
  84. m_map.Remove(type);
  85. throw IOErr.make(msg).val;
  86. }
  87. }
  88. private static Facets m_empty;
  89. private Hashtable m_map; // Type : String/Facet, lazy decoding
  90. private List m_list; // Facet[]
  91. }
  92. }