/*
 * Copyright 2015 Open mHealth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openmhealth.schema.domain;

import org.openmhealth.schema.serializer.SerializationConstructor;

import java.time.OffsetDateTime;


/**
 * A base class for measures that provides an extensible builder for common properties.
 *
 * @author Emerson Farrugia
 */
public abstract class Measure {

    // these fields are not final to prevent errors caused by the presence of the default serialization constructor
    private TimeFrame effectiveTimeFrame;
    private DescriptiveStatistic descriptiveStatistic;


    public static abstract class Builder<T extends Builder> {

        private TimeFrame effectiveTimeFrame;
        private DescriptiveStatistic descriptiveStatistic;

        public Builder setEffectiveTimeFrame(TimeFrame effectiveTimeFrame) {
            this.effectiveTimeFrame = effectiveTimeFrame;
            return this;
        }

        public T setEffectiveTimeFrame(TimeInterval timeInterval) {
            this.effectiveTimeFrame = new TimeFrame(timeInterval);
            return (T) this;
        }

        public T setEffectiveTimeFrame(OffsetDateTime dateTime) {
            this.effectiveTimeFrame = new TimeFrame(dateTime);
            return (T) this;
        }

        public T setDescriptiveStatistic(DescriptiveStatistic descriptiveStatistic) {
            this.descriptiveStatistic = descriptiveStatistic;
            return (T) this;
        }

        public abstract Measure build();
    }

    @SerializationConstructor
    protected Measure() {

    }

    protected Measure(Builder builder) {

        this.effectiveTimeFrame = builder.effectiveTimeFrame;
        this.descriptiveStatistic = builder.descriptiveStatistic;
    }

    public TimeFrame getEffectiveTimeFrame() {
        return effectiveTimeFrame;
    }

    public DescriptiveStatistic getDescriptiveStatistic() {
        return descriptiveStatistic;
    }

    @SuppressWarnings("SimplifiableIfStatement")
    @Override
    public boolean equals(Object object) {

        if (this == object) {
            return true;
        }
        if (object == null || getClass() != object.getClass()) {
            return false;
        }

        Measure measure = (Measure) object;

        if (effectiveTimeFrame != null
                ? !effectiveTimeFrame.equals(measure.effectiveTimeFrame)
                : measure.effectiveTimeFrame != null) {
            return false;
        }
        return descriptiveStatistic == measure.descriptiveStatistic;

    }

    @Override
    public int hashCode() {

        int result = effectiveTimeFrame != null ? effectiveTimeFrame.hashCode() : 0;
        result = 31 * result + (descriptiveStatistic != null ? descriptiveStatistic.hashCode() : 0);
        return result;
    }
}

@@ -1106,16 +1106,46 @@
 tistic;%0A
+    private String userNotes;%0A
 %0A%0A    pu
@@ -1301,24 +1301,58 @@
 veStatistic;
+%0A        private String userNotes;
 %0A%0A        pu
@@ -2036,32 +2036,162 @@
 his;%0A        %7D%0A%0A
+        public T setUserNotes(String userNotes) %7B%0A            this.userNotes = userNotes;%0A            return (T) this;%0A        %7D%0A%0A
         public a
@@ -2652,24 +2652,106 @@
 tic;%0A    %7D%0A%0A
+    public String getUserNotes() %7B%0A        return userNotes;%0A    %7D%0A%0A    @Override%0A
     @Suppres
@@ -2787,30 +2787,16 @@
 ement%22)%0A
-    @Override%0A
     publ
@@ -3081,32 +3081,16 @@
  != null
-%0A               
  ? !effe
@@ -3231,39 +3231,36 @@
       %7D%0A        
-return 
+if (
 descriptiveStati
@@ -3264,17 +3264,17 @@
 atistic 
-=
+!
 = measur
@@ -3295,16 +3295,158 @@
 tatistic
+) %7B%0A            return false;%0A        %7D%0A        return !(userNotes != null ? !userNotes.equals(measure.userNotes) : measure.userNotes != null)
 ;%0A%0A    %7D
@@ -3676,16 +3676,95 @@
 ) : 0);%0A
+        result = 31 * result + (userNotes != null ? userNotes.hashCode() : 0);%0A
         

Add user notes property to measures