001/*-------------------------------------------------------------------------+ 002| | 003| Copyright (c) 2009-2017 CQSE GmbH | 004| | 005+-------------------------------------------------------------------------*/ 006package eu.cqse.check.framework.shallowparser.languages.cobol; 007 008import static eu.cqse.check.framework.scanner.ETokenType.NOT; 009 010import java.util.ArrayList; 011import java.util.List; 012 013import eu.cqse.check.framework.scanner.ETokenType; 014 015/** 016 * Describes a condition clause of a COBOL verb. 017 */ 018public class ConditionalClause { 019 020 /** 021 * Indicates the sequence of tokens that are mandatory for this verb 022 */ 023 private List<ETokenType> mandatoryTokens; 024 025 /** 026 * Represents an optional token in the verb 027 */ 028 private ETokenType optionalToken; 029 030 /** 031 * Indicates if the optional token starts the conditional clause or 032 * otherwise. 033 */ 034 private boolean optionalTokenStartsClause; 035 036 /** Constructor */ 037 public ConditionalClause(List<ETokenType> mandatoryTokens, ETokenType optionalToken, 038 boolean optionalTokenStartsClause) { 039 this.mandatoryTokens = mandatoryTokens; 040 this.optionalToken = optionalToken; 041 this.optionalTokenStartsClause = optionalTokenStartsClause; 042 } 043 044 /** 045 * Gets a sequence of tokens of this conditional clause with or without the 046 * optional token. 047 */ 048 public ETokenType[] getClauseTokens(boolean withOptionalToken) { 049 return getClauseTokensUsingCollection(withOptionalToken, new ArrayList<>()); 050 } 051 052 /** 053 * Get tokens for a conditional clause using an empty collection. 054 */ 055 private ETokenType[] getClauseTokensUsingCollection(boolean withOptionalToken, List<ETokenType> result) { 056 int index; 057 if (optionalTokenStartsClause) { 058 result.add(optionalToken); 059 index = result.size() - 1; 060 result.addAll(mandatoryTokens); 061 } else { 062 result.addAll(mandatoryTokens); 063 result.add(optionalToken); 064 index = result.size() - 1; 065 } 066 067 if (!withOptionalToken) { 068 result.remove(index); 069 } 070 071 return result.toArray(new ETokenType[result.size()]); 072 } 073 074 /** 075 * Get the conditional clause tokens prepended with the NOT token type. 076 */ 077 public ETokenType[] getNegatedClauseTokens(boolean withOptionalToken) { 078 List<ETokenType> result = new ArrayList<>(); 079 result.add(NOT); 080 return getClauseTokensUsingCollection(withOptionalToken, result); 081 } 082}