Example #1
0
  private static int safeCompare(Object left, Object right) throws ValueCompareException {

    if (left == right) {
      return 0;
    }

    boolean leftNullish = isNullish(left);
    boolean rightNullish = isNullish(right);

    if (leftNullish && !rightNullish) {
      return -1;
    } else if (!leftNullish && rightNullish) {
      return 1;
    } else if (leftNullish && rightNullish) {
      return 0;
    } else if (left instanceof String && right instanceof String) {
      return ((String) left).compareTo((String) right);
    } else if (left instanceof Number && right instanceof Number) {
      return new BigDecimal(left.toString()).compareTo(new BigDecimal(right.toString()));
    } else if (left instanceof String && right instanceof Number) {
      return new BigDecimal(left.toString()).compareTo(new BigDecimal(right.toString()));
    } else if (left instanceof String && right instanceof Boolean) {
      Boolean e = Boolean.valueOf((String) left);
      Boolean a = (Boolean) right;
      return e.compareTo(a);
    } else if (left instanceof Boolean && right instanceof Boolean) {
      Boolean e = (Boolean) left;
      Boolean a = (Boolean) right;
      return e.compareTo(a);
    } else {
      logger.debug(
          "Can not compare a {} with a {}", left.getClass().getName(), right.getClass().getName());
      throw new ValueCompareException();
    }
  }
 @Override
 public int compare(Object aObj, Object bObj) {
   if (aObj instanceof Boolean && bObj instanceof Boolean) {
     Boolean a = (Boolean) aObj;
     Boolean b = (Boolean) bObj;
     return a.compareTo(b);
   }
   return super.compare(aObj, bObj);
 }
 @Override
 public int compare(Disk o1, Disk o2) {
   Boolean boot1 = o1.isBoot();
   Boolean boot2 = o2.isBoot();
   int bootResult = boot1.compareTo(boot2);
   if (bootResult == 0 && o1.isBoot()) {
     return Boolean.compare(o2.isDiskSnapshot(), o1.isDiskSnapshot());
   }
   return bootResult;
 }
Example #4
0
 public static int compare(Boolean o1, Object o2) {
   if (o2 == null) {
     return 1;
   } else if (o2 instanceof Boolean) {
     Boolean b2 = (Boolean) o2;
     return o1.compareTo(b2);
   } else {
     return -1;
   }
 }
Example #5
0
  /**
   * Returns <code>true</code> if data to save, <code>false</code> otherwise.
   *
   * @return See above.
   */
  boolean hasDataToSave() {
    String text = loginArea.getText();
    if (text == null || text.trim().length() == 0) return false;
    text = text.trim();
    ExperimenterData original = (ExperimenterData) model.getRefObject();
    if (!text.equals(original.getUserName())) return true;
    // if (selectedIndex != originalIndex) return true;
    if (details == null) return false;
    Entry entry;
    Iterator i = details.entrySet().iterator();
    String key;
    String value;
    JTextField field;
    String v;
    if (items.size() > 0) {
      while (i.hasNext()) {
        entry = (Entry) i.next();
        key = (String) entry.getKey();
        field = items.get(key);
        if (field != null) {
          v = field.getText();
          if (v != null) {
            v = v.trim();
            value = (String) entry.getValue();
            if (value != null && !v.equals(value)) return true;
          }
        }
      }
    }

    Boolean b = ownerBox.isSelected();
    if (b.compareTo(groupOwner) != 0) return true;
    if (adminBox.isVisible()) {
      b = adminBox.isSelected();
      if (b.compareTo(admin) != 0) return true;
    }
    if (activeBox.isVisible()) {
      b = activeBox.isSelected();
      if (b.compareTo(active) != 0) return true;
    }
    return false;
  }
 public static final boolean hasMatchingDominantSortAttribute(
     CommonFieldsBase odkLastEntity, CommonFieldsBase odkEntity, DataField dominantAttr) {
   boolean matchingDominantAttr = false;
   switch (dominantAttr.getDataType()) {
     case BINARY:
       throw new IllegalStateException("unexpected sort on binary field");
     case LONG_STRING:
       throw new IllegalStateException("unexpected sort on long text field");
     case URI:
     case STRING:
       {
         String a1 = odkLastEntity.getStringField(dominantAttr);
         String a2 = odkEntity.getStringField(dominantAttr);
         matchingDominantAttr =
             (a1 == null) ? (a2 == null) : ((a2 != null) && a1.compareTo(a2) == 0);
       }
       break;
     case INTEGER:
       {
         Long a1 = odkLastEntity.getLongField(dominantAttr);
         Long a2 = odkEntity.getLongField(dominantAttr);
         matchingDominantAttr =
             (a1 == null) ? (a2 == null) : ((a2 != null) && a1.compareTo(a2) == 0);
       }
       break;
     case DECIMAL:
       {
         BigDecimal a1 = odkLastEntity.getNumericField(dominantAttr);
         BigDecimal a2 = odkEntity.getNumericField(dominantAttr);
         matchingDominantAttr =
             (a1 == null) ? (a2 == null) : ((a2 != null) && a1.compareTo(a2) == 0);
       }
       break;
     case BOOLEAN:
       {
         Boolean a1 = odkLastEntity.getBooleanField(dominantAttr);
         Boolean a2 = odkEntity.getBooleanField(dominantAttr);
         matchingDominantAttr =
             (a1 == null) ? (a2 == null) : ((a2 != null) && a1.compareTo(a2) == 0);
       }
       break;
     case DATETIME:
       {
         Date a1 = odkLastEntity.getDateField(dominantAttr);
         Date a2 = odkEntity.getDateField(dominantAttr);
         matchingDominantAttr =
             (a1 == null) ? (a2 == null) : ((a2 != null) && a1.compareTo(a2) == 0);
       }
       break;
     default:
       throw new IllegalStateException("unexpected data type");
   }
   return matchingDominantAttr;
 }
    private int checkBoolean(Boolean s1, Boolean s2) {
      int i = 0;
      if (s1 != null && s2 != null) {
        i = s1.compareTo(s2);
      } else if (s2 != null) {
        i = -1;
      } else if (s1 != null) {
        i = 1;
      }

      return i;
    }
  /**
   * Compare two booleans.
   *
   * @param boolean1 First boolean
   * @param boolean2 Second boolean
   * @return 1 if boolean1 > boolean2, 0 if boolean1 == boolean2, -1 if boolean1 < boolean2
   */
  public int compare(Boolean boolean1, Boolean boolean2) {
    if (boolean1 == boolean2) {
      return 0;
    }

    if (boolean1 == null) {
      return -1;
    }

    if (boolean2 == null) {
      return 1;
    }

    return boolean1.compareTo(boolean2);
  }
 @Override
 protected int sort(ClientTAXCode obj1, ClientTAXCode obj2, int index) {
   switch (index) {
     case 1:
       return obj1.getName().compareTo(obj2.getName());
     case 2:
       String desc1 = obj1.getDescription() != null ? obj1.getDescription() : "";
       String desc2 = obj2.getDescription() != null ? obj2.getDescription() : "";
       return desc1.toLowerCase().compareTo(desc2.toLowerCase());
     case 3:
       Boolean taxable1 = obj1.isTaxable();
       Boolean taxable2 = obj2.isTaxable();
       return taxable1.compareTo(taxable2);
   }
   return 0;
 }
 private static int compareValues(final Object v1, final Object v2) {
   if (v1 instanceof String && v2 instanceof String) {
     final String s1 = (String) v1;
     final String s2 = (String) v2;
     return s1.compareToIgnoreCase(s2);
   } else if (v1 instanceof Number && v2 instanceof Number) {
     final Double n1 = ((Number) v1).doubleValue();
     final Double n2 = ((Number) v2).doubleValue();
     return n1.compareTo(n2);
   } else if (v1 instanceof Boolean && v2 instanceof Boolean) {
     final Boolean b1 = (Boolean) v1;
     final Boolean b2 = (Boolean) v2;
     return b1.compareTo(b2);
   } else {
     return v1.getClass().getName().compareTo(v2.getClass().getName());
   }
 }
  /**
   * compares the value provided by the user and the one specified by the {@code WhenConstraint}
   *
   * @param fieldValue the value found in the field specified by a {@code CaseConstraint}'s {@code
   *     propertyName}
   * @param whenValue the value specified by a {@code WhenConstraint}
   * @param dataType the data type of the field which caseConstraint's propertyName refers to
   * @param operator the relationship to check between the {@code fieldValue} and the {@code
   *     whenValue}
   * @param isCaseSensitive whether string comparison will be carried out in a case sensitive
   *     fashion
   * @param dateTimeService used to convert strings to dates
   * @return true if the value matches the constraint
   */
  public static boolean compareValues(
      Object fieldValue,
      Object whenValue,
      DataType dataType,
      String operator,
      boolean isCaseSensitive,
      DateTimeService dateTimeService) {

    boolean result = false;
    Integer compareResult = null;

    if (UifConstants.CaseConstraintOperators.HAS_VALUE.equalsIgnoreCase(operator)) {
      if (fieldValue == null) {
        return "false".equals(whenValue.toString().toLowerCase());
      }
      if (fieldValue instanceof String && ((String) fieldValue).isEmpty()) {
        return "false".equals(whenValue.toString().toLowerCase());
      }
      if (fieldValue instanceof Collection && ((Collection<?>) fieldValue).isEmpty()) {
        return "false".equals(whenValue.toString().toLowerCase());
      }
      return "true".equals(whenValue.toString().toLowerCase());
    }
    // Convert objects into appropriate data types
    if (null != dataType) {
      if (DataType.STRING.equals(dataType)) {
        String v1 = getString(fieldValue);
        String v2 = getString(whenValue);

        if (!isCaseSensitive) {
          v1 = v1.toUpperCase();
          v2 = v2.toUpperCase();
        }

        compareResult = v1.compareTo(v2);
      } else if (DataType.INTEGER.equals(dataType)) {
        Integer v1 = getInteger(fieldValue);
        Integer v2 = getInteger(whenValue);
        compareResult = v1.compareTo(v2);
      } else if (DataType.LONG.equals(dataType)) {
        Long v1 = getLong(fieldValue);
        Long v2 = getLong(whenValue);
        compareResult = v1.compareTo(v2);
      } else if (DataType.DOUBLE.equals(dataType)) {
        Double v1 = getDouble(fieldValue);
        Double v2 = getDouble(whenValue);
        compareResult = v1.compareTo(v2);
      } else if (DataType.FLOAT.equals(dataType)) {
        Float v1 = getFloat(fieldValue);
        Float v2 = getFloat(whenValue);
        compareResult = v1.compareTo(v2);
      } else if (DataType.BOOLEAN.equals(dataType)) {
        Boolean v1 = getBoolean(fieldValue);
        Boolean v2 = getBoolean(whenValue);
        compareResult = v1.compareTo(v2);
      } else if (DataType.DATE.equals(dataType)) {
        Date v1 = getDate(fieldValue, dateTimeService);
        Date v2 = getDate(whenValue, dateTimeService);
        compareResult = v1.compareTo(v2);
      }
    }

    if (null != compareResult) {
      if ((UifConstants.CaseConstraintOperators.EQUALS.equalsIgnoreCase(operator)
              || UifConstants.CaseConstraintOperators.GREATER_THAN_EQUAL.equalsIgnoreCase(operator)
              || UifConstants.CaseConstraintOperators.LESS_THAN_EQUAL.equalsIgnoreCase(operator))
          && 0 == compareResult) {
        result = true;
      }

      if ((UifConstants.CaseConstraintOperators.NOT_EQUAL.equalsIgnoreCase(operator)
              || UifConstants.CaseConstraintOperators.NOT_EQUALS.equalsIgnoreCase(operator)
              || UifConstants.CaseConstraintOperators.GREATER_THAN.equalsIgnoreCase(operator))
          && compareResult >= 1) {
        result = true;
      }

      if ((UifConstants.CaseConstraintOperators.NOT_EQUAL.equalsIgnoreCase(operator)
              || UifConstants.CaseConstraintOperators.NOT_EQUALS.equalsIgnoreCase(operator)
              || UifConstants.CaseConstraintOperators.LESS_THAN.equalsIgnoreCase(operator))
          && compareResult <= -1) {
        result = true;
      }
    }

    return result;
  }
Example #12
0
  /**
   * Returns the experimenter to save.
   *
   * @return See above.
   */
  Object getExperimenterToSave() {
    ExperimenterData original = (ExperimenterData) model.getRefObject();
    // Required fields first

    String v = loginArea.getText();
    if (v == null || v.trim().length() == 0) showRequiredField();
    original.setLastName(v);
    JTextField f = items.get(EditorUtil.EMAIL);
    v = f.getText();
    if (v == null || v.trim().length() == 0) v = ""; // showRequiredField();
    original.setEmail(v);
    f = items.get(EditorUtil.INSTITUTION);
    v = f.getText();
    if (v == null) v = "";
    original.setInstitution(v.trim());
    f = items.get(EditorUtil.LAST_NAME);
    v = f.getText();
    if (v == null) v = "";
    original.setLastName(v.trim());

    f = items.get(EditorUtil.FIRST_NAME);
    v = f.getText();
    if (v == null) v = "";
    original.setFirstName(v.trim());

    f = items.get(EditorUtil.MIDDLE_NAME);
    v = f.getText();
    if (v == null) v = "";
    original.setMiddleName(v.trim());

    // set the groups
    GroupData g = null;
    /*

     	if (selectedIndex != originalIndex) {
     		if (selectedIndex < groupData.length)
     			g = groupData[selectedIndex];
     		ExperimenterData user = (ExperimenterData) model.getRefObject();
     		List userGroups = user.getGroups();
     		List<GroupData> newGroups = new ArrayList<GroupData>();
     		if (g != null) newGroups.add(g);
     		Iterator i = userGroups.iterator();
     		long id = -1;
     		if (g != null) id = g.getId();
     		GroupData group;
     		while (i.hasNext()) {
    	group = (GroupData) i.next();
    	if (group.getId() != id)
    		newGroups.add(group);
    }
     		//Need to see what to do b/c no ExperimenterGroupMap
     		original.setGroups(newGroups);
     	}
     	*/
    String value = loginArea.getText().trim();
    UserCredentials uc = new UserCredentials(value, "");
    Boolean b = ownerBox.isSelected();
    // if (g == null) g = original.getDefaultGroup();
    boolean a = false;
    if (b.compareTo(groupOwner) != 0) {
      a = true;
      uc.setOwner(b);
      Object parent = model.getParentRootObject();
      if (parent instanceof GroupData) {
        Map<GroupData, Boolean> map = new HashMap<GroupData, Boolean>();
        map.put((GroupData) parent, b);
        uc.setGroupsOwner(map);
      }
    }
    if (adminBox.isVisible()) {
      b = adminBox.isSelected();
      if (b.compareTo(admin) != 0) {
        a = true;
        uc.setAdministrator(b);
      }
    }
    if (activeBox.isVisible()) {
      b = activeBox.isSelected();
      if (b.compareTo(active) != 0) {
        a = true;
        uc.setActive(b);
      }
    }
    if (!original.getUserName().equals(value)) a = true;
    // if admin
    if (MetadataViewerAgent.isAdministrator()) a = true;
    if (a) {
      Map<ExperimenterData, UserCredentials> m = new HashMap<ExperimenterData, UserCredentials>();
      m.put(original, uc);
      AdminObject object = new AdminObject(g, m, AdminObject.UPDATE_EXPERIMENTER);
      return object;
    }
    return original; // newOne;
  }
Example #13
0
  public static int compareBoolean(Boolean o1Boolean, Boolean o2Boolean) {
    if (o1Boolean == null) o1Boolean = Boolean.FALSE;
    if (o2Boolean == null) o2Boolean = Boolean.FALSE;

    return o1Boolean.compareTo(o2Boolean);
  }
Example #14
0
  /** Copied from org.openrdf.query.QueryResultUtil */
  private static boolean bindingSetsMatch(final BindingSet bs1, final BindingSet bs2) {

    if (bs1.size() != bs2.size()) {
      return false;
    }

    for (Binding binding1 : bs1) {
      Value value1 = binding1.getValue();
      Value value2 = bs2.getValue(binding1.getName());

      if ((value1 instanceof BNode) && (value2 instanceof BNode)) {
        // BNode mappedBNode = bNodeMapping.get(value1);
        //
        // if (mappedBNode != null) {
        // // bNode 'value1' was already mapped to some other bNode
        // if (!value2.equals(mappedBNode)) {
        // // 'value1' and 'value2' do not match
        // return false;
        // }
        // } else {
        // // 'value1' was not yet mapped, we need to check if 'value2'
        // // is a
        // // possible mapping candidate
        // if (bNodeMapping.containsValue(value2)) {
        // // 'value2' is already mapped to some other value.
        // return false;
        // }
        // }

        return value1.equals(value2);
      } else {
        // values are not (both) bNodes
        if ((value1 instanceof Literal) && (value2 instanceof Literal)) {
          // do literal value-based comparison for supported datatypes
          Literal leftLit = (Literal) value1;
          Literal rightLit = (Literal) value2;

          URI dt1 = leftLit.getDatatype();
          URI dt2 = rightLit.getDatatype();

          if ((dt1 != null)
              && (dt2 != null)
              && dt1.equals(dt2)
              && XMLDatatypeUtil.isValidValue(leftLit.getLabel(), dt1)
              && XMLDatatypeUtil.isValidValue(rightLit.getLabel(), dt2)) {
            Integer compareResult = null;
            if (dt1.equals(XMLSchema.DOUBLE)) {
              compareResult = Double.compare(leftLit.doubleValue(), rightLit.doubleValue());
            } else if (dt1.equals(XMLSchema.FLOAT)) {
              compareResult = Float.compare(leftLit.floatValue(), rightLit.floatValue());
            } else if (dt1.equals(XMLSchema.DECIMAL)) {
              compareResult = leftLit.decimalValue().compareTo(rightLit.decimalValue());
            } else if (XMLDatatypeUtil.isIntegerDatatype(dt1)) {
              compareResult = leftLit.integerValue().compareTo(rightLit.integerValue());
            } else if (dt1.equals(XMLSchema.BOOLEAN)) {
              Boolean leftBool = Boolean.valueOf(leftLit.booleanValue());
              Boolean rightBool = Boolean.valueOf(rightLit.booleanValue());
              compareResult = leftBool.compareTo(rightBool);
            } else if (XMLDatatypeUtil.isCalendarDatatype(dt1)) {
              XMLGregorianCalendar left = leftLit.calendarValue();
              XMLGregorianCalendar right = rightLit.calendarValue();

              compareResult = left.compare(right);
            }

            if (compareResult != null) {
              if (compareResult.intValue() != 0) {
                return false;
              }
            } else if (!value1.equals(value2)) {
              return false;
            }
          } else if (!value1.equals(value2)) {
            return false;
          }
        } else if (!value1.equals(value2)) {
          return false;
        }
      }
    }

    return true;
  }
 public int compare(Presentation p1, Presentation p2) {
   final Boolean b1 = p1.getIsDefault();
   final Boolean b2 = p2.getIsDefault();
   return b1.compareTo(b2);
 }