001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 */ 020package org.sonar.api.ce.measure.test; 021 022import javax.annotation.CheckForNull; 023import javax.annotation.Nullable; 024import javax.annotation.concurrent.Immutable; 025import org.sonar.api.ce.measure.Component; 026 027import static com.google.common.base.Preconditions.checkState; 028import static java.util.Objects.requireNonNull; 029 030@Immutable 031public class TestComponent implements Component { 032 033 private final String key; 034 035 private final Type type; 036 037 @CheckForNull 038 private final FileAttributes fileAttributes; 039 040 public TestComponent(String key, Type type, @Nullable FileAttributes fileAttributes) { 041 this.key = requireNonNull(key, "Key cannot be null"); 042 this.type = requireNonNull(type, "Type cannot be null"); 043 this.fileAttributes = checkFileAttributes(fileAttributes); 044 } 045 046 @CheckForNull 047 private FileAttributes checkFileAttributes(@Nullable FileAttributes fileAttributes) { 048 if (fileAttributes == null && type == Type.FILE) { 049 throw new IllegalArgumentException("Component of type FILE must have a FileAttributes object"); 050 } else if (fileAttributes != null && type != Type.FILE) { 051 throw new IllegalArgumentException("Only component of type FILE have a FileAttributes object"); 052 } 053 return fileAttributes; 054 } 055 056 @Override 057 public Type getType() { 058 return type; 059 } 060 061 @Override 062 public String getKey() { 063 return key; 064 } 065 066 @Override 067 public FileAttributes getFileAttributes() { 068 checkState(this.type == Component.Type.FILE, "Only component of type FILE have a FileAttributes object"); 069 return fileAttributes; 070 } 071 072 @Override 073 public boolean equals(Object o) { 074 if (this == o) { 075 return true; 076 } 077 if (o == null || getClass() != o.getClass()) { 078 return false; 079 } 080 081 TestComponent component = (TestComponent) o; 082 083 return key.equals(component.key); 084 } 085 086 @Override 087 public int hashCode() { 088 return key.hashCode(); 089 } 090 091 @Override 092 public String toString() { 093 return "ComponentImpl{" + 094 "key=" + key + 095 ", type='" + type + '\'' + 096 ", fileAttributes=" + fileAttributes + 097 '}'; 098 } 099 100 @Immutable 101 public static class FileAttributesImpl implements FileAttributes { 102 103 private final boolean unitTest; 104 private final String languageKey; 105 106 public FileAttributesImpl(@Nullable String languageKey, boolean unitTest) { 107 this.languageKey = languageKey; 108 this.unitTest = unitTest; 109 } 110 111 @Override 112 public boolean isUnitTest() { 113 return unitTest; 114 } 115 116 @Override 117 @CheckForNull 118 public String getLanguageKey() { 119 return languageKey; 120 } 121 122 @Override 123 public String toString() { 124 return "FileAttributesImpl{" + 125 "languageKey='" + languageKey + '\'' + 126 ", unitTest=" + unitTest + 127 '}'; 128 } 129 } 130} 131