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