001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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 */
020package org.sonar.wsclient.issue.internal;
021
022import org.sonar.wsclient.issue.IssueChangeDiff;
023import org.sonar.wsclient.unmarshallers.JsonUtils;
024
025import javax.annotation.CheckForNull;
026
027import java.util.Map;
028
029/**
030 * @since 4.1
031 */
032public class DefaultIssueChangeDiff implements IssueChangeDiff {
033
034  private final Map json;
035
036  DefaultIssueChangeDiff(Map json) {
037    this.json = json;
038  }
039
040  public String key() {
041    return JsonUtils.getString(json, "key");
042  }
043
044  @CheckForNull
045  public Object newValue() {
046    return parseValue("newValue");
047
048  }
049
050  @CheckForNull
051  public Object oldValue() {
052    return parseValue("oldValue");
053  }
054
055  private Object parseValue(String attribute) {
056    if ("technicalDebt".equals(key())) {
057      return parseDefaultTechnicalDebt(attribute);
058    } else {
059      return JsonUtils.getString(json, attribute);
060    }
061  }
062
063  private DefaultWorkDayDuration parseDefaultTechnicalDebt(String attribute){
064    Map technicalDebt = (Map) json.get(attribute);
065    if (technicalDebt != null) {
066      return new DefaultWorkDayDuration(technicalDebt);
067    }
068    return null;
069  }
070
071}