001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2009-2018 CQSE GmbH                                        |
004|                                                                          |
005+-------------------------------------------------------------------------*/
006package eu.cqse.check.framework.util.clike;
007
008import java.util.List;
009import java.util.Objects;
010
011import eu.cqse.check.framework.shallowparser.TokenStreamTextUtils;
012import eu.cqse.check.framework.shallowparser.framework.ShallowEntity;
013
014/**
015 * Represents a switch construct.
016 */
017public class SwitchBlockGroup extends ConditionalBlockGroupBase {
018
019        /** The surrounding shallow entity. (Switch for case statements) */
020        private ShallowEntity surroundingEntity;
021
022        /** Constructor. */
023        public SwitchBlockGroup(ShallowEntity surroundingEntity, List<ConditionalBlock> correspondingBlocks) {
024                super(correspondingBlocks);
025                setSurroundingEntity(surroundingEntity);
026        }
027
028        /** Constructor. */
029        public SwitchBlockGroup() {
030        }
031
032        /** @see #surroundingEntity */
033        public ShallowEntity getSurroundingEntity() {
034                return surroundingEntity;
035        }
036
037        /** @see #surroundingEntity */
038        public void setSurroundingEntity(ShallowEntity surroundingEntity) {
039                this.surroundingEntity = surroundingEntity;
040        }
041
042        /** {@inheritDoc} */
043        @Override
044        public String toString() {
045                final StringBuilder s = new StringBuilder(
046                                ConditionalBlockUtils.getBlockTypeFromEntity(surroundingEntity).toString() + " ");
047                s.append(TokenStreamTextUtils.concatTokenTexts(ConditionalBlockUtils.extractCondition(surroundingEntity)));
048                s.append("\r\n");
049                return s.append(super.toString()).toString();
050        }
051
052        /** {@inheritDoc} */
053        @Override
054        public boolean equals(Object obj) {
055                if (!(obj instanceof SwitchBlockGroup)) {
056                        return false;
057                }
058
059                return ((SwitchBlockGroup) obj).getSurroundingEntity().equals(this.getSurroundingEntity()) && super.equals(obj);
060        }
061
062        /** {@inheritDoc} */
063        @Override
064        public int hashCode() {
065                return Objects.hash(super.hashCode(), surroundingEntity.hashCode());
066        }
067}