eu.cqse.check.framework.util.clike.ConditionalBlock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-check-api Show documentation
Show all versions of teamscale-check-api Show documentation
The Teamscale Custom Check API allows users to extend Teamscale by writing custom analyses that create findings.
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.cqse.check.framework.util.clike;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import eu.cqse.check.framework.scanner.IToken;
import eu.cqse.check.framework.shallowparser.framework.ShallowEntity;
/**
* This class represents a single block of a conditional expression. It can be one of the types of
* {@link EConditionalType}. An condition may be given if the block represents an if, else if or
* case block. The content of the block is a list of shallow entities that represent the code that
* gets executed when the block gets executed.
*/
public class ConditionalBlock {
/** The {@link EConditionalType} of the block. */
protected EConditionalType blockType;
/** The list of ITokens representing the condition. */
protected List condition;
/** The content of the block that gets executed. */
protected List content;
/**
* Constructor. Converts the given shallow entity to a Block object with the given blocktype.
*/
public ConditionalBlock(EConditionalType blockType, List content) {
this(blockType, new ArrayList<>(), content);
}
public ConditionalBlock(EConditionalType blockType, List condition, List content) {
setBlockType(blockType);
setCondition(condition);
setContent(content);
}
/** @see #blockType */
public EConditionalType getBlockType() {
return blockType;
}
/** @see #blockType */
public void setBlockType(EConditionalType blockType) {
this.blockType = blockType;
}
/** @see #condition */
public List getCondition() {
return condition;
}
/** @see #condition */
public void setCondition(List condition) {
this.condition = condition;
}
/** @see #content */
public List getContent() {
return content;
}
/** @see #content */
public void setContent(List content) {
this.content = content;
}
/** Weather or not the block has a condition. */
public boolean hasCondition() {
return !condition.isEmpty();
}
/** {@inheritDoc} */
@Override
public String toString() {
StringBuilder s = new StringBuilder(blockType.toString() + " ");
condition.forEach(token -> s.append(token.getText()));
s.append("\r\n");
return s.append(content.toString()).toString();
}
/** {@inheritDoc} */
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ConditionalBlock)) {
return false;
}
ConditionalBlock other = (ConditionalBlock) obj;
return other.blockType.equals(blockType) && other.condition.equals(condition) && other.content.equals(content);
}
/** {@inheritDoc} */
@Override
public int hashCode() {
return Objects.hash(getBlockType(), condition, content);
}
/** @return whether the block contains any content */
public boolean hasContent() {
return !content.isEmpty();
}
}