/** * Adds the supplied name and value binding to the environment * * @param name the name to add * @param value the associated binding */ public void add(String name, Object value) { if (map.containsKey(name)) { String message = OCLMessages.bind(OCLMessages.BindingExist_ERROR_, name, map.get(name)); throw new IllegalArgumentException(message); } map.put(name, value); }
/** * 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; } } }
/** * 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(); }