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;
  }
Пример #3
0
  /**
   * Returns a new descriptor eqiuvalent to this+D. Remember, composition is not commutative for all
   * attributes, and may not exist for some. To compose, remember to call
   * {@linkDescriptor#isComposable() isComposable} first and try both <code>A.compose (B)</code> and
   * <code>B.compose (A)</code>.
   *
   * @param D The Descriptor to compose with this Descriptor.
   * @param scope The attribute scope and mapping.
   * @return A new Descriptor that is equivalent to the composition of this and the argument D.
   * @throws BadDataException if the compose semantics are not correct.
   * @throws UncomposableException this instance of the descriptor cannot be composed. Check
   *     isComposable!
   */
  public Descriptor compose(Descriptor D, EvaluationParameters.ScopeRules scope)
      throws BadDataException, UncomposableException {
    DescAggregate temp = (DescAggregate) this.clone();
    // Unify Descriptions and Spans
    temp.span = temp.span.union(D.getFrameSpan());
    if (D.getClass().equals(DescSingle.class)) {
      if (idList.contains(D.getID()))
        throw new BadDataException("Attempting to compose the same descriptor multiple times");
      else temp.idList.add(D.getID());
    } else {
      // First, check to see if there are any dup ID numbers
      Iterator iterA = idList.iterator();
      Iterator iterB = ((TreeSet) D.getID()).iterator();
      Comparable A = (Comparable) iterA.next();
      Comparable B = (Comparable) iterB.next();

      /// difference will be negative iff A < B, and positive iff A > B.
      // I wish java had operator overloading. I really do.
      double difference = A.compareTo(B);
      while (iterA.hasNext() && iterB.hasNext() && (difference != 0)) {
        while ((difference < 0) && (iterA.hasNext() && iterB.hasNext())) {
          A = (Comparable) iterA.next();
          difference = A.compareTo(B);
        }
        while ((difference > 0) && (iterA.hasNext() && iterB.hasNext())) {
          B = (Comparable) iterB.next();
          difference = A.compareTo(B);
        }
      }
      if (difference == 0)
        // If there was a dup ID num, return 0
        throw new BadDataException("Attempting to compose the same descriptor multiple times");
      temp.idList.addAll(((DescAggregate) D).idList);
    }

    // Unify Attributes
    String errMsg = null;
    for (Iterator iter = scope.getInScopeAttributesFor(temp); iter.hasNext(); ) {
      String currAttrName = (String) iter.next();
      int i = temp.getAttributeIndex(currAttrName, scope.getMap());
      try {
        temp.attributes[i] =
            Attribute.compose(
                this.span,
                this.getAttribute(currAttrName, scope.getMap()),
                D.span,
                D.getAttribute(currAttrName, scope.getMap()));
      } catch (UncomposableException ux) {
        if (errMsg == null) {
          errMsg = ux.getMessage();
        } else {
          errMsg += "\n" + ux.getMessage();
        }
      }
    }
    if (errMsg != null) System.err.println(errMsg + "\n fix your .epf");
    return temp;
  }
 /**
  * Constructs a <code>SpinnerModel</code> that represents a closed sequence of numbers from <code>
  * minimum</code> to <code>maximum</code>. The <code>nextValue</code> and <code>previousValue
  * </code> methods compute elements of the sequence by adding or subtracting <code>stepSize</code>
  * respectively. All of the parameters must be mutually <code>Comparable</code>, <code>value
  * </code> and <code>stepSize</code> must be instances of <code>Integer</code> <code>Long</code>,
  * <code>Float</code>, or <code>Double</code>.
  *
  * <p>The <code>minimum</code> and <code>maximum</code> parameters can be <code>null</code> to
  * indicate that the range doesn't have an upper or lower bound. If <code>value</code> or <code>
  * stepSize</code> is <code>null</code>, or if both <code>minimum</code> and <code>maximum</code>
  * are specified and <code>mininum &gt; maximum</code> then an <code>IllegalArgumentException
  * </code> is thrown. Similarly if <code>(minimum &lt;= value &lt;= maximum</code>) is false, an
  * <code>IllegalArgumentException</code> is thrown.
  *
  * @param value the current (non <code>null</code>) value of the model
  * @param minimum the first number in the sequence or <code>null</code>
  * @param maximum the last number in the sequence or <code>null</code>
  * @param stepSize the difference between elements of the sequence
  * @throws IllegalArgumentException if stepSize or value is <code>null</code> or if the following
  *     expression is false: <code>minimum &lt;= value &lt;= maximum</code>
  */
 public SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize) {
   if ((value == null) || (stepSize == null)) {
     throw new IllegalArgumentException("value and stepSize must be non-null");
   }
   if (!(((minimum == null) || (minimum.compareTo(value) <= 0))
       && ((maximum == null) || (maximum.compareTo(value) >= 0)))) {
     throw new IllegalArgumentException("(minimum <= value <= maximum) is false");
   }
   this.value = value;
   this.minimum = minimum;
   this.maximum = maximum;
   this.stepSize = stepSize;
 }
  /**
   * Makes the Node node the rootNode of the SplayTree.
   *
   * @param node Node to be splayed
   */
  public void splay(Comparable comp) {
    Node left = head;
    Node right = head;
    Node top = rootNode;
    Node temp;

    head.setLeft(null);
    head.setRight(null);

    while (true) {
      if (comp.compareTo(top.getValue()) < 0) {
        if (top.getLeft() == null) // exit
        break;

        if (comp.compareTo(top.getLeft().getValue()) < 0) { // Rotate right
          temp = top.getLeft();
          top.setLeft(temp.getRight());
          temp.setRight(top);
          top = temp;

          if (top.getLeft() == null) // exit
          break;
        }
        right.setLeft(top); // Links the right
        right = top;
        top = top.getLeft();
      } else if (comp.compareTo(top.getValue()) > 0) {
        if (top.getRight() == null) // exit
        break;

        if (comp.compareTo(top.getRight().getValue()) > 0) { // Rotate left
          temp = top.getRight();
          top.setRight(temp.getLeft());
          temp.setLeft(top);
          top = temp;

          if (top.getRight() == null) // exit
          break;
        }
        left.setRight(top); // Links the left
        left = top;
        top = top.getRight();
      } else break; // exit
    }
    left.setRight(top.getLeft());
    right.setLeft(top.getRight());
    top.setLeft(head.getRight());
    top.setRight(head.getLeft());
    rootNode = top;
  }
  /**
   * Inserts a node into the SplayTree, then splays around that node.
   *
   * @param comp Comparable value of new node being added
   */
  public void insert(Comparable comp) {
    Node node = new Node(comp);
    if (rootNode == null && size == 0) {
      rootNode = node;
      return;
    }

    splay(comp);

    int temp = comp.compareTo(rootNode.getValue());
    if (temp == 0) // Double checks for duplicates
    return;

    if (temp < 0) {
      node.setLeft(rootNode.getLeft());
      node.setRight(rootNode);
      rootNode.setLeft(null);
    } else {
      node.setRight(rootNode.getRight());
      node.setLeft(rootNode);
      rootNode.setRight(null);
    }
    rootNode = node;
    size++;
  }
Пример #7
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);
 }
Пример #8
0
 public Object getPreviousValue() {
   Calendar cal = Calendar.getInstance();
   cal.setTime(value.getTime());
   cal.add(calendarField, -1);
   Date prev = cal.getTime();
   return ((start == null) || (start.compareTo(prev) <= 0)) ? prev : null;
 }
Пример #9
0
 public Object getNextValue() {
   Calendar cal = Calendar.getInstance();
   cal.setTime(value.getTime());
   cal.add(calendarField, 1);
   Date next = cal.getTime();
   return ((end == null) || (end.compareTo(next) >= 0)) ? next : null;
 }
Пример #10
0
 public boolean remove(Comparable info) {
   Node parent = null;
   Node current = root;
   while (current != null) {
     int cmp = info.compareTo(current.info);
     if (cmp < 0) {
       parent = current;
       current = current.left;
     } else if (cmp > 0) {
       parent = current;
       current = current.right;
     } else {
       break;
     }
   }
   if (current == null) return false;
   Node change = removeNode(current);
   if (parent == null) {
     root = change;
   } else if (parent.left == current) {
     parent.left = change;
   } else {
     parent.right = change;
   }
   return true;
 }
Пример #11
0
  private boolean compare_Comparable(int operation, Comparable value1, Object value2) {
    if (operation == SUBSTRING) {
      return false;
    }
    Constructor constructor;
    try {
      constructor = value1.getClass().getConstructor(constructorType);
    } catch (NoSuchMethodException e) {
      return false;
    }
    try {
      if (!constructor.isAccessible())
        AccessController.doPrivileged(new SetAccessibleAction(constructor));
      value2 = constructor.newInstance(new Object[] {((String) value2).trim()});
    } catch (IllegalAccessException e) {
      return false;
    } catch (InvocationTargetException e) {
      return false;
    } catch (InstantiationException e) {
      return false;
    }

    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;
        }
    }
    return false;
  }
  /**
   * Removes the node with the value comp.
   *
   * @param comp Comparable being removed
   */
  public void remove(Comparable comp) {
    splay(comp);

    if (comp.compareTo(rootNode.getValue()) != 0) // Node not found
    return;

    if (rootNode.getLeft() == null) rootNode = rootNode.getRight();
    else {
      Node node = rootNode.getRight();
      rootNode = rootNode.getLeft();
      splay(comp);
      rootNode.setRight(node);
    }

    size--;
  }
 public static void verifyElementOrder(
     Iterator<? extends Element> elements, String key, Order order, int expectedCount) {
   Comparable previous = null;
   int count = 0;
   while (elements.hasNext()) {
     Element element = elements.next();
     Comparable current = element.value(key);
     if (previous != null) {
       int cmp = previous.compareTo(current);
       assertTrue(
           previous + " <> " + current + " @ " + count, order == Order.ASC ? cmp <= 0 : cmp >= 0);
     }
     previous = current;
     count++;
   }
   assertEquals(expectedCount, count);
 }
 public Object aggregate(Class key, List<? extends CoreMap> in) {
   if (in == null) return null;
   Comparable max = null;
   for (CoreMap cm : in) {
     Object obj = cm.get(key);
     if (obj != null) {
       if (obj instanceof Comparable) {
         Comparable c = (Comparable) obj;
         if (max == null) {
           max = c;
         } else if (c.compareTo(max) > 0) {
           max = c;
         }
       } else {
         throw new RuntimeException(
             "Cannot get max of attribute " + key + ", object of type: " + obj.getClass());
       }
     }
   }
   return max;
 }
Пример #15
0
  /**
   * Method compareTo compares this Tuple to the given Tuple instance.
   *
   * @param other of type Tuple
   * @return int
   */
  public int compareTo(Tuple other) {
    if (other == null || other.elements == null) return 1;

    if (other.elements.size() != this.elements.size())
      return this.elements.size() - other.elements.size();

    for (int i = 0; i < this.elements.size(); i++) {
      Comparable lhs = this.elements.get(i);
      Comparable rhs = other.elements.get(i);

      if (lhs == null && rhs == null) continue;

      if (lhs == null && rhs != null) return -1;
      else if (lhs != null && rhs == null) return 1;

      int c = lhs.compareTo(rhs); // guaranteed to not be null
      if (c != 0) return c;
    }

    return 0;
  }
Пример #16
0
  /**
   * Merges the given peer info with the local info. A newer contribution of the same contributor
   * replaces the older one. Otherwise no elements are removed by this method, ie the cache and
   * commands are not truncated.
   */
  private synchronized void merge(Collective peer) {

    synchronized (cache) {
      Iterator i = peer.cache.values().iterator();
      while (i.hasNext()) {
        ContributionBox cb = (ContributionBox) i.next();
        ContributionBox x = (ContributionBox) cache.get(cb.contributor.name);
        if (x == null || x.timeStamp < cb.timeStamp) {
          cache.put(cb.contributor.name, cb);
        }
      }
    }
    synchronized (commands) {
      Iterator i = peer.commands.entrySet().iterator();
      while (i.hasNext()) {
        Map.Entry e = (Map.Entry) i.next();
        Comparable time = (Comparable) commands.get(e.getKey());
        if (time == null || time.compareTo(e.getValue()) < 0) {
          commands.put(e.getKey(), e.getValue());
        }
      }
    }
    cutToSize();
  }
Пример #17
0
 public boolean less(Comparable a, Comparable b) {
   return (a.compareTo(b) < 0);
 }