/*
 * Copyright 2016 the original author or authors.
 *
 * 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.gradle.launcher.daemon.registry;

import org.gradle.api.Nullable;
import org.gradle.launcher.daemon.server.expiry.DaemonExpirationStatus;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

/**
 * Information regarding when and why a daemon was stopped.
 */
public class DaemonStopEvent implements Serializable, Comparable<DaemonStopEvent> {
    private final Date timestamp;
    private final long pid;
    @Nullable
    private final DaemonExpirationStatus status;
    @Nullable
    private final String reason;

    public DaemonStopEvent(Date timestamp, long pid, @Nullable DaemonExpirationStatus status, @Nullable String reason) {
        this.timestamp = timestamp;
        this.status = status;
        this.reason = reason;
        this.pid = pid;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public long getPid() {
        return pid;
    }

    @Nullable
    public DaemonExpirationStatus getStatus() {
        return status;
    }

    @Nullable
    public String getReason() {
        if (reason == null) {
            return "";
        }
        return reason;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        DaemonStopEvent stopEvent = (DaemonStopEvent) o;
        return timestamp.equals(stopEvent.timestamp)
            && (reason != null ? reason.equals(stopEvent.reason) : stopEvent.reason == null);
    }

    @Override
    public int hashCode() {
        int result = timestamp.hashCode();
        result = 31 * result + (reason != null ? reason.hashCode() : 0);
        return result;
    }

    @Override
    public int compareTo(@Nullable DaemonStopEvent other) {
        if (other == null) {
            return 1;
        }

        if (this.status != null && other.status == null) {
            return -1;
        } else if (this.status == null && other.status != null) {
            return 1;
        } else if (this.status != null && other.status != null) {
            return other.status.compareTo(this.status);
        }
        return 0;
    }

    @Override
    public String toString() {
        return "DaemonStopEvent{" +
            "timestamp=" + timestamp +
            ", pid=" + pid +
            ", status=" + status +
            '}';
    }

    public boolean occurredInLastHours(final int numHours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(System.currentTimeMillis()));
        cal.add(Calendar.HOUR_OF_DAY, -1 * numHours);
        return timestamp.after(cal.getTime());
    }
}

@@ -2909,18 +2909,16 @@
 pEvent%7B%22
- +
 %0A       
@@ -2921,16 +2921,18 @@
         
+ +
  %22timest
@@ -2948,18 +2948,16 @@
 imestamp
- +
 %0A       
@@ -2956,24 +2956,26 @@
 %0A           
+ +
  %22, pid=%22 + 
@@ -2977,18 +2977,16 @@
 =%22 + pid
- +
 %0A       
@@ -2989,16 +2989,18 @@
         
+ +
  %22, stat
@@ -3012,18 +3012,16 @@
 + status
- +
 %0A       
@@ -3029,11 +3029,13 @@
     
-'%7D'
++ %22%7D%22
 ;%0A  

Fix checkstyle warning for long string.