/*
 * Copyright 2011 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.api.internal.tasks.compile.daemon;

import org.gradle.api.InvalidUserDataException;
import org.gradle.api.tasks.compile.ForkOptions;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DaemonForkOptions {
    private final String minHeapSize;
    private final String maxHeapSize;
    private final List<String> jvmArgs;

    public DaemonForkOptions(String minHeapSize, String maxHeapSize, List<String> jvmArgs) {
        this.minHeapSize = minHeapSize;
        this.maxHeapSize = maxHeapSize;
        this.jvmArgs = jvmArgs;
    }
    
    public DaemonForkOptions(ForkOptions forkOptions) {
        this(forkOptions.getMemoryInitialSize(), forkOptions.getMemoryMaximumSize(), forkOptions.getJvmArgs());
    }

    public String getMinHeapSize() {
        return minHeapSize;
    }

    public String getMaxHeapSize() {
        return maxHeapSize;
    }

    public List<String> getJvmArgs() {
        return jvmArgs;
    }

    public boolean isCompatibleWith(DaemonForkOptions other) {
        return getHeapSizeMb(minHeapSize) >= getHeapSizeMb(other.getMinHeapSize())
                && getHeapSizeMb(maxHeapSize) >= getHeapSizeMb(other.getMaxHeapSize())
                && getNormalizedJvmArgs(jvmArgs).equals(getNormalizedJvmArgs(other.getJvmArgs()));
    }

    private int getHeapSizeMb(String heapSize) {
        String normalized = heapSize.trim().toLowerCase();
        try {
            if (normalized.endsWith("m")) {
                return Integer.parseInt(normalized.substring(0, normalized.length() - 1));
            }
            if (normalized.endsWith("g")) {
                return Integer.parseInt(normalized.substring(0, normalized.length() - 1)) * 1024;
            }
        } catch (NumberFormatException e) {
            throw new InvalidUserDataException("Cannot parse heap size: " + heapSize, e);
        }
        throw new InvalidUserDataException("Cannot parse heap size: " + heapSize);
    }
    
    private Set<String> getNormalizedJvmArgs(List<String> jvmArgs) {
        Set<String> normalized = new HashSet<String>(jvmArgs.size());
        for (String jvmArg : jvmArgs) {
            normalized.add(jvmArg.trim());
        }
        return normalized;
    }
}

@@ -762,16 +762,46 @@
 tions;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -2003,24 +2003,105 @@
 heapSize) %7B%0A
+        if (heapSize == null) %7B%0A            return -1; // unspecified%0A        %7D%0A%0A
         Stri
@@ -2765,32 +2765,117 @@
 ing%3E jvmArgs) %7B%0A
+        if (jvmArgs == null) %7B%0A            return Collections.emptySet();%0A        %7D%0A%0A
         Set%3CStri

handle null elements in daemon fork options