package de.cooperateproject.modeling.textual.usecase.derivedstate.calculator;

import org.eclipse.uml2.uml.Classifier;

import de.cooperateproject.modeling.textual.common.metamodel.textualCommons.UMLReferencingElement;
import de.cooperateproject.modeling.textual.usecase.usecase.Generalization;
import de.cooperateproject.modeling.textual.xtext.runtime.derivedstate.initializer.Applicability;
import de.cooperateproject.modeling.textual.xtext.runtime.derivedstate.initializer.AtomicDerivedStateProcessorBase;
import de.cooperateproject.modeling.textual.xtext.runtime.derivedstate.initializer.DerivedStateProcessorApplicability;

/**
 * State calculator for generalizations.
 */
@Applicability(applicabilities = DerivedStateProcessorApplicability.CALCULATION)
public class GeneralizationCalculator extends AtomicDerivedStateProcessorBase<Generalization> {

    /**
     * Constructs the calculator.
     */
    public GeneralizationCalculator() {
        super(Generalization.class);
    }

    @Override
    protected void applyTyped(Generalization object) {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        UMLReferencingElement<Classifier> specific = (UMLReferencingElement) object.getSpecific();
        @SuppressWarnings({ "rawtypes", "unchecked" })
        UMLReferencingElement<Classifier> general = (UMLReferencingElement) object.getGeneral();

        Classifier umlSpecific = specific.getReferencedElement();
        Classifier umlGeneral = general.getReferencedElement();

        if (umlSpecific == null || umlGeneral == null) {
            return;
        }

        org.eclipse.uml2.uml.Generalization umlGeneralization = umlSpecific.getGeneralization(umlGeneral);
        if (umlGeneralization != null) {
            object.setReferencedElement(umlGeneralization);
            return;
        }

        return;
    }

}

@@ -68,24 +68,52 @@
 alculator;%0A%0A
+import java.util.Optional;%0A%0A
 import org.e
@@ -1092,84 +1092,16 @@
     
-@SuppressWarnings(%7B %22rawtypes%22, %22unchecked%22 %7D)%0A        UMLReferencingElement
+Optional
 %3CCla
@@ -1109,17 +1109,20 @@
 sifier%3E 
-s
+umlS
 pecific 
@@ -1127,32 +1127,28 @@
 c = 
-(UMLReferencingElement) 
+Optional.ofNullable(
 obje
@@ -1163,17 +1163,17 @@
 ecific()
-;
+)
 %0A       
@@ -1177,119 +1177,55 @@
     
-@SuppressWarnings(%7B %22rawtypes%22, %22unchecked%22 %7D)%0A        UMLReferencingElement%3CClassifier%3E general = (UML
+        .map(UMLReferencingElement::get
 Referenc
 ingE
@@ -1224,19 +1224,18 @@
 renc
-ing
+ed
 Element)
  obj
@@ -1234,39 +1234,27 @@
 ent)
- object.getGeneral();%0A%0A        
+;%0A        Optional%3C
 Clas
@@ -1263,96 +1263,108 @@
 fier
+%3E
  uml
-Specific = specific.getReferencedElement();%0A        Classifier umlGeneral = general.
+General = Optional.ofNullable(object.getGeneral())%0A                .map(UMLReferencingElement::
 getR
@@ -1379,17 +1379,16 @@
 dElement
-(
 );%0A%0A    
@@ -1395,16 +1395,17 @@
     if (
+!
 umlSpeci
@@ -1411,20 +1411,25 @@
 ific
- == null
+.isPresent()
  %7C%7C 
+!
 umlG
@@ -1438,16 +1438,20 @@
 eral
- == null
+.isPresent()
 ) %7B%0A
@@ -1557,16 +1557,22 @@
 pecific.
+get().
 getGener
@@ -1591,16 +1591,22 @@
 lGeneral
+.get()
 );%0A     
@@ -1713,46 +1713,9 @@
     
-    return;%0A        %7D%0A%0A        return;
+%7D
 %0A   

Use case diagram: Fixed NPE during editing of generalizations