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.core.timemachine;
021
022 import com.google.common.collect.Lists;
023 import org.apache.commons.codec.digest.DigestUtils;
024 import org.apache.commons.lang.StringUtils;
025
026 import java.util.List;
027
028 public final class SourceChecksum {
029
030 private static final String SPACE_CHARS = "\t\n\r ";
031
032 private SourceChecksum() {
033 // only static methods
034 }
035
036 /**
037 * @param line line number (first line has number 1)
038 * @return checksum or null if checksum not exists for line
039 */
040 public static String getChecksumForLine(List<String> checksums, Integer line) {
041 if (line == null || line < 1 || line > checksums.size()) {
042 return null;
043 }
044 return checksums.get(line - 1);
045 }
046
047 public static List<String> lineChecksumsOfFile(String file) {
048 List<String> result = Lists.newArrayList();
049 if (file != null) {
050 String[] lines = file.split("\r?\n|\r", -1);
051 for (String line : lines) {
052 result.add(lineChecksum(line));
053 }
054 }
055 return result;
056 }
057
058 public static String lineChecksum(String line) {
059 String reducedLine = StringUtils.replaceChars(line, SPACE_CHARS, "");
060 return DigestUtils.md5Hex(reducedLine);
061 }
062
063 }