1 package com.github.javaparser.printer.lexicalpreservation.changes; 2 3 import com.github.javaparser.ast.Node; 4 import com.github.javaparser.ast.NodeList; 5 import com.github.javaparser.ast.observer.ObservableProperty; 6 7 /** 8 * The removal of an element in a list. 9 */ 10 public class ListRemovalChange implements Change { 11 private final ObservableProperty observableProperty; 12 private final int index; 13 14 public ListRemovalChange(ObservableProperty observableProperty, int index) { 15 this.observableProperty = observableProperty; 16 this.index = index; 17 } 18 19 @Override 20 public Object getValue(ObservableProperty property, Node node) { 21 if (property == observableProperty) { 22 NodeList<Node> nodeList = new NodeList<>(); 23 Object currentRawValue = new NoChange().getValue(property, node); 24 if (!(currentRawValue instanceof NodeList)){ 25 throw new IllegalStateException("Expected NodeList, found " + currentRawValue.getClass().getCanonicalName()); 26 } 27 NodeList<?> currentNodeList = (NodeList<?>)currentRawValue; 28 nodeList.addAll(currentNodeList); 29 nodeList.remove(index); 30 return nodeList; 31 } else { 32 return new NoChange().getValue(property, node); 33 } 34 } 35 } 36