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