Fix standard functions for nullable types
[platform/upstream/libgee.git] / gee / functions.vala
1 /* functions.vala
2  *
3  * Copyright (C) 2009  Didier Villevalois, Maciej Piechotka
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
21  *      Maciej Piechotka <uzytkownik2@gmail.com>
22  */
23
24 using GLib;
25
26 namespace Gee {
27
28         /**
29          * Helpers for equal, hash and compare functions.
30          *
31          * With those functions, you can retrieve the equal, hash and compare
32          * functions that best match your element, key or value types. Supported
33          * types are (non-boxed) primitive, string and ``Object`` types.
34          *
35          * A special care is taken for classes inheriting from the
36          * {@link Comparable} interface. For such types, an appropriate compare
37          * function is returned that calls {@link Comparable.compare_to}.
38          *
39          */
40         namespace Functions {
41
42                 /**
43                  * Get a equality testing function for a given type.
44                  *
45                  * @param t the type which to get an equality testing function for.
46                  *
47                  * @return the equality testing function corresponding to the given type.
48                  */
49                 public static EqualDataFunc get_equal_func_for (Type t) {
50                         if (t == typeof (string)) {
51                                 return (a, b) => {
52                                         if (a == b)
53                                                 return true;
54                                         else if (a == null || b == null)
55                                                 return false;
56                                         else
57                                                 return str_equal ((string) a, (string) b);
58                                 };
59                         } else if (t.is_a (typeof (Hashable))) {
60                                 return (a, b) => {
61                                         if (a == b)
62                                                 return true;
63                                         else if (a == null || b == null)
64                                                 return false;
65                                         else
66                                                 return ((Hashable<Hashable>) a).equal_to ((Hashable) b);
67                                 };
68                         } else if (t.is_a (typeof (Comparable))) {
69                                 return (a, b) => {                                      
70                                         if (a == b)
71                                                 return true;
72                                         else if (a == null || b == null)
73                                                 return false;
74                                         else
75                                                 return ((Comparable<Comparable>) a).compare_to ((Comparable) b) == 0;};
76                         } else {
77                                 return (a, b) => {return direct_equal (a, b);};
78                         }
79                 }
80
81                 /**
82                  * Get a hash function for a given type.
83                  *
84                  * @param t the type which to get the hash function for.
85                  *
86                  * @return the hash function corresponding to the given type.
87                  */
88                 public static HashDataFunc get_hash_func_for (Type t) {
89                         if (t == typeof (string)) {
90                                 return (a) => {
91                                         if (a == null)
92                                                 return (uint)0xdeadbeef;
93                                         else
94                                                 return str_hash ((string) a);
95                                 };
96                         } else if (t.is_a (typeof (Hashable))) {
97                                 return (a) => {
98                                         if (a == null)
99                                                 return (uint)0xdeadbeef;
100                                         else
101                                                 return ((Hashable) a).hash();
102                                 };
103                         } else {
104                                 return (a) => {return direct_hash (a);};
105                         }
106                 }
107
108                 /**
109                  * Get a comparator function for a given type.
110                  *
111                  * @param t the type which to get a comparator function for.
112                  *
113                  * @return the comparator function corresponding to the given type.
114                  */
115                 public static CompareDataFunc get_compare_func_for (Type t) {
116                         if (t == typeof (string)) {
117                                 return (a, b) => {
118                                         if (a == b)
119                                                 return 0;
120                                         else if (a == null)
121                                                 return -1;
122                                         else if (b == null)
123                                                 return 1;
124                                         else
125                                                 return strcmp((string) a, (string) b);
126                                 };
127                         } else if (t.is_a (typeof (Comparable))) {
128                                 return (a, b) => {
129                                         if (a == b)
130                                                 return 0;
131                                         else if (a == null)
132                                                 return -1;
133                                         else if (b == null)
134                                                 return 1;
135                                         else
136                                                 return ((Comparable<Comparable>) a).compare_to ((Comparable) b);
137                                 };
138                         } else {
139                                 return (_val1, _val2) => {
140                                         long val1 = (long)_val1, val2 = (long)_val2;
141                                         if (val1 > val2) {
142                                                 return 1;
143                                         } else if (val1 == val2) {
144                                                 return 0;
145                                         } else {
146                                                 return -1;
147                                         }
148                                 };
149                         }
150                 }
151         }
152 }