All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.ocl.parser.OCLFactoryWithHistory Maven / Gradle / Ivy

/**
 * 
 *
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 *
 * 
 *
 * $Id: OCLFactoryWithHistory.java,v 1.4 2010/12/15 17:33:43 ewillink Exp $
 */
package org.eclipse.ocl.parser;

import java.util.List;
import java.util.Set;

import org.eclipse.ocl.expressions.AssociationClassCallExp;
import org.eclipse.ocl.expressions.BooleanLiteralExp;
import org.eclipse.ocl.expressions.CollectionItem;
import org.eclipse.ocl.expressions.CollectionKind;
import org.eclipse.ocl.expressions.CollectionLiteralExp;
import org.eclipse.ocl.expressions.CollectionRange;
import org.eclipse.ocl.expressions.EnumLiteralExp;
import org.eclipse.ocl.expressions.IfExp;
import org.eclipse.ocl.expressions.IntegerLiteralExp;
import org.eclipse.ocl.expressions.InvalidLiteralExp;
import org.eclipse.ocl.expressions.IterateExp;
import org.eclipse.ocl.expressions.IteratorExp;
import org.eclipse.ocl.expressions.LetExp;
import org.eclipse.ocl.expressions.MessageExp;
import org.eclipse.ocl.expressions.NullLiteralExp;
import org.eclipse.ocl.expressions.OperationCallExp;
import org.eclipse.ocl.expressions.PropertyCallExp;
import org.eclipse.ocl.expressions.RealLiteralExp;
import org.eclipse.ocl.expressions.StateExp;
import org.eclipse.ocl.expressions.StringLiteralExp;
import org.eclipse.ocl.expressions.TupleLiteralExp;
import org.eclipse.ocl.expressions.TupleLiteralPart;
import org.eclipse.ocl.expressions.TypeExp;
import org.eclipse.ocl.expressions.UnlimitedNaturalLiteralExp;
import org.eclipse.ocl.expressions.UnspecifiedValueExp;
import org.eclipse.ocl.expressions.Variable;
import org.eclipse.ocl.expressions.VariableExp;
import org.eclipse.ocl.types.BagType;
import org.eclipse.ocl.types.CollectionType;
import org.eclipse.ocl.types.MessageType;
import org.eclipse.ocl.types.OrderedSetType;
import org.eclipse.ocl.types.SequenceType;
import org.eclipse.ocl.types.SetType;
import org.eclipse.ocl.types.TupleType;
import org.eclipse.ocl.types.TypeType;
import org.eclipse.ocl.util.ObjectUtil;
import org.eclipse.ocl.utilities.OCLFactory;
import org.eclipse.ocl.utilities.TypedElement;

/**
 * A wrapper for {@link OCLFactory}s that records a history of the objects
 * created by it.  This is useful in case any objects that were created ended
 * up not being used, because an error occurred in parsing, and thus need to
 * be {@linkplain ObjectUtil#dispose() disposed).
 * 
 * @author Christian W. Damus (cdamus)
 * 
 * @since 3.1
 */
public class OCLFactoryWithHistory implements OCLFactory {

    protected final OCLFactory delegate;
    private List history = new java.util.ArrayList();
    private Set> errorNodes = new java.util.HashSet>();
    
    private boolean disposable;
    
    public OCLFactoryWithHistory(OCLFactory delegate) {
        this.delegate = delegate;
    }

    public void clear() {
        if (isDisposable()) {
            for (Object next : history) {
                ObjectUtil.dispose(next);
            }
        }
        
        history.clear();
        errorNodes.clear();
    }
    
    boolean isDisposable() {
        return disposable;
    }
    
    void setDisposable() {
        disposable = true;
    }
    
    protected  T record(T object) {
        history.add(object);
        return object;
    }
    
    void markAsErrorNode(TypedElement expr) {
    	errorNodes.add(expr);
    }
    
    boolean isErrorNode(TypedElement expr) {
    	return errorNodes.contains(expr);
    }
    
    public  AssociationClassCallExp createAssociationClassCallExp() {
        return record(delegate.createAssociationClassCallExp());
    }

    public  BagType createBagType(C elementType) {
        return record(delegate.createBagType(elementType));
    }

    public  BooleanLiteralExp createBooleanLiteralExp() {
        return record(delegate.createBooleanLiteralExp());
    }

    public  CollectionItem createCollectionItem() {
        return record(delegate.createCollectionItem());
    }

    public  CollectionLiteralExp createCollectionLiteralExp() {
        return record(delegate.createCollectionLiteralExp());
    }

    public  CollectionRange createCollectionRange() {
        return record(delegate.createCollectionRange());
    }

    public  CollectionType createCollectionType(C elementType) {
        return record(delegate.createCollectionType(elementType));
    }

    public  CollectionType createCollectionType(
            CollectionKind kind, C elementType) {
        return record(delegate.createCollectionType(kind, elementType));
    }

    public  EnumLiteralExp createEnumLiteralExp() {
        return record(delegate.createEnumLiteralExp());
    }

    public  IfExp createIfExp() {
        return record(delegate.createIfExp());
    }

    public  IntegerLiteralExp createIntegerLiteralExp() {
        return record(delegate.createIntegerLiteralExp());
    }

    public  InvalidLiteralExp createInvalidLiteralExp() {
        return record(delegate.createInvalidLiteralExp());
    }

    public  IterateExp createIterateExp() {
        return record(delegate.createIterateExp());
    }

    public  IteratorExp createIteratorExp() {
        return record(delegate.createIteratorExp());
    }

    public  LetExp createLetExp() {
        return record(delegate.createLetExp());
    }

    public  MessageExp createMessageExp() {
        return record(delegate.createMessageExp());
    }

    public  NullLiteralExp createNullLiteralExp() {
        return record(delegate.createNullLiteralExp());
    }

    public  OperationCallExp createOperationCallExp() {
        return record(delegate.createOperationCallExp());
    }

    public  MessageType createOperationMessageType(O operation) {
        return record(delegate.createOperationMessageType(operation));
    }

    public  OrderedSetType createOrderedSetType(C elementType) {
        return record(delegate.createOrderedSetType(elementType));
    }

    public  PropertyCallExp createPropertyCallExp() {
        return record(delegate.createPropertyCallExp());
    }

    public  RealLiteralExp createRealLiteralExp() {
        return record(delegate.createRealLiteralExp());
    }

    public  SequenceType createSequenceType(C elementType) {
        return record(delegate.createSequenceType(elementType));
    }

    public  SetType createSetType(C elementType) {
        return record(delegate.createSetType(elementType));
    }

    public  MessageType createSignalMessageType(C signal) {
        return record(delegate.createSignalMessageType(signal));
    }

    public  StateExp createStateExp() {
        return record(delegate.createStateExp());
    }

    public  StringLiteralExp createStringLiteralExp() {
        return record(delegate.createStringLiteralExp());
    }

    public  TupleLiteralExp createTupleLiteralExp() {
        return record(delegate.createTupleLiteralExp());
    }

    public  TupleLiteralPart createTupleLiteralPart() {
        return record(delegate.createTupleLiteralPart());
    }

    public  TupleType createTupleType(
            List> parts) {
        return record(delegate.createTupleType(parts));
    }

    public  TypeExp createTypeExp() {
        return record(delegate.createTypeExp());
    }

    public  TypeType createTypeType(C type) {
        return record(delegate.createTypeType(type));
    }

    public  UnlimitedNaturalLiteralExp createUnlimitedNaturalLiteralExp() {
        return record(delegate.createUnlimitedNaturalLiteralExp());
    }

    public  UnspecifiedValueExp createUnspecifiedValueExp() {
        return record(delegate.createUnspecifiedValueExp());
    }

    public  Variable createVariable() {
        return record(delegate.createVariable());
    }

    public  VariableExp createVariableExp() {
        return record(delegate.createVariableExp());
    }
}