001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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 com.google.common.collect.Lists;
023 import org.sonar.api.resources.Resource;
024 import org.sonar.duplications.block.Block;
025 import org.sonar.duplications.block.ByteArray;
026 import org.sonar.duplications.index.AbstractCloneIndex;
027 import org.sonar.duplications.index.CloneIndex;
028 import org.sonar.duplications.index.PackedMemoryCloneIndex;
029
030 import java.util.Collection;
031 import java.util.List;
032
033 public class SonarDuplicationsIndex extends AbstractCloneIndex {
034
035 private final CloneIndex mem = new PackedMemoryCloneIndex();
036 private final DbDuplicationsIndex db;
037
038 public SonarDuplicationsIndex() {
039 this.db = null;
040 }
041
042 public SonarDuplicationsIndex(DbDuplicationsIndex db) {
043 this.db = db;
044 }
045
046 public void insert(Resource resource, Collection<Block> blocks) {
047 for (Block block : blocks) {
048 mem.insert(block);
049 }
050 if (db != null) {
051 db.insert(resource, blocks);
052 }
053 }
054
055 public Collection<Block> getByResource(Resource resource, String resourceKey) {
056 if (db != null) {
057 db.prepareCache(resource);
058 }
059 return mem.getByResourceId(resourceKey);
060 }
061
062 public Collection<Block> getBySequenceHash(ByteArray hash) {
063 if (db == null) {
064 return mem.getBySequenceHash(hash);
065 } else {
066 List<Block> result = Lists.newArrayList(mem.getBySequenceHash(hash));
067 result.addAll(db.getByHash(hash));
068 return result;
069 }
070 }
071
072 public Collection<Block> getByResourceId(String resourceId) {
073 throw new UnsupportedOperationException();
074 }
075
076 public void insert(Block block) {
077 throw new UnsupportedOperationException();
078 }
079
080 }