Пример #1
0
  private Node add(Node node, Comparable data) {
    if (node == null) {
      return new Node(data);
    }
    if (data.compareTo(node.data) < 0) {
      node.left = add(node.left, data);
    } else {
      node.right = add(node.right, data);
    }

    node.setHeight(Math.max(getHeight(node.left), getHeight(node.right)) + 1);

    int balance = balanceFactor(node);

    // Left Left Case
    if (balance > 1 && data.compareTo(node.left.data) < 0) return rightRotate(node);

    // Right right Case
    if (balance < -1 && data.compareTo(node.right.data) > 0) return leftRotate(node);

    // Left Right Case
    if (balance > 1 && data.compareTo(node.left.data) > 0) {
      node.left = leftRotate(node.left);
      return rightRotate(node);
    }

    // Right Left Case
    if (balance < -1 && data.compareTo(node.right.data) < 0) {
      node.right = rightRotate(node.right);
      return leftRotate(node);
    }
    return node;
  }
Пример #2
0
  /**
   * Method getString returns the element at the given position i as a String.
   *
   * @param pos of type int
   * @return String
   */
  public String getString(int pos) {
    Comparable value = get(pos);

    if (value == null) return null;

    return value.toString();
  }
Пример #3
0
 /**
  * Tests if this object is equal to another
  *
  * @param a_obj the other object
  * @return true: this object is equal to the other one
  * @author Klaus Meffert
  * @since 2.3
  */
 public boolean equals(final Object a_obj) {
   if (a_obj == null) {
     return false;
   }
   if (a_obj == this) {
     return true;
   }
   if (!(a_obj instanceof KeyedValues)) {
     return false;
   }
   final KeyedValues kvs = (KeyedValues) a_obj;
   final int count = size();
   if (count != kvs.size()) {
     return false;
   }
   for (int i = 0; i < count; i++) {
     final Comparable k1 = getKey(i);
     final Comparable k2 = kvs.getKey(i);
     if (!k1.equals(k2)) {
       return false;
     }
     final Number v1 = getValue(i);
     final Number v2 = kvs.getValue(i);
     if (v1 == null) {
       if (v2 != null) {
         return false;
       }
     } else {
       if (!v1.equals(v2)) {
         return false;
       }
     }
   }
   return true;
 }
 /*     */ public int compareTo(NodeSortRecord other) /*     */ {
   /* 211 */ int[] sortOrder = this._settings.getSortOrders();
   /* 212 */ int levels = this._settings.getSortOrders().length;
   /* 213 */ int[] compareTypes = this._settings.getTypes();
   /*     */
   /* 215 */ for (int level = 0; level < levels; level++)
   /*     */ {
     /*     */ int cmp;
     /*     */ int cmp;
     /* 217 */ if (compareTypes[level] == 1) {
       /* 218 */ Double our = numericValue(level);
       /* 219 */ Double their = other.numericValue(level);
       /* 220 */ cmp = our.compareTo(their);
       /*     */ }
     /*     */ else {
       /* 223 */ Comparable our = stringValue(level);
       /* 224 */ Comparable their = other.stringValue(level);
       /* 225 */ cmp = our.compareTo(their);
       /*     */ }
     /*     */
     /* 229 */ if (cmp != 0) {
       /* 230 */ return sortOrder[level] == 1 ? 0 - cmp : cmp;
       /*     */ }
     /*     */ }
   /*     */
   /* 234 */ return this._node - other._node;
   /*     */ }
Пример #5
0
  /**
   * Method getDouble returns the element at the given position i as a double. Zero if null.
   *
   * @param pos of type int
   * @return double
   */
  public double getDouble(int pos) {
    Comparable value = get(pos);

    if (value instanceof Number) return ((Number) value).doubleValue();
    else if (value == null) return 0;
    else return Double.parseDouble(value.toString());
  }
Пример #6
0
 protected final int compare(int i, int j) {
   curMetrics.compareCnt++;
   Comparable obj1 = (Comparable) values[i];
   Comparable obj2 = (Comparable) values[j];
   if (obj1.compareTo(obj2) == 0) return 0;
   else return (obj1.compareTo(obj2) < 0 ? -1 : 1);
 }
Пример #7
0
  /**
   * See {@link LessOrEqualCondition} description.
   *
   * @throws ScriptException if not both left and right values implement {@link Comparable}
   *     interface
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  public boolean isTrue() throws ScriptException {
    Comparable leftValue = toComparable(getLeftValue());
    Comparable rightValue = toComparable(getRightValue());

    return leftValue.compareTo(rightValue) <= 0;
  }
Пример #8
0
  public int compare(Object o1, Object o2) {
    int ret = 0;
    IRecordInternal record1 = null;
    IRecordInternal record2 = null;
    if (foundSet != null) {
      record1 = foundSet.getRecord(((Integer) o1).intValue());
      record2 = foundSet.getRecord(((Integer) o2).intValue());
    } else {
      record1 = (IRecordInternal) o1;
      record2 = (IRecordInternal) o2;
    }

    for (int i = 0; i < sortColumns.size(); i++) {
      SortColumn sc = (SortColumn) sortColumns.get(i);
      String dataProviderId = sc.getDataProviderID();
      int ascending = (sc.getSortOrder() == SortColumn.ASCENDING ? 1 : -1);

      Comparable comp1 = (Comparable) record1.getValue(dataProviderId);
      Comparable comp2 = (Comparable) record2.getValue(dataProviderId);
      if (comp1 == null && comp2 == null) continue;
      if (comp1 == null) return 1 * ascending;
      if (comp2 == null) return -1 * ascending;

      if (comp1 instanceof String) {
        ret = ((String) comp1).compareToIgnoreCase((String) comp2);
      } else if (comp1 instanceof Number) {
        ret = Utils.compare(((Number) comp1).doubleValue(), ((Number) comp2).doubleValue());
      } else {
        ret = comp1.compareTo(comp2);
      }
      if (ret != 0) return (ret * ascending);
    }
    return ret;
  }
Пример #9
0
 public static java.util.Date addMonths(Comparable<?> param1, Comparable<?> param2, Calendar cal)
     throws ParseException {
   if (param1 == null && param2 == null) {
     return null;
   }
   try {
     int months = getInteger(param2);
     if (param1 instanceof Timestamp) {
       Timestamp d = (Timestamp) param1;
       cal.setTimeInMillis(d.getTime());
       cal.add(Calendar.MONTH, months);
       return new Timestamp(cal.getTimeInMillis());
     } else if (param1 instanceof java.sql.Date) {
       java.util.Date d = (java.util.Date) param1;
       cal.setTimeInMillis(d.getTime());
       cal.add(Calendar.MONTH, months);
       return new java.util.Date(cal.getTimeInMillis());
     } else {
       throw new ParseException();
     }
   } catch (ParseException e) {
     throw new ParseException(
         WRONG_TYPE + "  month_between(" + param1.getClass() + "," + param2.getClass() + ")");
   }
 }
Пример #10
0
  private static boolean evaluateAlert(Alert alert, Measure measure, Metric.Level alertLevel) {
    String valueToEval;
    if (alertLevel.equals(Metric.Level.ERROR)) {
      valueToEval = alert.getValueError();

    } else if (alertLevel.equals(Metric.Level.WARN)) {
      valueToEval = alert.getValueWarning();
    } else {
      throw new IllegalStateException(alertLevel.toString());
    }
    if (StringUtils.isEmpty(valueToEval)) {
      return false;
    }

    Comparable criteriaValue = getValueForComparison(alert.getMetric(), valueToEval);
    Comparable metricValue = getMeasureValue(alert.getMetric(), measure);

    int comparison = metricValue.compareTo(criteriaValue);
    if (alert.isNotEqualsOperator() && comparison == 0
        || alert.isGreaterOperator() && comparison != 1
        || alert.isSmallerOperator() && comparison != -1
        || alert.isEqualsOperator() && comparison != 0) {
      return false;
    }
    return true;
  }
Пример #11
0
  /**
   * Method getShort returns the element at the given position i as an short. Zero if null.
   *
   * @param pos of type int
   * @return long
   */
  public short getShort(int pos) {
    Comparable value = get(pos);

    if (value instanceof Number) return ((Number) value).shortValue();
    else if (value == null) return 0;
    else return Short.parseShort(value.toString());
  }
Пример #12
0
 public static Comparable<?> remainder(Comparable<?> param1, Comparable<?> param2)
     throws ParseException {
   if (param1 == null || param2 == null) {
     return null;
   }
   if (param1 instanceof String) {
     param1 = parse((String) param1);
   }
   if (param2 instanceof String) {
     param2 = parse((String) param2);
   }
   if (param1 instanceof Number && param2 instanceof Number) {
     // BigInteger type is not supported
     if (param1 instanceof BigDecimal || param2 instanceof BigDecimal) {
       BigDecimal b1 = getBigDecimal((Number) param1);
       BigDecimal b2 = getBigDecimal((Number) param2);
       return b1.remainder(b2);
     }
     if (param1 instanceof Double
         || param2 instanceof Double
         || param1 instanceof Float
         || param2 instanceof Float) {
       return ((Number) param1).doubleValue() % ((Number) param2).doubleValue();
     } else { // Long, Integer, Short, Byte
       long l1 = ((Number) param1).longValue();
       long l2 = ((Number) param2).longValue();
       return l1 % l2;
     }
   } else {
     throw new ParseException(
         WRONG_TYPE + "  (" + param1.getClass() + "%" + param2.getClass() + ")");
   }
 }
  public Object evaluateEnumMethod(
      EventBean[] eventsLambda,
      Collection target,
      boolean isNewData,
      ExprEvaluatorContext context) {
    Comparable minKey = null;

    Collection<EventBean> beans = (Collection<EventBean>) target;
    for (EventBean next : beans) {
      eventsLambda[streamNumLambda] = next;

      Object comparable = innerExpression.evaluate(eventsLambda, isNewData, context);
      if (comparable == null) {
        continue;
      }

      if (minKey == null) {
        minKey = (Comparable) comparable;
      } else {
        if (max) {
          if (minKey.compareTo(comparable) < 0) {
            minKey = (Comparable) comparable;
          }
        } else {
          if (minKey.compareTo(comparable) > 0) {
            minKey = (Comparable) comparable;
          }
        }
      }
    }

    return minKey;
  }
Пример #14
0
  /**
   * 根据传入的参数决定使用哪类枚举器
   *
   * @param comp
   * @param needMergeValueInCloseInterval
   * @return
   */
  private CloseIntervalFieldsEnumeratorHandler getCloseIntervalEnumeratorHandlerByComparative(
      Comparative comp, boolean needMergeValueInCloseInterval) {
    if (!needMergeValueInCloseInterval) {
      return enumeratorMap.get(DEFAULT_ENUMERATOR);
    }
    if (comp == null) {
      throw new IllegalArgumentException("comp is null");
    }

    Comparable value = comp.getValue();
    if (value instanceof ComparativeBaseList) {
      ComparativeBaseList comparativeBaseList = (ComparativeBaseList) value;
      for (Comparative comparative : comparativeBaseList.getList()) {
        return getCloseIntervalEnumeratorHandlerByComparative(
            comparative, needMergeValueInCloseInterval);
      }

      throw new NotSupportException(); // 不可能到这一步
    } else if (value instanceof Comparative) {
      return getCloseIntervalEnumeratorHandlerByComparative(comp, needMergeValueInCloseInterval);
    } else {
      // 表明是一个comparative对象
      CloseIntervalFieldsEnumeratorHandler enumeratorHandler =
          enumeratorMap.get(value.getClass().getName());
      if (enumeratorHandler != null) {
        return enumeratorHandler;
      } else {
        return enumeratorMap.get(DEFAULT_ENUMERATOR);
      }
    }
  }
Пример #15
0
 @SuppressWarnings({"unchecked", "rawtypes", "unused"})
 private TreeNode add(Comparable val, TreeNode tree) {
   if (tree == null) tree = new TreeNode(val, null, null);
   else if (val.compareTo(tree.getLeft()) < 0) tree.setLeft((add(val, tree.getLeft())));
   else if (val.compareTo(tree.getValue()) > 0) tree.setRight(add(val, tree.getRight()));
   return tree;
 }
Пример #16
0
 /**
  * 根据排序类型比较大小.
  *
  * @param thisValue 待比较的值
  * @param otherValue 待比较的值
  * @param orderByType 排序类型
  * @return 负数,零和正数分别表示小于,等于和大于
  */
 @SuppressWarnings({"rawtypes", "unchecked"})
 public static int compareTo(
     final Comparable thisValue, final Comparable otherValue, final OrderByType orderByType) {
   return OrderByType.ASC == orderByType
       ? thisValue.compareTo(otherValue)
       : -thisValue.compareTo(otherValue);
 }
Пример #17
0
  /**
   * Method getInteger returns the element at the given position i as an int. Zero if null.
   *
   * @param pos of type int
   * @return int
   */
  public int getInteger(int pos) {
    Comparable value = get(pos);

    if (value instanceof Number) return ((Number) value).intValue();
    else if (value == null) return 0;
    else return Integer.parseInt(value.toString());
  }
Пример #18
0
  /**
   * Internal quicksort method that makes recursive calls. Uses median-of-three partitioning and a
   * cutoff of 10.
   *
   * @param a an array of Comparable items.
   * @param low the left-most index of the subarray.
   * @param high the right-most index of the subarray.
   */
  private static void quicksort(Comparable[] a, int low, int high) {
    if (low + QuickSortCUTOFF > high) insertionSort(a, low, high);
    else {
      // Sort low, middle, high
      int middle = (low + high) / 2;
      if (a[middle].compareTo(a[low]) < 0) swapReferences(a, low, middle);
      if (a[high].compareTo(a[low]) < 0) swapReferences(a, low, high);
      if (a[high].compareTo(a[middle]) < 0) swapReferences(a, middle, high);

      // Place pivot at position high - 1
      swapReferences(a, middle, high - 1);
      Comparable pivot = a[high - 1];

      // Begin partitioning
      int i, j;
      for (i = low, j = high - 1; ; ) {
        while (a[++i].compareTo(pivot) < 0) ;
        while (pivot.compareTo(a[--j]) < 0) ;
        if (i >= j) break;
        swapReferences(a, i, j);
      }

      // Restore pivot
      swapReferences(a, i, high - 1);

      quicksort(a, low, i - 1); // Sort small elements
      quicksort(a, i + 1, high); // Sort large elements
    }
  }
Пример #19
0
  /**
   * Method getLong returns the element at the given position i as an long. Zero if null.
   *
   * @param pos of type int
   * @return long
   */
  public long getLong(int pos) {
    Comparable value = get(pos);

    if (value instanceof Number) return ((Number) value).longValue();
    else if (value == null) return 0;
    else return Long.parseLong(value.toString());
  }
Пример #20
0
 /**
  * Attempts to compare to objects: if the first is {@link Comparable} then call it's {@link
  * Comparable#compareTo(Object)} method, call {@link Comparable#compareTo(Object)} on it's {@link
  * String#compareTo(java.lang.String)} with tthe other object's <code>toString()</code> version.
  *
  * @param x the object to compare with y
  * @param y the object to be campared
  * @return return -1 if x&lt;y; 0 if x=y; +1 if x&gt;y
  */
 @SuppressWarnings("unchecked")
 private int compare(Object x, Object y) {
   if (x instanceof Comparable) {
     Comparable<Object> x1 = (Comparable) x;
     return x1.compareTo(y);
   } else return x.toString().compareTo(y.toString());
 }
Пример #21
0
  /**
   * Method getBoolean returns the element at the given position as a boolean. If the value is (case
   * ignored) the string 'true', a {@code true} value will be returned. {@code false} if null.
   *
   * @param pos of type int
   * @return boolean
   */
  public boolean getBoolean(int pos) {
    Comparable value = get(pos);

    if (value instanceof Boolean) return ((Boolean) value).booleanValue();
    else if (value == null) return false;
    else return Boolean.parseBoolean(value.toString());
  }
  private Number incrValue(int dir) {
    Number newValue;
    if ((value instanceof Float) || (value instanceof Double)) {
      double v = value.doubleValue() + (stepSize.doubleValue() * (double) dir);
      if (value instanceof Double) {
        newValue = new Double(v);
      } else {
        newValue = new Float(v);
      }
    } else {
      long v = value.longValue() + (stepSize.longValue() * (long) dir);

      if (value instanceof Long) {
        newValue = new Long(v);
      } else if (value instanceof Integer) {
        newValue = new Integer((int) v);
      } else if (value instanceof Short) {
        newValue = new Short((short) v);
      } else {
        newValue = new Byte((byte) v);
      }
    }

    if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
      return null;
    }
    if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
      return null;
    } else {
      return newValue;
    }
  }
  /**
   * Returns true if <code>value</code> is between the min/max.
   *
   * @param wantsCCE If false, and a ClassCastException is thrown in comparing the values, the
   *     exception is consumed and false is returned.
   */
  boolean isValidValue(Object value, boolean wantsCCE) {
    Comparable min = getMinimum();

    try {
      if (min != null && min.compareTo(value) > 0) {
        return false;
      }
    } catch (ClassCastException cce) {
      if (wantsCCE) {
        throw cce;
      }
      return false;
    }

    Comparable max = getMaximum();
    try {
      if (max != null && max.compareTo(value) < 0) {
        return false;
      }
    } catch (ClassCastException cce) {
      if (wantsCCE) {
        throw cce;
      }
      return false;
    }
    return true;
  }
  @Override
  public int compare(ObjectListDataEntry arg0, ObjectListDataEntry arg1) {
    Object val1 = arg0.getColumnValue(m_Attribute);
    Object val2 = arg1.getColumnValue(m_Attribute);
    if (val1 == null && val2 == null) {
      return 0;
    }
    if (val1 == null) {
      return -1 * (m_Ascending ? 1 : -1);
    }
    if (val2 == null) {
      return 1 * (m_Ascending ? 1 : -1);
    }
    if (val1 instanceof String && val2 instanceof String) {
      String sVal1 = (String) val1;
      String sVal2 = (String) val2;
      return sVal1.toLowerCase().compareTo(sVal2.toLowerCase()) * (m_Ascending ? 1 : -1);
    }
    if (val1 instanceof Comparable<?> && val2 instanceof Comparable<?>) {
      @SuppressWarnings("unchecked")
      Comparable<Object> valO1 = (Comparable<Object>) val1;
      @SuppressWarnings("unchecked")
      Comparable<Object> valO2 = (Comparable<Object>) val2;

      return valO1.compareTo(valO2) * (m_Ascending ? 1 : -1);
    }
    return 0;
  }
Пример #25
0
 private boolean compare_Comparable(int operation, Comparable<Object> value1, Object value2) {
   if (operation == SUBSTRING) {
     return false;
   }
   value2 = valueOf(value1.getClass(), (String) value2);
   if (value2 == null) {
     return false;
   }
   try {
     switch (operation) {
       case APPROX:
       case EQUAL:
         {
           return value1.compareTo(value2) == 0;
         }
       case GREATER:
         {
           return value1.compareTo(value2) >= 0;
         }
       case LESS:
         {
           return value1.compareTo(value2) <= 0;
         }
     }
   } catch (Exception e) {
     // if the compareTo method throws an exception; return false
     return false;
   }
   return false;
 }
  @Override
  public int compare(ServiceReference<T> serviceReference1, ServiceReference<T> serviceReference2) {

    if (serviceReference1 == null) {
      if (serviceReference2 == null) {
        return 0;
      } else {
        return 1;
      }
    } else if (serviceReference2 == null) {
      return -1;
    }

    Object propertyValue1 = serviceReference1.getProperty(_propertyKey);
    Object propertyValue2 = serviceReference2.getProperty(_propertyKey);

    if (propertyValue1 == null) {
      if (propertyValue2 == null) {
        return 0;
      } else {
        return 1;
      }
    } else if (propertyValue2 == null) {
      return -1;
    }

    if (!(propertyValue2 instanceof Comparable)) {
      return serviceReference2.compareTo(serviceReference1);
    }

    Comparable<Object> propertyValueComparable2 = (Comparable<Object>) propertyValue2;

    return propertyValueComparable2.compareTo(propertyValue1);
  }
Пример #27
0
 public int compare(Object p1, Object p2) {
   Row r1 = (Row) p1;
   Row r2 = (Row) p2;
   Comparable val1 = (Comparable) r1.get(mFieldName);
   Comparable val2 = (Comparable) r2.get(mFieldName);
   return val1.compareTo(val2);
 }
Пример #28
0
  /**
   * Method getFloat returns the element at the given position i as a float. Zero if null.
   *
   * @param pos of type int
   * @return float
   */
  public float getFloat(int pos) {
    Comparable value = get(pos);

    if (value instanceof Number) return ((Number) value).floatValue();
    else if (value == null) return 0;
    else return Float.parseFloat(value.toString());
  }
  /**
   * @see java.lang.Comparable#compareTo(java.lang.Object) A negative integer, zero, or a positive
   *     integer as this object is less than, equal to, or greater than the specified object.
   */
  public int compareTo(Object arg0) {
    GenomicIdentifierSetOntWithAllDSIds dest = (GenomicIdentifierSetOntWithAllDSIds) arg0;
    List srcGenomicIdentifierValues =
        SummaryReflectionUtil.getGenomicIdentifierSetValues(gene, messengerRNA, protein);
    List destGenomicIdentifierValues =
        SummaryReflectionUtil.getGenomicIdentifierSetValues(
            dest.gene, dest.messengerRNA, dest.protein);

    Comparable currentSrcGenomicIdentifierValues = null;

    for (int i = 0; i < srcGenomicIdentifierValues.size(); i++) {
      if (srcGenomicIdentifierValues.get(i).equals("")) {
        if (destGenomicIdentifierValues.get(i).equals("")) {
          continue;
        } else {
          return -1;
        }
      } else if (destGenomicIdentifierValues.get(i).equals("")) {
        return 1;
      }

      if (!srcGenomicIdentifierValues.get(i).equals(destGenomicIdentifierValues.get(i))) {
        currentSrcGenomicIdentifierValues = (Comparable) srcGenomicIdentifierValues.get(i);
        return currentSrcGenomicIdentifierValues.compareTo(destGenomicIdentifierValues.get(i));
      }
    }

    if (this.ontId < dest.ontId) {
      return -1;
    } else if (this.ontId > dest.ontId) {
      return 1;
    }
    return 0;
  }
Пример #30
0
 private Node find(Comparable x, Node t) {
   while (t != null) {
     if (x.compareTo(t.data) < 0) t = t.left;
     else if (x.compareTo(t.data) > 0) t = t.right;
     else return t;
   }
   return null;
 }