blob: 3553aed828275668abaa2bdee772650d165eba76 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2011, 2019 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.r.core.sourcemodel;
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.r.core.model.IRCompositeSourceElement;
import org.eclipse.statet.r.core.model.IRLangSourceElement;
import org.eclipse.statet.r.core.model.IRSourceUnit;
public class CompositeSourceElement extends RSourceFileElement
implements IRCompositeSourceElement {
private final ImList<? extends RChunkBuildElement> compositeElements;
private final TextRegion sourceRange;
private volatile List<IRLangSourceElement> allSourceChildren;
public CompositeSourceElement(final IRSourceUnit su, final BuildSourceFrame envir,
final List<? extends RChunkBuildElement> elements, final TextRegion sourceRange) {
super(su, envir);
this.compositeElements= ImCollections.toList(elements);
this.sourceRange= sourceRange;
}
// @Override
// public int getElementType() {
// return IRElement.C2_SOURCE_FILE | 0x1;
// }
@Override
public ImList<? extends IRLangSourceElement> getCompositeElements() {
return this.compositeElements;
}
@Override
public TextRegion getSourceRange() {
return this.sourceRange;
}
@Override
public boolean hasSourceChildren(final Filter filter) {
for (final RChunkBuildElement element : this.compositeElements) {
if (element.hasSourceChildren(filter)) {
return true;
}
}
return false;
}
@Override
public List<IRLangSourceElement> getSourceChildren(final Filter filter) {
if (filter == null) {
List<IRLangSourceElement> children= this.allSourceChildren;
if (children == null) {
final List<? extends IRLangSourceElement>[] 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<IRLangSourceElement> children= new ArrayList<>();
for (final RChunkBuildElement element : this.compositeElements) {
final List<? extends IRLangSourceElement> list= element.getSourceChildren(null);
for (final IRLangSourceElement child : list) {
if (filter.include(child)) {
children.add(child);
}
}
}
return children;
}
}
}