001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.web.health;
017
018import org.codehaus.jackson.annotate.JsonProperty;
019import org.apache.commons.lang.StringUtils;
020
021/**
022 * Defines the name and value for a single metric.
023 *
024 * All metrics must have a "Measure" name, a "Metric" name and a value. The value may be any value that translates
025 * property to JSON. The value may also be null.
026 *
027 * This class is annotated such that it should serialize to JSON when using the Jackson library.
028 *
029 * @author Eric Westfall
030 */
031public class HealthMetric {
032
033    @JsonProperty("Measure")
034    private final String measure;
035
036    @JsonProperty("Metric")
037    private final String metric;
038
039    @JsonProperty("Value")
040    private final Object value;
041
042    /**
043     * Construct a new HealthMetric using the given two-art name and value. The format of the name is "Measure:Metric"
044     * where "Measure" and "Metric" are replaced by the desired measure and metric name.
045     *
046     * Invoking new HealthMetric("a:b", "c") is equivalent to invoking new HealthMetric("a", "b", "c")
047     *
048     * @param name the name for this health metric, must be a string that includes two non-blank parts separated by a colon
049     * @param value the value of this health metric
050     */
051    public HealthMetric(String name, Object value) {
052        if (StringUtils.isBlank(name)) {
053            throw new IllegalArgumentException("Metric name must not be blank");
054        }
055        String[] nameParts = name.split(":");
056        if (nameParts.length != 2 || nameParts[0].isEmpty() || nameParts[1].isEmpty()) {
057            throw new IllegalArgumentException("Metric name was not valid, should be two non-blank parts separated by ':'. Instead was " + name);
058        }
059        this.measure = nameParts[0];
060        this.metric = nameParts[1];
061        this.value = value;
062    }
063
064    /**
065     * Construct a new HealthMetric with the given measure name, metric name, and value.
066     *
067     * @param measure the name of the measure
068     * @param metric the name of the metric
069     * @param value the value of this health metric
070     */
071    public HealthMetric(String measure, String metric, Object value) {
072        if (StringUtils.isBlank(measure)) {
073            throw new IllegalArgumentException("measure name must not be blank");
074        }
075        if (StringUtils.isBlank(metric)) {
076            throw new IllegalArgumentException("metric name must not be blank");
077        }
078        this.measure = measure;
079        this.metric = metric;
080        this.value = value;
081    }
082
083    public String getMeasure() {
084        return measure;
085    }
086
087    public String getMetric() {
088        return metric;
089    }
090
091    public Object getValue() {
092        return value;
093    }
094
095}