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