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

eu.mihosoft.vmf.vmftext.grammar.impl.ReadOnlyAlternativeImpl Maven / Gradle / Ivy

The newest version!

package eu.mihosoft.vmf.vmftext.grammar.impl;

// vmf imports
//import eu.mihosoft.vmf.runtime.core.*;
//import eu.mihosoft.vmf.runtime.core.internal.*;
import eu.mihosoft.vcollections.*;
import eu.mihosoft.vmf.vmftext.grammar.*;
import java.beans.PropertyChangeListener;

// property types imports
import eu.mihosoft.vcollections.*;

// interface

/**
 * An read-only representation of the model object {@code eu.mihosoft.vmf.vmftext.grammar.Alternative}.
 */
@SuppressWarnings("deprecation")
class ReadOnlyAlternativeImpl implements ReadOnlyAlternative, eu.mihosoft.vmf.runtime.core.internal.VObjectInternal, VCloneableInternal {

  private final Alternative mutableObject;

  // Read-only view for collection properties
  // - properties with collection type (model & non-model collections)
  // - we don't allow/provide setters for collection properties
  private VList elements;
      
  public ReadOnlyAlternativeImpl(Alternative mutableObject) {
    this.mutableObject = mutableObject;
  }


  @Override
  // generic type is a model-type
  public VList getElements() {

    // we return an unmodifiable mapped list that contains read-only versions of the list elements
    // since read-only types and normal types are incompatible by design, we specify the lambda
    // operation that converts an element into a read-only version of that element
    if(this.elements == null) {
      this.elements = VMappedList.newUnmodifiableInstance(mutableObject.getElements(), (e)->e.asReadOnly());
    }

    return this.elements;

 
  }


  @Override
  public int getId() {
    return this.mutableObject.getId();
  }


  @Override
  // property type is a model-type
  public ReadOnlyUPRuleBase getParentRule() {
    if(this.mutableObject.getParentRule()!=null) {
      return this.mutableObject.getParentRule().asReadOnly();
    } else {
      return null;
    }
  }


  @Override
  public java.lang.String getText() {
    return this.mutableObject.getText();
  }

 // end for each property

  @Override
  public void addPropertyChangeListener(PropertyChangeListener l) {
    ((eu.mihosoft.vmf.runtime.core.internal.VObjectInternal)this.mutableObject).addPropertyChangeListener(l);
  }

  @Override
  public void removePropertyChangeListener(PropertyChangeListener l) {
    ((eu.mihosoft.vmf.runtime.core.internal.VObjectInternal)this.mutableObject).removePropertyChangeListener(l);
  }

  @Override
  public String toString() {
    return this.mutableObject.toString();
  }

  @Override
  public boolean equals(Object o) {
    return this.mutableObject.equals(o);
  }

  @Override
  public int hashCode() {
    return this.mutableObject.hashCode();
  }

  @Override
  public Alternative _vmf_getMutableObject() {
    return this.mutableObject;
  }

  @Override
  public ReadOnlyAlternativeImpl asReadOnly() {
    return this;
  }

  // --------------------------------------------------------------------
  // --- Cloning
  // --------------------------------------------------------------------

  @Override
  public ReadOnlyAlternative clone() {
    return this.mutableObject.clone().asReadOnly();
  }

  @Override
  public Alternative asModifiable() {
    return this.mutableObject.clone();
  }

  @Override
  public Alternative _vmf_deepCopy(
    java.util.IdentityHashMap identityMap) {
    return (Alternative)
      ((VCloneableInternal)this.mutableObject)._vmf_deepCopy(
        new java.util.IdentityHashMap<>());
  }

  @Override
  public ReadOnlyAlternative _vmf_shallowCopy(
    java.util.IdentityHashMap identityMap) {
    return (ReadOnlyAlternative)
      ((VCloneableInternal)this.mutableObject)._vmf_shallowCopy(
        new java.util.IdentityHashMap<>()).asReadOnly();
  }

  // --------------------------------------------------------------------
  // --- Public VMF API
  // --------------------------------------------------------------------

  @Override
  public eu.mihosoft.vmf.runtime.core.VMF vmf() {
    return new eu.mihosoft.vmf.runtime.core.VMF() {
      public eu.mihosoft.vmf.runtime.core.Content content() {
        return new eu.mihosoft.vmf.runtime.core.Content() {
          public eu.mihosoft.vmf.runtime.core.VIterator iterator() {
            return eu.mihosoft.vmf.runtime.core.VIterator.of(ReadOnlyAlternativeImpl.this);
          }
          public java.util.stream.Stream stream() {
            return eu.mihosoft.vmf.runtime.core.VIterator.of(ReadOnlyAlternativeImpl.this).asStream();
          }

          public eu.mihosoft.vmf.runtime.core.VIterator iterator(eu.mihosoft.vmf.runtime.core.VIterator.IterationStrategy strategy) {
            return eu.mihosoft.vmf.runtime.core.VIterator.of(ReadOnlyAlternativeImpl.this, strategy);
          }
          public java.util.stream.Stream stream(eu.mihosoft.vmf.runtime.core.VIterator.IterationStrategy strategy) {
            return eu.mihosoft.vmf.runtime.core.VIterator.of(ReadOnlyAlternativeImpl.this, strategy).asStream();
          }
          public  java.util.stream.Stream stream(Class type) {
            return stream().filter(e->type.isAssignableFrom(e.getClass())).map(e->(T)e);
          }
          public  java.util.stream.Stream stream(Class type, eu.mihosoft.vmf.runtime.core.VIterator.IterationStrategy strategy) {
            return stream(strategy).filter(e->type.isAssignableFrom(e.getClass())).map(e->(T)e);
          }
          public VList referencedBy() {
            return _vmf_referencedBy().asUnmodifiable();
          }
          public VList references() {
            return _vmf_references().asUnmodifiable();
          }

          @Override
          public ReadOnlyAlternative deepCopy() {
            return (ReadOnlyAlternativeImpl)
              ((VCloneableInternal)ReadOnlyAlternativeImpl.this.mutableObject)._vmf_deepCopy(
                new java.util.IdentityHashMap<>()).asReadOnly();
          }

          @Override
          public ReadOnlyAlternative shallowCopy() {
            return ReadOnlyAlternativeImpl.
              this._vmf_shallowCopy(new java.util.IdentityHashMap<>());
          }
        };
      }

      private eu.mihosoft.vmf.runtime.core.internal.ReflectImpl reflect;

      public eu.mihosoft.vmf.runtime.core.Reflect reflect() {
        if (reflect==null) {
          reflect = new eu.mihosoft.vmf.runtime.core.internal.ReflectImpl();
          reflect.setModel(ReadOnlyAlternativeImpl.this);
        }

        return reflect;
      }
    }; // end vmf
  }


  // --------------------------------------------------------------------
  // --- Reflection methods
  // --------------------------------------------------------------------

  // type id for improved reflection performance
  public static final int _VMF_TYPE_ID = 1;

  @Override
  public int _vmf_getTypeId() {
    return _VMF_TYPE_ID;
  }


  static final String[] _VMF_PROPERTY_NAMES = {
    "elements",
    "id",
    "parentRule",
    "text"
  };

  static final String[] _VMF_PROPERTY_TYPE_NAMES = {
    "eu.mihosoft.vcollections.VList", // type id -2
    "int", // type id -1
    "eu.mihosoft.vmf.vmftext.grammar.UPRuleBase", // type id 48
    "java.lang.String"  // type id -1
  };

  static final int[] _VMF_PROPERTY_TYPES = {
       -2, // type eu.mihosoft.vcollections.VList
       -1, // type int
       48, // type eu.mihosoft.vmf.vmftext.grammar.UPRuleBase
       -1  // type java.lang.String
  };


  /**
   * - indices of model objects as properties
   * - parents, i.e., containers are skipped
   * - only indices of reference properties and contained elements, i.e.,
   *   children are listed here
   */
  static final int[] _VMF_PROPERTIES_WITH_MODEL_TYPES_INDICES = {
  };

  /**
   * - indices of lists that contain model objects as elements
   */
  static final int[] _VMF_PROPERTIES_WITH_MODEL_ELEMENT_TYPES_INDICES = {
        0, // type eu.mihosoft.vcollections.VList
  };

  /**
   * - indices of model objects as properties and model objects as
   *   elements of lists
   * - parents, i.e., containers are skipped
   * - only indices of reference properties and contained elements, i.e.,
   *   children are listed here
   */
  static final int[] _VMF_PROPERTIES_WITH_MODEL_TYPE_OR_ELEMENT_TYPES_INDICES = {
        0, // type eu.mihosoft.vcollections.VList
  };

  /**
   * - indices of model children
   * - parents, i.e., containers and pure references are skipped
   * - only indices of contained elements, i.e.,
   *   children are listed here
   */
  static final int[] _VMF_CHILDREN_INDICES = {
        0, // type eu.mihosoft.vcollections.VList
  };

  @Override
  public String[] _vmf_getPropertyNames() {
    return _VMF_PROPERTY_NAMES;
  }

  @Override
  public int[] _vmf_getPropertyTypes() {
    return _VMF_PROPERTY_TYPES;
  }

  @Override
  public String[] _vmf_getPropertyTypeNames() {
    return _VMF_PROPERTY_TYPE_NAMES;
  }

  @Override
  public int[] _vmf_getIndicesOfPropertiesWithModelTypes() {
    return _VMF_PROPERTIES_WITH_MODEL_TYPES_INDICES;
  }

  @Override
  public int[] _vmf_getIndicesOfPropertiesWithModelElementTypes() {
    return _VMF_PROPERTIES_WITH_MODEL_ELEMENT_TYPES_INDICES;
  }

  @Override
  public int[] _vmf_getIndicesOfPropertiesWithModelTypeOrElementTypes() {
    return _VMF_PROPERTIES_WITH_MODEL_TYPE_OR_ELEMENT_TYPES_INDICES;
  }

  @Override
  public int[] _vmf_getChildrenIndices() {
    return _VMF_CHILDREN_INDICES;
  }

  @Override
  public Object _vmf_getPropertyValueById(int propertyId) {

    switch(propertyId) {
      case 0:
        // TODO check whether we can prevent lazy initialized properties from
        //      being initialized just for iterating the object graph
        return getElements();
      case 1:
        // TODO check whether we can prevent lazy initialized properties from
        //      being initialized just for iterating the object graph
        return getId();
      case 2:
        // TODO check whether we can prevent lazy initialized properties from
        //      being initialized just for iterating the object graph
        return getParentRule();
      case 3:
        // TODO check whether we can prevent lazy initialized properties from
        //      being initialized just for iterating the object graph
        return getText();
    }

    return null;
  }

  @Override
  public int _vmf_getPropertyIdByName(String propertyName) {
    switch(propertyName) {
      case "elements":
        return 0;
      case "id":
        return 1;
      case "parentRule":
        return 2;
      case "text":
        return 3;
      default:
        return -1;
    } // end switch
  }


  @Override
  public Object _vmf_getDefaultValueById(int propertyId) {

    switch(propertyId) {
      case 0:
        Object __vmf_default_value_tmpElements = _VMF_DEFAULT_VALUES[0];
        if(__vmf_default_value_tmpElements==null) {
            return null;
        } else {
            return __vmf_default_value_tmpElements;
        }

      case 1:
        Object __vmf_default_value_tmpId = _VMF_DEFAULT_VALUES[0];
        if(__vmf_default_value_tmpId==null) {
            return
             0;
        } else {
            return __vmf_default_value_tmpId;
        }    

      case 2:
        Object __vmf_default_value_tmpParentRule = _VMF_DEFAULT_VALUES[1];
        if(__vmf_default_value_tmpParentRule==null) {
            return null;
        } else {
            return __vmf_default_value_tmpParentRule;
        }

      case 3:
        Object __vmf_default_value_tmpText = _VMF_DEFAULT_VALUES[0];
        if(__vmf_default_value_tmpText==null) {
            return null;
        } else {
            return __vmf_default_value_tmpText;
        }

    } // end switch

    return null;
  }

  @Override
  public void _vmf_setDefaultValueById(int propertyId, Object defaultValue) {

    // property elements
    if(propertyId == 0) { 
      throw new RuntimeException("Cannot set default value for property 'elements' with id="+propertyId +": property is a containment property and not writable.");
    }
    // property parentRule
    if(propertyId == 2) { 
      throw new RuntimeException("Cannot set default value for property 'parentRule' with id="+propertyId +": property is a containment property and not writable.");
    }

  }

  @Override
  public boolean _vmf_isSetById(int propertyId) {
    return !java.util.Objects.equals(_vmf_getDefaultValueById(propertyId), _vmf_getPropertyValueById(propertyId));
  }


  final Object[] _VMF_DEFAULT_VALUES = {
    null, // type elements (containment type, no defaut value possible)  
    null, // type id   
    null, // type parentRule (containment type, no defaut value possible)  
    null  // type text   
  };

  @Override
  public boolean _vmf_isReadOnly() {
    return true;
  }

  // --------------------------------------------------------------------
  // --- Id related methods
  // --------------------------------------------------------------------

  // id management is currently not part of VMF (TODO how should we support this?)

  // --------------------------------------------------------------------
  // --- Reference methods
  // --------------------------------------------------------------------

  public VList _vmf_referencedBy() { return  ((eu.mihosoft.vmf.runtime.core.internal.VObjectInternalModifiable)this.mutableObject)._vmf_referencedBy();}

  public VList _vmf_references() { return  ((eu.mihosoft.vmf.runtime.core.internal.VObjectInternalModifiable)this.mutableObject)._vmf_references();}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy