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.duplications.index; 021 022 import java.util.Collection; 023 import java.util.Collections; 024 import java.util.List; 025 import java.util.Map; 026 027 import org.sonar.duplications.block.Block; 028 import org.sonar.duplications.block.ByteArray; 029 030 import com.google.common.collect.Lists; 031 import com.google.common.collect.Maps; 032 033 public class MemoryCloneIndex2 extends AbstractCloneIndex { 034 035 private Map<String, List<Block>> byResource = Maps.newHashMap(); 036 private Map<ByteArray, List<Block>> byHash = Maps.newHashMap(); 037 038 public Collection<Block> getByResourceId(String resourceId) { 039 return get(byResource, resourceId); 040 } 041 042 public Collection<Block> getBySequenceHash(ByteArray sequenceHash) { 043 return get(byHash, sequenceHash); 044 } 045 046 public void insert(Block block) { 047 put(byResource, block.getResourceId(), block); 048 put(byHash, block.getBlockHash(), block); 049 } 050 051 private static <T> List<Block> get(Map<T, List<Block>> map, T key) { 052 List<Block> blocks = map.get(key); 053 return blocks != null ? blocks : Collections.EMPTY_LIST; 054 } 055 056 private static <T> void put(Map<T, List<Block>> map, T key, Block value) { 057 List<Block> blocks = map.get(key); 058 if (blocks == null) { 059 blocks = Lists.newLinkedList(); 060 map.put(key, blocks); 061 } 062 blocks.add(value); 063 } 064 065 }