/*
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

package org.broad.igv.util.collections;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * A "map-like" class that supports multiple values for a given key.
 * Note this violates the map contract,  thus "map-like".
 * Created to support GFF column 9 and Genbank tag-value pairs.
 */
public class MultiMap<K, V> {

    LinkedHashMap<K, Object> map;
    int size = 0;

    public MultiMap() {
        this(10);
    }

    public MultiMap(int size) {
        map = new LinkedHashMap<K, Object>(size);
    }

    public void put(K key, V value) {
        Object currentValue = map.get(key);
        if (currentValue == null) {
            map.put(key, value);
        } else if (currentValue instanceof List) {
            ((List) currentValue).add(value);
        } else {
            List<V> valueList = new ArrayList<V>();
            valueList.add((V) currentValue);
            valueList.add(value);
            map.put(key, valueList);
        }
        size++;
    }

    public V get(K key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        } else if (value instanceof List) {
            return ((List<V>) value).get(0);
        } else {
            return (V) value;
        }
    }

    public V remove(K key) {
        Object value = map.get(key);
        if (value == null) return null;
        if (value instanceof List) {
            size -= ((List) value).size();
            map.remove(key);
            return (V) ((List) value).get(0);
        } else {
            size--;
            map.remove(key);
            return (V) value;
        }
    }

    public int size() {
        return size;
    }

    public boolean containsKey(K key) {
        return map.containsKey(key);
    }

    public void printHtml(StringBuffer buffer, int max) {

        if (map == null || map.isEmpty()) return;

        int count = 0;

        buffer.append("<br>");
        for (Map.Entry<K, Object> entry : map.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof List) {
                for (V v : ((List<V>) value)) {
                    buffer.append(entry.getKey());
                    buffer.append("=");
                    buffer.append(v.toString());
                    buffer.append("<br/>");
                    count++;
                }
            } else {
                buffer.append(entry.getKey());
                buffer.append("=");
                buffer.append(value.toString());
                buffer.append("<br/>");
                count++;
            }
            if (++count > max) {
                buffer.append("...");
                break;
            }

        }
    }

    public List<V> values() {
        List<V> allValues = new ArrayList<V>(map.size());
        for (Map.Entry<K, Object> entry : map.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof List) {
                allValues.addAll((List) value);

            } else {
                allValues.add((V) value);
            }

        }
        return allValues;
    }
}

@@ -2348,32 +2348,87 @@
 ey(key);%0A    %7D%0A%0A
+    private static final int MAX_CHARS_PER_LINE = 50;%0A%0A
     public void 
@@ -2469,16 +2469,16 @@
  max) %7B%0A
-
 %0A       
@@ -2540,25 +2540,24 @@
  count = 0;%0A
-%0A
         buff
@@ -3124,38 +3124,107 @@
     
-buffer.append(value.toString()
+String ts = lineWrapString(value.toString(), MAX_CHARS_PER_LINE);%0A%0A                buffer.append(ts
 );%0A 
@@ -3402,32 +3402,32 @@
 %0A            %7D%0A%0A
-
         %7D%0A    %7D%0A
@@ -3419,32 +3419,546 @@
        %7D%0A    %7D%0A%0A
+    private String lineWrapString(String input, int maxCharsPerLine) %7B%0A        int lines = input.length() / maxCharsPerLine + 1;%0A        if (lines == 1) return input;%0A%0A        String result = input.substring(0, maxCharsPerLine);%0A        for (int lineNum = 1; lineNum %3C lines; lineNum++) %7B%0A            int start = lineNum * maxCharsPerLine;%0A            int end = Math.min(start + maxCharsPerLine, input.length());%0A            result += %22%3Cbr/%3E%22 + input.substring(start, end);%0A        %7D%0A        return result;%0A    %7D%0A%0A
     public List%3C

Add line-wrapping for long lines to MultiMap.printHtml