| /*=============================================================================# |
| # Copyright (c) 2015, 2018 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.yaml.core.model; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.statet.jcommons.collections.ImCollections; |
| import org.eclipse.statet.jcommons.collections.ImList; |
| import org.eclipse.statet.jcommons.text.core.TextRegion; |
| |
| import org.eclipse.statet.ltk.model.core.elements.ISourceUnit; |
| import org.eclipse.statet.yaml.core.model.IYamlCompositeSourceElement; |
| import org.eclipse.statet.yaml.core.model.IYamlSourceElement; |
| |
| |
| public class CompositeSourceElement extends YamlSourceElement.SourceContainer |
| implements IYamlCompositeSourceElement { |
| |
| |
| private final ImList<? extends IYamlSourceElement> compositeElements; |
| |
| private final TextRegion sourceRange; |
| |
| private volatile List<IYamlSourceElement> allSourceChildren; |
| |
| |
| public CompositeSourceElement(final List<? extends IYamlSourceElement> elements, |
| final ISourceUnit su, final TextRegion sourceRange) { |
| super(su, null); |
| |
| this.compositeElements= ImCollections.toList(elements); |
| this.sourceRange= sourceRange; |
| } |
| |
| |
| @Override |
| public ImList<? extends IYamlSourceElement> getCompositeElements() { |
| return this.compositeElements; |
| } |
| |
| @Override |
| public TextRegion getSourceRange() { |
| return this.sourceRange; |
| } |
| |
| @Override |
| public boolean hasSourceChildren(final Filter filter) { |
| for (final IYamlSourceElement element : this.compositeElements) { |
| if (element.hasSourceChildren(filter)) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| @Override |
| public List<IYamlSourceElement> getSourceChildren(final Filter filter) { |
| if (filter == null) { |
| List<IYamlSourceElement> children= this.allSourceChildren; |
| if (children == null) { |
| final List<? extends IYamlSourceElement>[] compositeLists= new List[this.compositeElements.size()]; |
| for (int i= 0; i < compositeLists.length; i++) { |
| compositeLists[i]= this.compositeElements.get(i).getSourceChildren(null); |
| } |
| children= this.allSourceChildren= ImCollections.concatList(compositeLists); |
| } |
| return children; |
| } |
| else { |
| final List<IYamlSourceElement> children= new ArrayList<>(); |
| for (final IYamlSourceElement element : this.compositeElements) { |
| final List<? extends IYamlSourceElement> list= element.getSourceChildren(null); |
| for (final IYamlSourceElement child : list) { |
| if (filter.include(child)) { |
| children.add(child); |
| } |
| } |
| } |
| return children; |
| } |
| } |
| |
| } |