DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ObjectData.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Globalization;
20 
21 namespace Deveel.Data.Serialization {
22  public sealed class ObjectData {
23  private readonly Dictionary<string, object> values;
24 
25  internal ObjectData(Type entityType, IEnumerable<KeyValuePair<string, object>> values) {
26  EntityType = entityType;
27  this.values = new Dictionary<string, object>();
28  if (values != null) {
29  foreach (var pair in values) {
30  this.values[pair.Key] = pair.Value;
31  }
32  }
33  }
34 
35  public Type EntityType { get; private set; }
36 
37  public int Count {
38  get { return values.Count; }
39  }
40 
41  public bool HasValue(string key) {
42  if (String.IsNullOrEmpty(key))
43  throw new ArgumentNullException("key");
44 
45  return values.ContainsKey(key);
46  }
47 
48  public object GetValue(string key) {
49  if (String.IsNullOrEmpty(key))
50  throw new ArgumentNullException("key");
51 
52  object value;
53  if (!values.TryGetValue(key, out value))
54  return null;
55 
56  return value;
57  }
58 
59  public T GetValue<T>(string key) {
60  var value = GetValue(key);
61  if (value == null)
62  return default(T);
63 
64  if (value is T)
65  return (T) value;
66 
67  if (!(value is IConvertible))
68  throw new InvalidCastException();
69 
70  return (T) Convert.ChangeType(value, typeof (T), CultureInfo.InvariantCulture);
71  }
72 
73  public bool GetBoolean(string key) {
74  return GetValue<bool>(key);
75  }
76 
77  public byte GetByte(string key) {
78  return GetValue<byte>(key);
79  }
80 
81  public short GetInt16(string key) {
82  return GetValue<short>(key);
83  }
84 
85  public int GetInt32(string key) {
86  return GetValue<int>(key);
87  }
88 
89  public long GetInt64(string key) {
90  return GetValue<long>(key);
91  }
92 
93  public float GetSingle(string key) {
94  return GetValue<float>(key);
95  }
96 
97  public double GetDouble(string key) {
98  return GetValue<double>(key);
99  }
100 
101  public string GetString(string key) {
102  return GetValue<string>(key);
103  }
104  }
105 }
readonly Dictionary< string, object > values
Definition: ObjectData.cs:23
ObjectData(Type entityType, IEnumerable< KeyValuePair< string, object >> values)
Definition: ObjectData.cs:25