001/*-------------------------------------------------------------------------+ 002| | 003| Copyright (c) 2009-2018 CQSE GmbH | 004| | 005+-------------------------------------------------------------------------*/ 006package eu.cqse.check.framework.util.clike; 007 008import java.util.ArrayList; 009import java.util.List; 010import java.util.Objects; 011 012import eu.cqse.check.framework.scanner.IToken; 013import eu.cqse.check.framework.shallowparser.framework.ShallowEntity; 014 015/** 016 * This class represents a single block of a conditional expression. It can be 017 * one of the types of {@link EConditionalType}. An condition may be given if 018 * the block represents an if, else if or case block. The content of the block 019 * is a list of shallow entities that represent the code that gets executed when 020 * the block gets executed. 021 */ 022public class ConditionalBlock { 023 024 /** The {@link EConditionalType} of the block. */ 025 protected EConditionalType blockType; 026 027 /** The list of ITokens representing the condition. */ 028 protected List<IToken> condition; 029 030 /** The content of the block that gets executed. */ 031 protected List<ShallowEntity> content; 032 033 /** 034 * Constructor. Converts the given shallow entity to a Block object with the 035 * given blocktype. 036 */ 037 public ConditionalBlock(EConditionalType blockType, List<ShallowEntity> content) { 038 this(blockType, new ArrayList<IToken>(), content); 039 } 040 041 public ConditionalBlock(EConditionalType blockType, List<IToken> condition, List<ShallowEntity> content) { 042 setBlockType(blockType); 043 setCondition(condition); 044 setContent(content); 045 } 046 047 /** @see #blockType */ 048 public EConditionalType getBlockType() { 049 return blockType; 050 } 051 052 /** @see #blockType */ 053 public void setBlockType(EConditionalType blockType) { 054 this.blockType = blockType; 055 } 056 057 /** @see #condition */ 058 public List<IToken> getCondition() { 059 return condition; 060 } 061 062 /** @see #condition */ 063 public void setCondition(List<IToken> condition) { 064 this.condition = condition; 065 } 066 067 /** @see #content */ 068 public List<ShallowEntity> getContent() { 069 return content; 070 } 071 072 /** @see #content */ 073 public void setContent(List<ShallowEntity> content) { 074 this.content = content; 075 } 076 077 /** Weather or not the block has a condition. */ 078 public boolean hasCondition() { 079 return !condition.isEmpty(); 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 public String toString() { 085 final StringBuilder s = new StringBuilder(blockType.toString() + " "); 086 condition.forEach(token -> s.append(token.getText())); 087 s.append("\r\n"); 088 return s.append(content.toString()).toString(); 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public boolean equals(Object obj) { 094 if (!(obj instanceof ConditionalBlock)) { 095 return false; 096 } 097 098 ConditionalBlock other = (ConditionalBlock) obj; 099 100 return other.blockType.equals(blockType) && other.condition.equals(condition) && other.content.equals(content); 101 } 102 103 /** {@inheritDoc} */ 104 @Override 105 public int hashCode() { 106 return Objects.hash(getBlockType(), condition, content); 107 } 108 109 /** @return whether the block contains any content */ 110 public boolean hasContent() { 111 return !content.isEmpty(); 112 } 113}