Example usage for java.lang Comparable getClass

List of usage examples for java.lang Comparable getClass

Introduction

In this page you can find the example usage for java.lang Comparable getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

/**
 * Similar to java.lang.Comparable.compareTo, but can handle 2 different
 * data types./*from w w w.  ja va  2s .c  om*/
 *
 * @param val1
 * @param val2
 *
 */
static int compare(Comparable val1, Comparable val2) {
    if (val1.getClass() == val2.getClass()) {
        //both the same type, no conversion is necessary.
        return val1.compareTo(val2);
    }

    //find most appropriate class
    Object[] objects = new Object[] { val1, val2 };
    Class bestClass = bestClass(objects);

    if (bestClass != val1.getClass()) {
        val1 = (Comparable) convert(val1, bestClass);
    }

    if (bestClass != val2.getClass()) {
        val2 = (Comparable) convert(val2, bestClass);
    }

    //now do the comparison
    return val1.compareTo(val2);
}

From source file:org.openspaces.grid.gsm.autoscaling.AutoScalingSlaUtils.java

@SuppressWarnings("unchecked")
public static int compare(Comparable<?> threshold, Object value) throws NumberFormatException {

    if (threshold.getClass().equals(value.getClass())) {
        return ((Comparable<Object>) threshold).compareTo(value);
    }//w  w w  .j a va  2  s  .c o  m

    return toDouble(threshold).compareTo(toDouble(value));
}

From source file:gaffer.function.simple.filter.IsLessThan.java

@Override
public boolean isValid(final Comparable input) {
    if (null == input || !controlValue.getClass().isAssignableFrom(input.getClass())) {
        return false;
    }/*  ww w  . j  ava 2s . c om*/

    final int compareVal = controlValue.compareTo(input);
    if (orEqualTo) {
        return compareVal >= 0;
    }

    return compareVal > 0;
}

From source file:gaffer.function.simple.filter.IsMoreThan.java

@Override
public boolean isValid(final Comparable input) {
    if (null == input || !controlValue.getClass().isAssignableFrom(input.getClass())) {
        return false;
    }//from  w  w w . j a  v  a 2s . c om

    final int compareVal = controlValue.compareTo(input);
    if (orEqualTo) {
        return compareVal <= 0;
    }

    return compareVal < 0;
}

From source file:uk.gov.gchq.koryphe.impl.predicate.IsLessThan.java

@Override
public boolean test(final Comparable input) {
    if (null == input || !controlValue.getClass().isAssignableFrom(input.getClass())) {
        return false;
    }/*from  ww  w  .ja va  2s . co m*/

    final int compareVal = controlValue.compareTo(input);
    if (orEqualTo) {
        return compareVal >= 0;
    }

    return compareVal > 0;
}

From source file:uk.gov.gchq.koryphe.impl.predicate.IsMoreThan.java

@Override
public boolean test(final Comparable input) {
    if (null == input || !controlValue.getClass().isAssignableFrom(input.getClass())) {
        return false;
    }//from w ww. jav  a2 s.  c  om

    final int compareVal = controlValue.compareTo(input);
    if (orEqualTo) {
        return compareVal <= 0;
    }

    return compareVal < 0;
}

From source file:com.haulmont.cuba.gui.components.validators.RangeValidator.java

protected void checkValueType(Comparable value) {
    if (!type.isAssignableFrom(value.getClass())) {
        throw new RuntimeException(String.format("Wrong type of value '%s'", value));
    }//  w w  w  . j ava2 s .  com
}

From source file:jetbrains.exodus.entitystore.iterate.PropertiesIterableWrapper.java

@SuppressWarnings({ "ConstantConditions", "ObjectAllocationInLoop" })
public PropertiesIterableWrapper(@Nullable final PersistentEntityStoreImpl store,
        @Nullable final PropertyValueIterator it, @NotNull final EntityIterableBase source) {
    super(store, source);
    index = new Persistent23Tree<>();
    mutableIndex = null;/*from  ww  w  .  ja v a 2s .com*/
    if (it == null) {
        entityTypeId = -1;
        return;
    }
    try {
        if (!it.hasNext()) {
            entityTypeId = -1;
            valueClass = null;
        } else {
            final Collection<IndexEntry> tempList = new ArrayList<>();
            EntityId id = it.nextId();
            entityTypeId = id.getTypeId();
            Comparable propValue = it.currentValue();
            valueClass = propValue.getClass();
            while (true) {
                tempList.add(new IndexEntry(propValue, id.getLocalId()));
                if (!it.hasNext()) {
                    break;
                }
                id = it.nextId();
                propValue = it.currentValue();
                if (!valueClass.equals(propValue.getClass())) {
                    throw new EntityStoreException("Unexpected property value class");
                }
            }
            final Persistent23Tree.MutableTree<IndexEntry> mutableTree = index.beginWrite();
            mutableTree.addAll(tempList, tempList.size());
            mutableTree.endWrite();
        }
    } finally {
        ((EntityIteratorBase) it).disposeIfShouldBe();
    }
}

From source file:com.aurel.track.fieldType.runtime.base.CustomSelectComparable.java

@Override
public int compareTo(Object o) {
    CustomSelectComparable compositeComparable = (CustomSelectComparable) o;
    Map<Integer, Comparable> paramComparableMap = compositeComparable.getComparableMap();
    if ((comparableMap == null) && (paramComparableMap == null)) {
        return 0;
    }/*from  w  w w .java  2 s.  c  om*/
    if (comparableMap == null) {
        return -1;
    }
    if (paramComparableMap == null) {
        return 1;
    }
    Iterator<Integer> itrComparableMap = comparableMap.keySet().iterator();
    while (itrComparableMap.hasNext()) {
        Integer key = itrComparableMap.next();
        Comparable value0 = comparableMap.get(key);
        Comparable value1 = paramComparableMap.get(key);
        if ((value0 == null) && (value1 == null)) {
            return 0;
        }
        if (value0 == null) {
            return -1;
        }
        if (value1 == null) {
            return 1;
        }
        try {
            int compareResult = value0.compareTo(value1);
            if (compareResult != 0) {
                //return now only if the part if different
                return compareResult;
            }
        } catch (Exception e) {
            LOGGER.warn("Sorting the values " + value0 + " of class " + value0.getClass().getName() + " and "
                    + value1 + " of class " + value1.getClass().getName() + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    return 0;
}

From source file:com.aurel.track.fieldType.runtime.base.CompositeComparable.java

@Override
public int compareTo(Object o) {
    CompositeComparable compositeComparable = (CompositeComparable) o;
    Map<Integer, Comparable> paramComparableMap = compositeComparable.getComparableMap();
    if ((comparableMap == null) && (paramComparableMap == null)) {
        return 0;
    }// w ww  .  jav a2  s.  c om
    if (comparableMap == null) {
        return -1;
    }
    if (paramComparableMap == null) {
        return 1;
    }
    for (int i = 0; i < numberOfParts; i++) {
        Integer parameterCode = new Integer(i + 1);
        Comparable value0 = comparableMap.get(parameterCode);
        Comparable value1 = paramComparableMap.get(parameterCode);
        if ((value0 == null) && (value1 == null)) {
            return 0;
        }
        if (value0 == null) {
            return -1;
        }
        if (value1 == null) {
            return 1;
        }
        try {
            int compareResult = value0.compareTo(value1);
            if (compareResult != 0) {
                //return now only if the part if different
                return compareResult;
            }
        } catch (Exception e) {
            LOGGER.warn("Sorting the values " + value0 + " of class " + value0.getClass().getName() + " and "
                    + value1 + " of class " + value1.getClass().getName() + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    return 0;
}