35
|
1 using System.Collections;
|
20
|
2 using System.Collections.Generic;
|
|
3 using System;
|
|
4 using System.Text;
|
32
|
5 using System.Runtime.Serialization.Formatters.Binary;
|
|
6 using System.IO;
|
20
|
7
|
|
8 namespace JungleDB {
|
|
9 public class DefaultTreeNodeAttribute : TreeNodeAttributes {
|
|
10 public List<TreeNode> children;
|
31
|
11 public TreeMap<string, object> attrs;
|
20
|
12
|
31
|
13 public DefaultTreeNodeAttribute(List<TreeNode> _children, TreeMap<string, object> _attrs){
|
28
|
14 children = _children;
|
20
|
15 attrs = _attrs;
|
|
16 }
|
|
17
|
31
|
18 public TreeMap<string, object> getAttributesAsRawMap(){
|
20
|
19 return attrs;
|
|
20 }
|
|
21
|
|
22 public Either<Error, TreeNode> delete(string _key) {
|
|
23 if (_key == null) {
|
|
24 return DefaultEither<Error,TreeNode>.newA (NodeEditorError.NULL_VALUE_NOT_ALLOWED);
|
|
25 }
|
|
26
|
|
27 if (null == attrs.getRoot()) {
|
|
28 return DefaultEither<Error,TreeNode>.newA(NodeEditorError.DELETE_KEY_NOT_FOUND);
|
|
29 }
|
|
30
|
31
|
31 TreeMap<string, object> newMap = attrs.delete(_key);
|
20
|
32 TreeNode newNode = new DefaultTreeNode(children, newMap);
|
|
33 return DefaultEither<Error,TreeNode>.newB(newNode);
|
|
34 }
|
|
35
|
31
|
36 public Either<Error, TreeNode> put(string _key, object _value){
|
20
|
37 if (_key == null || _value == null) {
|
|
38 return DefaultEither<Error, TreeNode>.newA (NodeEditorError.NULL_VALUE_NOT_ALLOWED);
|
|
39 }
|
|
40
|
31
|
41 TreeMap<string, object> newMap = attrs.put (_key, _value);
|
20
|
42
|
|
43 TreeNode newNode = new DefaultTreeNode (children, newMap);
|
|
44
|
|
45 return DefaultEither<Error, TreeNode>.newB (newNode);
|
|
46 }
|
|
47
|
31
|
48 public object get(string _key) {
|
28
|
49 if (_key == null)
|
31
|
50 return null;
|
28
|
51
|
31
|
52 object op = attrs.get(_key);
|
28
|
53 if (op != null)
|
20
|
54 return op;
|
31
|
55 return null;
|
|
56 }
|
|
57
|
|
58 public T get<T> (string _key) {
|
|
59 if (_key == null)
|
|
60 return default(T);
|
|
61
|
|
62 T op = (T)attrs.get (_key);
|
|
63 if (op != null)
|
|
64 return op;
|
|
65 return default(T);
|
20
|
66 }
|
|
67
|
28
|
68 public string getString(string key) {
|
31
|
69 return attrs.get(key).ToString();
|
20
|
70 }
|
|
71
|
32
|
72 /// <summary>
|
|
73 /// Gets the bytes.
|
|
74 /// need Serializable to class.
|
|
75 /// </summary>
|
|
76 /// <returns>byte[]</returns>
|
|
77 /// <param name="key">Key.</param>
|
|
78 public byte[] getBytes(string key)
|
|
79 {
|
|
80 var formatter = new BinaryFormatter();
|
|
81 object source = attrs.get (key);
|
|
82 using (var stream = new MemoryStream())
|
|
83 {
|
|
84 formatter.Serialize(stream, source);
|
|
85 return stream.ToArray();
|
|
86 }
|
|
87 }
|
|
88
|
20
|
89 public IEnumerator<string> getKeys(){
|
|
90 return attrs.keys ();
|
|
91 }
|
|
92
|
|
93 }
|
|
94 }
|