ListMap.java 2.8 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ListMap {
    private List list = new LinkedList();
    private Map map = null;

    public ListMap() {
        this.map = new HashMap();
    }

    public ListMap(int size) {
        this.map = new HashMap(size);
    }

    public int size() {
        return this.list.size();
    }

    public Iterator iterator() {
        return this.list.iterator();
    }

    public int put(Object key, Object value) {
        if (this.map.get(key) != null) {
            throw new IllegalArgumentException("Key '" + key + "' already exists in the ListMap.");
        }
        this.map.put(key, value);
        this.list.add(new Entry(key, value));
        return this.list.size() - 1;
    }

    public Object get(Object key) {
        return this.map.get(key);
    }

    public Entry get(int index) {
        return (Entry)this.list.get(index);
    }

    public boolean isEmpty() {
        return this.list.isEmpty();
    }

    public void clear() {
        this.list.clear();
        this.map.clear();
    }

    public String toString() {
        String buf = "";
        Iterator listIf = this.list.iterator();
        while (listIf.hasNext()) {
            Entry entry = (Entry)listIf.next();
            if (entry.val instanceof ListMap) {
                buf = buf + "" + entry.key + "= (ListMap):\n" + ((ListMap)entry.val).toString(1);
                continue;
            }
            buf = buf + "" + entry.key + "=" + entry.getValue() + "\n";
        }
        return buf;
    }

    private String toString(int indent) {
        String buf = "";
        String white = "";
        for (int i = 0; i < indent; ++i) {
            white = white + "  ";
        }
        Iterator listIf = this.list.iterator();
        while (listIf.hasNext()) {
            Entry entry = (Entry)listIf.next();
            if (entry.val instanceof ListMap) {
                buf = buf + white + entry.key + "=\n" + ((ListMap)entry.val).toString(indent + 1);
                continue;
            }
            buf = buf + white + entry.key + "=" + entry.val + "\n";
        }
        return buf;
    }

    public Entry getLast() {
        if (this.list.size() == 0) {
            return null;
        }
        return (Entry)this.list.get(this.list.size() - 1);
    }

    public class Entry {
        private Object key;
        private Object val;

        public Entry(Object key, Object val) {
            this.key = null;
            this.val = null;
            this.key = key;
            this.val = val;
        }

        public Object getKey() {
            return this.key;
        }

        public Object getValue() {
            return this.val;
        }
    }

}