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
021package org.sonar.squid.measures;
022
023import java.util.IdentityHashMap;
024import java.util.Map;
025
026public class Measures {
027
028  private Map<MetricDef, Measure> measures = new IdentityHashMap<MetricDef, Measure>();
029
030  public double getValue(MetricDef metric) {
031    Measure measure = measures.get(metric);
032    if (measure == null) {
033      return 0;
034    }
035    return measure.getValue();
036  }
037
038  public Object getData(MetricDef metric) {
039    Measure measure = measures.get(metric);
040    if (measure == null) {
041      return null;
042    }
043    return measure.getData();
044  }
045
046  public void setValue(MetricDef metric, double measure) {
047    getMeasureOrCreateIt(metric).setValue(measure);
048  }
049
050  public void setData(MetricDef metric, Object data) {
051    getMeasureOrCreateIt(metric).setData(data);
052  }
053
054  private Measure getMeasureOrCreateIt(MetricDef metric) {
055    Measure measure = measures.get(metric);
056    if (measure == null) {
057      measure = new Measure(0);
058      measures.put(metric, measure);
059    }
060    return measure;
061  }
062
063  public void removeMeasure(MetricDef metric) {
064    measures.remove(metric);
065  }
066
067  private static final class Measure {
068
069    private double value;
070    private Object data;
071
072    private Measure(double value) {
073      this.value = value;
074    }
075
076    private double getValue() {
077      return value;
078    }
079
080    private void setValue(double value) {
081      this.value = value;
082    }
083
084    private Object getData() {
085      return data;
086    }
087
088    private void setData(Object data) {
089      this.data = data;
090    }
091  }
092
093}