
org.mapdb.Fun Maven / Gradle / Ivy
/*
* 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 comparator = getComparator();}
* @return comparator
*/
public static Comparator 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 comparator = getReversedComparator();}
* @return comparator
*/
public static Comparator reverseComparator(){
return Fun.REVERSE_COMPARATOR;
}
@SuppressWarnings("rawtypes")
public static final Comparator COMPARATOR = new Comparator() {
@Override
public int compare(Comparable o1, Comparable o2) {
return o1.compareTo(o2);
}
};
@SuppressWarnings("rawtypes")
public static final Comparator REVERSE_COMPARATOR = new Comparator() {
@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 Iterator 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;
}
public static long roundDown(long number, long roundDownToMultipleOf) {
return number - number % roundDownToMultipleOf;
}
/** 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();
}
public static boolean arrayContains(long[] longs, long val) {
for(long val2:longs){
if(val==val2)
return true;
}
return false;
}
static public final class Pair implements Comparable>, 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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy