root/lang/csharp/LINX/trunk/Linx/Extension/ObjectUtil.cs @ 34861

Revision 34861, 10.5 kB (checked in by takeshik, 4 years ago)

Moved codes from XSpectCommonFramework.

  • Property svn:mime-type set to text/plain; charset=utf-8
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1// -*- mode: csharp; encoding: utf-8; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2// vim:set ft=cs fenc=utf-8 ts=4 sw=4 sts=4 et:
3// $Id$
4/* Linx
5 *   Library that Integrates .NET with eXtremes
6 * Copyright © 2008-2009 Takeshi KIRIYA, XSpect Project <takeshik@users.sf.net>
7 * All rights reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Linq;
31using System.IO;
32using System.Reflection;
33using Achiral.Extension;
34using Achiral;
35
36namespace XSpect.Extension
37{
38    public static class ObjectUtil
39    {
40        public static Boolean If<TReceiver>(this TReceiver self, Func<TReceiver, Boolean> predicate)
41        {
42            return predicate(self);
43        }
44
45        public static TResult If<TReceiver, TResult>(this TReceiver self, Func<TReceiver, Boolean> predicate, TResult valueIfTrue, TResult valueIfFalse)
46        {
47            if (self == null)
48            {
49                return default(TResult);
50            }
51            else if (self == null || predicate(self))
52            {
53                return valueIfTrue;
54            }
55            else
56            {
57                return valueIfFalse;
58            }
59        }
60
61        public static TReceiver If<TReceiver>(this TReceiver self, Func<TReceiver, Boolean> predicate, TReceiver valueIfTrue)
62        {
63            return self.If(predicate, valueIfTrue, self);
64        }
65
66        public static TResult If<TReceiver, TResult>(this TReceiver self, Func<TReceiver, Boolean> predicate, Func<TReceiver, TResult> funcIfTrue, Func<TReceiver, TResult> funcIfFalse)
67        {
68            if (self == null)
69            {
70                return default(TResult);
71            }
72            else if (self == null || predicate(self))
73            {
74                return funcIfTrue(self);
75            }
76            else
77            {
78                return funcIfFalse(self);
79            }
80        }
81
82        public static TReceiver If<TReceiver>(this TReceiver self, Func<TReceiver, Boolean> predicate, Func<TReceiver, TReceiver> funcIfTrue)
83        {
84            return self.If(predicate, funcIfTrue, Lambda.Id<TReceiver>());
85        }
86
87        public static TResult Null<TReceiver, TResult>(this TReceiver self, Func<TReceiver, TResult> func, TResult valueIfNull)
88            where TReceiver : class
89        {
90            if (self == null)
91            {
92                return valueIfNull;
93            }
94            else
95            {
96                return func(self);
97            }
98        }
99
100        public static TResult Null<TReceiver, TResult>(this TReceiver self, Func<TReceiver, TResult> func)
101            where TReceiver : class
102        {
103            return Null(self, func, default(TResult));
104        }
105
106        public static void Null<TReceiver>(this TReceiver self, Action<TReceiver> action)
107        {
108            if (self != null)
109            {
110                action(self);
111            }
112        }
113
114        public static Nullable<TResult> Nullable<TReceiver, TResult>(this TReceiver self, Func<TReceiver, TResult> func)
115            where TResult : struct
116        {
117            if (self == null)
118            {
119                return null;
120            }
121            else
122            {
123                return func(self);
124            }
125        }
126
127        public static Nullable<TReceiver> NullIf<TReceiver>(this TReceiver self, Func<TReceiver, Boolean> predicate)
128            where TReceiver : struct
129        {
130            if (predicate(self))
131            {
132                return null;
133            }
134            else
135            {
136                return self;
137            }
138        }
139
140        public static Boolean IsDefault<TReceiver>(this TReceiver self)
141        {
142            return self.Equals(default(TReceiver));
143        }
144
145        public static TResult Do<TReceiver, TResult>(this TReceiver self, Func<TReceiver, TResult> func)
146        {
147            return func(self);
148        }
149
150        public static TResult[] Do<TReceiver, TResult>(this TReceiver self, params Func<TReceiver, TResult>[] funcs)
151        {
152            return funcs.Select(f => f(self)).ToArray();
153        }
154
155        public static TReceiver Do<TReceiver>(this TReceiver self, params Action<TReceiver>[] actions)
156        {
157            actions.ForEach(a => a(self));
158            return self;
159        }
160
161        public static TReceiver Write<TReceiver>(this TReceiver self)
162        {
163            return self.Write(Console.Out);
164        }
165
166        public static TReceiver WriteLine<TReceiver>(this TReceiver self)
167        {
168            return self.WriteLine(Console.Out);
169        }
170
171        public static TReceiver Write<TReceiver>(this TReceiver self, TextWriter writer)
172        {
173            return self.Do(o => writer.Write(o));
174        }
175
176        public static TReceiver WriteLine<TReceiver>(this TReceiver self, TextWriter writer)
177        {
178            return self.Do(o => writer.WriteLine(o));
179        }
180
181        public static void Void(this Object self)
182        {
183            return;
184        }
185
186        public static Boolean True(this Object self)
187        {
188            return true;
189        }
190
191        public static Boolean False(this Object self)
192        {
193            return false;
194        }
195
196        public static Boolean Try<TReceiver, TResult>(this TReceiver self, Func<TReceiver, TResult> func, out TResult result)
197        {
198            try
199            {
200                result = func(self);
201                return true;
202            }
203            catch (Exception)
204            {
205                result = default(TResult);
206                return false;
207            }
208        }
209
210        public static Boolean Try<TReceiver>(this TReceiver self, Action<TReceiver> action)
211        {
212            try
213            {
214                action(self);
215                return true;
216            }
217            catch (Exception)
218            {
219                return false;
220            }
221        }
222
223        public static TResult Scope<TReceiver, TResult>(
224            this TReceiver self,
225            Action<TReceiver> begin,
226            Func<TReceiver, TResult> body,
227            Action<TReceiver> end
228        )
229        {
230            if (begin != null)
231            {
232                begin(self);
233            }
234            TResult ret = body(self);
235            if (begin != null)
236            {
237                end(self);
238            }
239            return ret;
240        }
241
242        public static void Scope<TReceiver>(
243            this TReceiver self,
244            Action<TReceiver> begin,
245            Action<TReceiver> body,
246            Action<TReceiver> end
247        )
248        {
249            if (begin != null)
250            {
251                begin(self);
252            }
253            body(self);
254            if (begin != null)
255            {
256                end(self);
257            }
258        }
259
260        public static Boolean EqualsAny(this Object self, params Object[] objects)
261        {
262            return objects.Any(self.Equals);
263        }
264
265        public static Boolean EqualsAll(this Object self, params Object[] objects)
266        {
267            return objects.All(self.Equals);
268        }
269
270        public static TResult MemberOf<TResult>(this Object self, String name)
271        {
272            MemberInfo member = self.GetType()
273                .GetMember(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
274                .Single();
275            switch (member.MemberType)
276            {
277                case MemberTypes.Field:
278                    return (TResult) (member as FieldInfo).GetValue(self);
279                case MemberTypes.Property:
280                    return (TResult) (member as PropertyInfo).GetValue(self, null);
281                case MemberTypes.Method:
282                    return (TResult) ((Object) (member as MethodInfo).CreateDelegateFromType(typeof(TResult), self));
283                default:
284                    throw new NotSupportedException();
285            }
286        }
287
288        public static TReturn Walk<TReturn, TParam>(this TReturn origin, Func<TReturn, TParam, TReturn> walker, IEnumerable<TParam> parameters)
289        {
290            if (parameters.Any())
291            {
292                return parameters.Select(p => origin = walker(origin, p)).Last();
293            }
294            else
295            {
296                return origin;
297            }
298        }
299
300        public static TReturn Walk<TReturn, TParam>(this TReturn origin, Func<TReturn, TParam, TReturn> walker, params TParam[] parameters)
301        {
302            return origin.Walk(walker, parameters as IEnumerable<TParam>);
303        }
304
305        public static TReceiver MemberwiseClone<TReceiver>(this TReceiver self)
306        {
307            return (TReceiver) self.GetType()
308                .GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic)
309                .Invoke(self, null);
310        }
311
312        public static TReceiver Merge<TReceiver>(this TReceiver self, TReceiver difference)
313            where TReceiver : new()
314        {
315            TReceiver init = new TReceiver();
316            TReceiver result = self.MemberwiseClone();
317            Object v = null;
318            typeof(TReceiver).GetFields(BindingFlags.Instance | BindingFlags.Public)
319                .Where(f => f.GetValue(init) != (v = f.GetValue(difference)))
320                .ForEach(f => f.SetValue(result, v));
321            typeof(TReceiver).GetProperties(BindingFlags.Instance | BindingFlags.Public)
322                .Where(p =>
323                    p.CanRead &&
324                    p.CanWrite &&
325                    p.GetValue(init, null) != (v = p.GetValue(difference, null))
326                )
327                .ForEach(p => p.SetValue(result, v, null));
328            return result;
329        }
330    }
331}
Note: See TracBrowser for help on using the browser.