001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.cpd.index;
021    
022    import java.util.Collection;
023    import java.util.List;
024    
025    import org.sonar.api.resources.Resource;
026    import org.sonar.duplications.block.Block;
027    import org.sonar.duplications.block.ByteArray;
028    import org.sonar.duplications.index.AbstractCloneIndex;
029    import org.sonar.duplications.index.CloneIndex;
030    import org.sonar.duplications.index.PackedMemoryCloneIndex;
031    
032    import com.google.common.collect.Lists;
033    
034    public class SonarDuplicationsIndex extends AbstractCloneIndex {
035    
036      private final CloneIndex mem = new PackedMemoryCloneIndex();
037      private final DbDuplicationsIndex db;
038    
039      public SonarDuplicationsIndex() {
040        this(null);
041      }
042    
043      public SonarDuplicationsIndex(DbDuplicationsIndex db) {
044        this.db = db;
045      }
046    
047      public void insert(Resource resource, Collection<Block> blocks) {
048        for (Block block : blocks) {
049          mem.insert(block);
050        }
051        if (db != null) {
052          db.insert(resource, blocks);
053        }
054      }
055    
056      public Collection<Block> getByResource(Resource resource, String resourceKey) {
057        if (db != null) {
058          db.prepareCache(resource);
059        }
060        return mem.getByResourceId(resourceKey);
061      }
062    
063      public Collection<Block> getBySequenceHash(ByteArray hash) {
064        if (db == null) {
065          return mem.getBySequenceHash(hash);
066        } else {
067          List<Block> result = Lists.newArrayList(mem.getBySequenceHash(hash));
068          result.addAll(db.getByHash(hash));
069          return result;
070        }
071      }
072    
073      public Collection<Block> getByResourceId(String resourceId) {
074        throw new UnsupportedOperationException();
075      }
076    
077      public void insert(Block block) {
078        throw new UnsupportedOperationException();
079      }
080    
081    }