Repository navigation
Expand file tree
/
Copy pathFun.java
More file actions
464 lines (390 loc) · 14.8 KB
/
Copy pathFun.java
File metadata and controls
464 lines (390 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (c) 2012 Jan Kotek
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapdb;
import java.io.DataInput;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
/**
* Functional stuff. Tuples, function, callback methods etc..
*
* @author Jan Kotek
*/
public final class Fun {
/** place holder for some stuff in future */
public static final Object PLACEHOLDER = new Object(){
@Override public String toString() {
return "Fun.PLACEHOLDER";
}
};
/**
* A utility method for getting a type-safe Comparator, it provides type-inference help.
* Use this method instead of {@link Fun#COMPARATOR} in order to insure type-safety
* ex: {@code Comparator<Integer> comparator = getComparator();}
* @return comparator
*/
public static <T> Comparator<T> comparator(){
return Fun.COMPARATOR;
}
/**
* A utility method for getting a type-safe reversed Comparator (the negation of {@link Fun#comparator()}).
* Use this method instead of {@link Fun#REVERSE_COMPARATOR} in order to insure type-safety
* ex: {@code Comparator<Integer> comparator = getReversedComparator();}
* @return comparator
*/
public static <T> Comparator<T> reverseComparator(){
return Fun.REVERSE_COMPARATOR;
}
@SuppressWarnings("rawtypes")
public static final Comparator COMPARATOR = new Comparator<Comparable>() {
@Override
public int compare(Comparable o1, Comparable o2) {
return o1.compareTo(o2);
}
};
@SuppressWarnings("rawtypes")
public static final Comparator REVERSE_COMPARATOR = new Comparator<Comparable>() {
@Override
public int compare(Comparable o1, Comparable o2) {
return -COMPARATOR.compare(o1,o2);
}
};
public static final Iterator EMPTY_ITERATOR = new ArrayList(0).iterator();
public static <T> Iterator<T> emptyIterator(){
return EMPTY_ITERATOR;
}
private Fun(){}
/** returns true if all elements are equal, works with nulls*/
static public boolean eq(Object a, Object b) {
return a==b || (a!=null && a.equals(b));
}
public static long roundUp(long number, long roundUpToMultipleOf) {
return ((number+roundUpToMultipleOf-1)/(roundUpToMultipleOf))*roundUpToMultipleOf;
}
/** Convert object to string, even if it is primitive array */
static String toString(Object keys) {
if(keys instanceof long[])
return Arrays.toString((long[]) keys);
else if(keys instanceof int[])
return Arrays.toString((int[]) keys);
else if(keys instanceof byte[])
return Arrays.toString((byte[]) keys);
else if(keys instanceof char[])
return Arrays.toString((char[]) keys);
else if(keys instanceof float[])
return Arrays.toString((float[]) keys);
else if(keys instanceof double[])
return Arrays.toString((double[]) keys);
else if(keys instanceof boolean[])
return Arrays.toString((boolean[]) keys);
else if(keys instanceof Object[])
return Arrays.toString((Object[]) keys);
else
return keys.toString();
}
static public final class Pair<A,B> implements Comparable<Pair<A,B>>, Serializable {
private static final long serialVersionUID = -8816277286657643283L;
final public A a;
final public B b;
public Pair(A a, B b) {
this.a = a;
this.b = b;
}
/** constructor used for deserialization*/
protected Pair(SerializerBase serializer, DataInput in, SerializerBase.FastArrayList<Object> objectStack) throws IOException {
objectStack.add(this);
this.a = (A) serializer.deserialize(in, objectStack);
this.b = (B) serializer.deserialize(in, objectStack);
}
@Override public int compareTo(Pair<A,B> o) {
int i = ((Comparable<A>)a).compareTo(o.a);
if(i!=0)
return i;
return ((Comparable<B>)b).compareTo(o.b);
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Pair<?, ?> t = (Pair<?,?>) o;
return eq(a,t.a) && eq(b,t.b);
}
@Override public int hashCode() {
int result = a != null ? a.hashCode() : 0;
result = 31 * result + (b != null ? b.hashCode() : 0);
return result;
}
@Override public String toString() {
return "Pair[" + a +", "+b+"]";
}
}
/** function which takes no argument and returns one value*/
public interface Function0<R>{
R run();
}
/** function which takes one argument and returns one value*/
public interface Function1<R,A>{
R run(A a);
}
/** function which takes one int argument and returns one value*/
public interface Function1Int<R>{
R run(int a);
}
/** function which takes two argument and returns one value*/
public interface Function2<R,A,B>{
R run(A a, B b);
}
public static <K,V> Fun.Function1<K,Pair<K,V>> extractKey(){
return new Fun.Function1<K, Pair<K, V>>() {
@Override
public K run(Pair<K, V> t) {
return t.a;
}
};
}
public static <K,V> Fun.Function1<V,Pair<K,V>> extractValue(){
return new Fun.Function1<V, Pair<K, V>>() {
@Override
public V run(Pair<K, V> t) {
return t.b;
}
};
}
public static <K,V> Fun.Function1<K,Map.Entry<K,V>> extractMapEntryKey(){
return new Fun.Function1<K, Map.Entry<K, V>>() {
@Override
public K run(Map.Entry<K, V> t) {
return t.getKey();
}
};
}
public static <K,V> Fun.Function1<V,Map.Entry<K,V>> extractMapEntryValue(){
return new Fun.Function1<V, Map.Entry<K, V>>() {
@Override
public V run(Map.Entry<K, V> t) {
return t.getValue();
}
};
}
/** returns function which always returns the value itself without transformation */
public static <K> Function1<K,K> extractNoTransform() {
return new Function1<K, K>() {
@Override
public K run(K k) {
return k;
}
};
}
public static final Comparator<byte[]> BYTE_ARRAY_COMPARATOR = new Comparator<byte[]>() {
@Override
public int compare(byte[] o1, byte[] o2) {
if(o1==o2) return 0;
final int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
if(o1[i]==o2[i])
continue;
if(o1[i]>o2[i])
return 1;
return -1;
}
return compareInt(o1.length, o2.length);
}
};
public static final Comparator<char[]> CHAR_ARRAY_COMPARATOR = new Comparator<char[]>() {
@Override
public int compare(char[] o1, char[] o2) {
if(o1==o2) return 0;
final int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
if(o1[i]==o2[i])
continue;
if(o1[i]>o2[i])
return 1;
return -1;
}
return compareInt(o1.length, o2.length);
}
};
public static final Comparator<int[]> INT_ARRAY_COMPARATOR = new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1==o2) return 0;
final int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
if(o1[i]==o2[i])
continue;
if(o1[i]>o2[i])
return 1;
return -1;
}
return compareInt(o1.length, o2.length);
}
};
public static final Comparator<long[]> LONG_ARRAY_COMPARATOR = new Comparator<long[]>() {
@Override
public int compare(long[] o1, long[] o2) {
if(o1==o2) return 0;
final int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
if(o1[i]==o2[i])
continue;
if(o1[i]>o2[i])
return 1;
return -1;
}
return compareInt(o1.length, o2.length);
}
};
public static final Comparator<double[]> DOUBLE_ARRAY_COMPARATOR = new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
if(o1==o2) return 0;
final int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
if(o1[i]==o2[i])
continue;
if(o1[i]>o2[i])
return 1;
return -1;
}
return compareInt(o1.length, o2.length);
}
};
/** Compares two arrays which contains comparable elements */
public static final Comparator<Object[]> COMPARABLE_ARRAY_COMPARATOR = new Comparator<Object[]>() {
@Override
public int compare(Object[] o1, Object[] o2) {
if(o1==o2) return 0;
final int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
int r = Fun.COMPARATOR.compare(o1[i],o2[i]);
if(r!=0)
return r;
}
return compareInt(o1.length, o2.length);
}
};
/** compares two arrays using given comparators*/
public static final class ArrayComparator implements Comparator<Object[]>{
protected final Comparator[] comparators;
public ArrayComparator(Comparator<?>[] comparators2) {
this.comparators = comparators2.clone();
for(int i=0;i<this.comparators.length;i++){
if(this.comparators[i]==null)
this.comparators[i] = Fun.COMPARATOR;
}
}
/** constructor used for deserialization*/
protected ArrayComparator(SerializerBase serializer, DataInput in, SerializerBase.FastArrayList<Object> objectStack) throws IOException {
objectStack.add(this);
this.comparators = (Comparator[]) serializer.deserialize(in, objectStack);
}
@Override
public int compare(Object[] o1, Object[] o2) {
if(o1==o2) return 0;
int len = Math.min(o1.length,o2.length);
for(int i=0;i<len;i++){
int r = comparators[i].compare(o1[i],o2[i]);
if(r!=0)
return r;
}
return compareInt(o1.length, o2.length);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArrayComparator that = (ArrayComparator) o;
return Arrays.equals(comparators, that.comparators);
}
@Override
public int hashCode() {
return Arrays.hashCode(comparators);
}
}
public static int compareInt(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
public static int compareLong(long x, long y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
/**
* TODO document tuples
*
* Find all Primary Keys associated with Secondary Key.
* This is useful companion to {@link Bind#mapInverse(org.mapdb.Bind.MapWithModificationListener, java.util.Set)}
* and {@link Bind#secondaryKey(org.mapdb.Bind.MapWithModificationListener, java.util.Set, org.mapdb.Fun.Function2)}
* It can by also used to find values from 'MultiMap'.
*
* @param set Set or 'MultiMap' to find values in
* @param keys key to look from
* @return all keys where primary value equals to {@code secondaryKey}
*/
public static Iterable<Object[]> filter(final NavigableSet<Object[]> set, final Object... keys) {
return new Iterable<Object[]>() {
@Override
public Iterator<Object[]> iterator() {
final Iterator<Object[]> iter = set.tailSet(keys).iterator();
if(!iter.hasNext())
return Fun.EMPTY_ITERATOR;
return new Iterator<Object[]>() {
Object[] next = moveToNext();
Object[] moveToNext() {
if(!iter.hasNext())
return null;
Object[] next = iter.next();
if(next==null)
return null;
//check all elements are equal
//TODO this does not work if byte[] etc is used in array. Document or fail!
//TODO add special check for Fun.ARRAY comparator and use its sub-comparators
for(int i=0;i<keys.length;i++){
if(!keys[i].equals(next[i]))
return null;
}
return next;
}
@Override
public boolean hasNext() {
return next!=null;
}
@Override
public Object[] next() {
Object[] ret = next;
if(ret == null)
throw new NoSuchElementException();
next = moveToNext();
return ret;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
/** decides if some action should be executed on an record*/
public interface RecordCondition<A>{
boolean run(final long recid, final A value, final Serializer<A> serializer);
}
/** record condition which always returns true*/
public static final RecordCondition RECORD_ALWAYS_TRUE = new RecordCondition() {
@Override
public boolean run(long recid, Object value, Serializer serializer) {
return true;
}
};
}