001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    
021    package org.sonar.batch.issue.ignore.pattern;
022    
023    import com.google.common.base.Preconditions;
024    import com.google.common.collect.Sets;
025    
026    import java.util.Set;
027    
028    public class LineRange {
029      private int from;
030      private int to;
031    
032      public LineRange(int from, int to) {
033        Preconditions.checkArgument(from <= to, "Line range is not valid: %s must be greater than %s", from, to);
034    
035        this.from = from;
036        this.to = to;
037      }
038    
039      public boolean in(int lineId) {
040        return from <= lineId && lineId <= to;
041      }
042    
043      public Set<Integer> toLines() {
044        Set<Integer> lines = Sets.newLinkedHashSet();
045        for (int index = from; index <= to; index++) {
046          lines.add(index);
047        }
048        return lines;
049      }
050    
051      @Override
052      public String toString() {
053        return "[" + from + "-" + to + "]";
054      }
055    
056      @Override
057      public int hashCode() {
058        final int prime = 31;
059        int result = 1;
060        result = prime * result + from;
061        result = prime * result + to;
062        return result;
063      }
064    
065      @Override
066      public boolean equals(Object obj) {
067        if (this == obj) {
068          return true;
069        }
070        if (obj == null) {
071          return false;
072        }
073        if (getClass() != obj.getClass()) {
074          return false;
075        }
076        if (fieldsDiffer((LineRange) obj)) {
077          return false;
078        }
079        return true;
080      }
081    
082      private boolean fieldsDiffer(LineRange other) {
083        if (from != other.from) {
084          return true;
085        }
086        if (to != other.to) {
087          return true;
088        }
089        return false;
090      }
091    }