Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/Default/Decomposers/X2StringDecomposer.cs @ 1476

Last change on this file since 1476 was 1476, checked in by epitzer, 16 years ago

Decomposers for all primitive types except string. (#563)

File size: 3.2 KB
Line 
1using System;
2using HeuristicLab.Persistence.Interfaces;
3using HeuristicLab.Persistence.Core;
4using System.Collections.Generic;
5
6namespace HeuristicLab.Persistence.Default.Decomposers {
7 
8  public class Int2StringDecomposer : IDecomposer {
9
10    public bool CanDecompose(Type type) {
11      return type == typeof (int);
12    }
13
14    public IEnumerable<Tag> DeCompose(object obj) {
15      yield return new Tag(obj.ToString());
16    }
17
18    public object CreateInstance(Type type) {
19      return null;
20    }
21
22    public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
23      foreach ( Tag tag in tags ) {
24        return int.Parse((string) tag.Value);
25      }
26      throw new ApplicationException("Not enough components to compose an integer.");
27    }
28   
29  }
30
31  public class Long2StringDecomposer : IDecomposer {
32
33    public bool CanDecompose(Type type) {
34      return type == typeof(long);
35    }
36
37    public IEnumerable<Tag> DeCompose(object obj) {
38      yield return new Tag(obj.ToString());
39    }
40
41    public object CreateInstance(Type type) {
42      return null;
43    }
44
45    public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
46      foreach (Tag tag in tags) {
47        return long.Parse((string)tag.Value);
48      }
49      throw new ApplicationException("Not enough components to compose an integer.");
50    }
51
52  }
53
54  public class Double2StringDecomposer : IDecomposer {
55
56    public bool CanDecompose(Type type) {
57      return type == typeof (double);
58    }
59
60    public IEnumerable<Tag> DeCompose(object obj) {
61      yield return new Tag(((double)obj).ToString("r"));
62    }
63
64    public object CreateInstance(Type type) {
65      return null;
66    }
67
68    public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
69      foreach (Tag tag in tags) {
70        return double.Parse((string)tag.Value);
71      }
72      throw new ApplicationException("Not enough components to compose a double.");
73    }
74
75  }
76
77  public class Bool2StringDecomposer : IDecomposer {
78
79    public bool CanDecompose(Type type) {
80      return type == typeof(bool);
81    }
82
83    public IEnumerable<Tag> DeCompose(object obj) {
84      yield return new Tag(obj.ToString());
85    }
86
87    public object CreateInstance(Type type) {
88      return null;
89    }
90
91    public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
92      foreach (Tag tag in tags) {
93        return bool.Parse((string) tag.Value);
94      }
95      throw new ApplicationException("Not enough components to compose a bool.");
96    }
97
98  }
99
100  public class DateTime2StringDecomposer : IDecomposer {
101
102    public bool CanDecompose(Type type) {
103      return type == typeof(DateTime);
104    }
105
106    public IEnumerable<Tag> DeCompose(object obj) {
107      yield return new Tag(((DateTime)obj).Ticks);
108    }
109
110    public object CreateInstance(Type type) {
111      return null;
112    }
113
114    public object Populate(object instance, IEnumerable<Tag> tags, Type type) {
115      foreach (Tag tag in tags) {
116        return new DateTime((long)tag.Value);
117      }
118      throw new ApplicationException("Not enough components to compose a bool.");
119    }
120
121  } 
122
123}
Note: See TracBrowser for help on using the repository browser.