예제 #1
0
  // Assumes the elements of the collection are all Integer or
  // all Double.
  public static Object sum(Collection<?> self) {

    if (self.isEmpty()) {
      return null; // undefined
    }

    Iterator<?> it = self.iterator();
    Object object = it.next();

    // two cases: Integer and Double
    if (object instanceof Integer) {
      int currVal = 0;
      for (it = self.iterator(); it.hasNext(); ) {
        currVal += ((Integer) it.next()).intValue();
      }
      return new Integer(currVal);
    } else if (object instanceof Double) {
      double currVal = 0.0;
      for (it = self.iterator(); it.hasNext(); ) {
        currVal += ((Double) it.next()).doubleValue();
      }
      return new Double(currVal);
    } else {
      IllegalArgumentException error = new IllegalArgumentException(OCLMessages.SumOperator_ERROR_);
      OCLPlugin.throwing(CollectionUtil.class, "sum", error); // $NON-NLS-1$
      throw error;
    }
  }
예제 #2
0
 /**
  * Creates a new OCL <tt>Collection</tt> of the specified kind.
  *
  * @param kind the OCL collection kind
  * @param c the contents of the new collection
  * @return the new collection of the specified <code>kind</code>, containing the same elements as
  *     <code>c</code>
  */
 public static <E> Collection<E> createNewCollection(CollectionKind kind, Collection<E> c) {
   switch (kind) {
     case SET_LITERAL:
       return new HashSet<E>(c);
     case SEQUENCE_LITERAL:
       return new ArrayList<E>(c);
     case BAG_LITERAL:
       return new BagImpl<E>(c);
     case ORDERED_SET_LITERAL:
       return new LinkedHashSet<E>(c);
     default:
       {
         String message = OCLMessages.bind(OCLMessages.OCLCollectionKindNotImpl_ERROR_, kind);
         IllegalArgumentException error = new IllegalArgumentException(message);
         OCLPlugin.throwing(CollectionUtil.class, "createNewCollection", error); // $NON-NLS-1$
         throw error;
       }
   }
 }
예제 #3
0
 /**
  * Creates a default diagnostic for construction of an exception with just a message.
  *
  * @param message the message
  * @return a diagnostic
  */
 private Diagnostic createDiagnostic(String message) {
   return new BasicDiagnostic(
       Diagnostic.ERROR, OCLPlugin.getPluginId(), OCLStatusCodes.ERROR, message, null);
 }
  /**
   * Implements the inherited method by attempting to find an appropriate Java method in the actual
   * type of the <tt>source</tt> object and invoking it. On failure to find or invoke the method
   * (e.g., an exception), the <tt>OclInvalid</tt> result is returned.
   *
   * @return the result of the Java method invocation, or <tt>OclInvalid</tt> on failure of the
   *     method invocation
   */
  public Object callOperation(O operation, int opcode, Object source, Object[] args)
      throws IllegalArgumentException {

    if (getParent() != null) {
      return getParent().callOperation(operation, opcode, source, args);
    }

    Method method = getJavaMethodFor(operation, source);

    if (method != null) {
      try {
        // coerce any collection arguments to EList as necessary
        Class<?>[] parmTypes = method.getParameterTypes();
        for (int i = 0; i < parmTypes.length; i++) {
          if (EList.class.isAssignableFrom(parmTypes[i])) {
            if (args[i] == null) {
              args[i] = ECollections.EMPTY_ELIST;
            } else if (!(args[i] instanceof Collection<?>)) {
              EList<Object> list = new BasicEList.FastCompare<Object>(1);
              list.add(args[i]);
              args[i] = list;
            } else if (!(args[i] instanceof EList<?>)) {
              args[i] = new BasicEList.FastCompare<Object>((Collection<?>) args[i]);
            }
          }
        }

        return method.invoke(source, args);
      } catch (Exception e) {
        OCLPlugin.catching(getClass(), "callOperation", e); // $NON-NLS-1$
        OCLPlugin.log(
            Diagnostic.ERROR,
            OCLStatusCodes.IGNORED_EXCEPTION_WARNING,
            OCLMessages.bind(
                OCLMessages.ErrorMessage_ERROR_,
                "calloperation", //$NON-NLS-1$
                e.getLocalizedMessage()),
            e);
        return getInvalidResult();
      }
    }

    // maybe it's a comparison operation that is implemented implicitly
    // via the Comparable interface?
    switch (opcode) {
      case PredefinedType.LESS_THAN:
      case PredefinedType.GREATER_THAN:
      case PredefinedType.LESS_THAN_EQUAL:
      case PredefinedType.GREATER_THAN_EQUAL:
        if ((source instanceof Comparable<?>) && (args.length == 1)) {
          @SuppressWarnings("unchecked")
          Comparable<Object> comparable = (Comparable<Object>) source;
          Object other = args[0];

          switch (opcode) {
            case PredefinedType.LESS_THAN:
              return comparable.compareTo(other) < 0;
            case PredefinedType.GREATER_THAN:
              return comparable.compareTo(other) > 0;
            case PredefinedType.LESS_THAN_EQUAL:
              return comparable.compareTo(other) <= 0;
            case PredefinedType.GREATER_THAN_EQUAL:
              return comparable.compareTo(other) >= 0;
          }
        }
        break;
    }

    throw new IllegalArgumentException();
  }