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.source;
022    
023    import com.google.common.collect.SortedSetMultimap;
024    import com.google.common.collect.TreeMultimap;
025    import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbol;
026    import org.sonar.api.source.Symbol;
027    import org.sonar.api.source.Symbolizable;
028    import org.sonar.batch.symbol.DefaultSymbolTableBuilder;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    public class DefaultSymbolTable implements Symbolizable.SymbolTable {
034    
035      private SortedSetMultimap<org.sonar.api.batch.sensor.symbol.Symbol, Integer> referencesBySymbol;
036    
037      private DefaultSymbolTable(SortedSetMultimap<org.sonar.api.batch.sensor.symbol.Symbol, Integer> referencesBySymbol) {
038        this.referencesBySymbol = referencesBySymbol;
039      }
040    
041      public SortedSetMultimap<org.sonar.api.batch.sensor.symbol.Symbol, Integer> getReferencesBySymbol() {
042        return referencesBySymbol;
043      }
044    
045      @Override
046      public List<Symbol> symbols() {
047        List<Symbol> result = new ArrayList<Symbol>();
048        for (org.sonar.api.batch.sensor.symbol.Symbol symbol : referencesBySymbol.keySet()) {
049          result.add((Symbol) symbol);
050        }
051        return result;
052      }
053    
054      @Override
055      public List<Integer> references(Symbol symbol) {
056        return new ArrayList<Integer>(referencesBySymbol.get(symbol));
057      }
058    
059      public static class Builder implements Symbolizable.SymbolTableBuilder {
060    
061        private final SortedSetMultimap<org.sonar.api.batch.sensor.symbol.Symbol, Integer> referencesBySymbol;
062        private final String componentKey;
063    
064        public Builder(String componentKey) {
065          this.componentKey = componentKey;
066          referencesBySymbol = TreeMultimap.create(new DefaultSymbolTableBuilder.SymbolComparator(), new DefaultSymbolTableBuilder.ReferenceComparator());
067        }
068    
069        @Override
070        public Symbol newSymbol(int fromOffset, int toOffset) {
071          Symbol symbol = new DefaultSymbol(componentKey, fromOffset, toOffset);
072          referencesBySymbol.put(symbol, symbol.getDeclarationStartOffset());
073          return symbol;
074        }
075    
076        @Override
077        public void newReference(Symbol symbol, int fromOffset) {
078          if (fromOffset >= symbol.getDeclarationStartOffset() && fromOffset < symbol.getDeclarationEndOffset()) {
079            throw new UnsupportedOperationException("Cannot add reference (" + fromOffset + ") overlapping " + symbol);
080          }
081          referencesBySymbol.put(symbol, fromOffset);
082        }
083    
084        @Override
085        public Symbolizable.SymbolTable build() {
086          return new DefaultSymbolTable(referencesBySymbol);
087        }
088    
089      }
090    }